util.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import CALENDAR from './calendar.js'
  2. class Calendar {
  3. constructor({
  4. date,
  5. selected,
  6. startDate,
  7. endDate,
  8. range
  9. } = {}) {
  10. // 当前日期
  11. this.date = this.getDate(new Date()) // 当前初入日期
  12. // 打点信息
  13. this.selected = selected || [];
  14. // 范围开始
  15. this.startDate = startDate
  16. // 范围结束
  17. this.endDate = endDate
  18. this.range = range
  19. // 多选状态
  20. this.cleanMultipleStatus()
  21. // 每周日期
  22. this.weeks = {}
  23. // this._getWeek(this.date.fullDate)
  24. }
  25. /**
  26. * 设置日期
  27. * @param {Object} date
  28. */
  29. setDate(date) {
  30. this.selectDate = this.getDate(date)
  31. this._getWeek(this.selectDate.fullDate)
  32. }
  33. /**
  34. * 清理多选状态
  35. */
  36. cleanMultipleStatus() {
  37. this.multipleStatus = {
  38. before: '',
  39. after: '',
  40. data: []
  41. }
  42. }
  43. /**
  44. * 重置开始日期
  45. */
  46. resetSatrtDate(startDate) {
  47. // 范围开始
  48. this.startDate = startDate
  49. }
  50. /**
  51. * 重置结束日期
  52. */
  53. resetEndDate(endDate) {
  54. // 范围结束
  55. this.endDate = endDate
  56. }
  57. /**
  58. * 获取任意时间
  59. */
  60. getDate(date, AddDayCount = 0, str = 'day') {
  61. if (!date) {
  62. date = new Date()
  63. }
  64. if (typeof date !== 'object') {
  65. date = date.replace(/-/g, '/')
  66. }
  67. const dd = new Date(date)
  68. switch (str) {
  69. case 'day':
  70. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  71. break
  72. case 'month':
  73. if (dd.getDate() === 31 && AddDayCount > 0) {
  74. dd.setDate(dd.getDate() + AddDayCount)
  75. } else {
  76. const preMonth = dd.getMonth()
  77. dd.setMonth(preMonth + AddDayCount) // 获取AddDayCount天后的日期
  78. const nextMonth = dd.getMonth()
  79. // 处理 pre 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
  80. if (AddDayCount < 0 && preMonth !== 0 && nextMonth - preMonth > AddDayCount) {
  81. dd.setMonth(nextMonth + (nextMonth - preMonth + AddDayCount))
  82. }
  83. // 处理 next 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
  84. if (AddDayCount > 0 && nextMonth - preMonth > AddDayCount) {
  85. dd.setMonth(nextMonth - (nextMonth - preMonth - AddDayCount))
  86. }
  87. }
  88. break
  89. case 'year':
  90. dd.setFullYear(dd.getFullYear() + AddDayCount) // 获取AddDayCount天后的日期
  91. break
  92. }
  93. const y = dd.getFullYear()
  94. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  95. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  96. return {
  97. fullDate: y + '-' + m + '-' + d,
  98. year: y,
  99. month: m,
  100. date: d,
  101. day: dd.getDay()
  102. }
  103. }
  104. /**
  105. * 获取上月剩余天数
  106. */
  107. _getLastMonthDays(firstDay, full) {
  108. let dateArr = []
  109. for (let i = firstDay; i > 0; i--) {
  110. const beforeDate = new Date(full.year, full.month - 1, -i + 1).getDate()
  111. dateArr.push({
  112. date: beforeDate,
  113. month: full.month - 1,
  114. lunar: this.getlunar(full.year, full.month - 1, beforeDate),
  115. disable: true
  116. })
  117. }
  118. // 获取打点信息
  119. for (let i = 0; i < dateArr.length; i++) {
  120. let nowDate = full.year + '-' + (dateArr[i].month < 10 ?
  121. '0' + dateArr[i].month : dateArr[i].month) + '-' + (dateArr[i].date < 10 ?
  122. '0' + dateArr[i].date : dateArr[i].date)
  123. let info = this.selected && this.selected.find((item) => {
  124. if (this.dateEqual(nowDate, item.date)) {
  125. return item
  126. }
  127. })
  128. if (info) dateArr[i].extraInfo = info
  129. }
  130. return dateArr
  131. }
  132. /**
  133. * 获取本月天数
  134. */
  135. _currentMonthDys(dateData, full) {
  136. let dateArr = []
  137. let fullDate = this.date.fullDate
  138. for (let i = 1; i <= dateData; i++) {
  139. let nowDate = full.year + '-' + (full.month < 10 ?
  140. full.month : full.month) + '-' + (i < 10 ?
  141. '0' + i : i)
  142. // 是否今天
  143. let isDay = fullDate === nowDate
  144. // 获取打点信息
  145. let info = this.selected && this.selected.find((item) => {
  146. if (this.dateEqual(nowDate, item.date)) {
  147. return item
  148. }
  149. })
  150. // 日期禁用
  151. let disableBefore = true
  152. let disableAfter = true
  153. if (this.startDate) {
  154. // let dateCompBefore = this.dateCompare(this.startDate, fullDate)
  155. // disableBefore = this.dateCompare(dateCompBefore ? this.startDate : fullDate, nowDate)
  156. disableBefore = this.dateCompare(this.startDate, nowDate)
  157. }
  158. if (this.endDate) {
  159. // let dateCompAfter = this.dateCompare(fullDate, this.endDate)
  160. // disableAfter = this.dateCompare(nowDate, dateCompAfter ? this.endDate : fullDate)
  161. disableAfter = this.dateCompare(nowDate, this.endDate)
  162. }
  163. let multiples = this.multipleStatus.data
  164. let checked = false
  165. let multiplesStatus = -1
  166. if (this.range) {
  167. if (multiples) {
  168. multiplesStatus = multiples.findIndex((item) => {
  169. return this.dateEqual(item, nowDate)
  170. })
  171. }
  172. if (multiplesStatus !== -1) {
  173. checked = true
  174. }
  175. }
  176. let data = {
  177. fullDate: nowDate,
  178. year: full.year,
  179. date: i,
  180. multiple: this.range ? checked : false,
  181. beforeMultiple: this.dateEqual(this.multipleStatus.before, nowDate),
  182. afterMultiple: this.dateEqual(this.multipleStatus.after, nowDate),
  183. month: full.month,
  184. lunar: this.getlunar(full.year, full.month, i),
  185. disable: !(disableBefore && disableAfter),
  186. isDay
  187. }
  188. if (info) {
  189. data.extraInfo = info
  190. }
  191. dateArr.push(data)
  192. }
  193. return dateArr
  194. }
  195. /**
  196. * 获取下月天数
  197. */
  198. _getNextMonthDays(surplus, full) {
  199. let dateArr = []
  200. for (let i = 1; i < surplus + 1; i++) {
  201. dateArr.push({
  202. date: i,
  203. month: Number(full.month) + 1,
  204. lunar: this.getlunar(full.year, Number(full.month) + 1, i),
  205. disable: true
  206. })
  207. }
  208. // 获取打点信息
  209. for (let i = 0; i < dateArr.length; i++) {
  210. let nowDate = full.year + '-' + (dateArr[i].month < 10 ?
  211. '0' + dateArr[i].month : dateArr[i].month) + '-' + (dateArr[i].date < 10 ?
  212. '0' + dateArr[i].date : dateArr[i].date)
  213. let info = this.selected && this.selected.find((item) => {
  214. if (this.dateEqual(nowDate, item.date)) {
  215. return item
  216. }
  217. })
  218. if (info) dateArr[i].extraInfo = info
  219. }
  220. return dateArr
  221. }
  222. /**
  223. * 获取当前日期详情
  224. * @param {Object} date
  225. */
  226. getInfo(date) {
  227. if (!date) {
  228. date = new Date()
  229. }
  230. const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate)
  231. return dateInfo
  232. }
  233. /**
  234. * 比较时间大小
  235. */
  236. dateCompare(startDate, endDate) {
  237. // 计算截止时间
  238. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  239. // 计算详细项的截止时间
  240. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  241. if (startDate <= endDate) {
  242. return true
  243. } else {
  244. return false
  245. }
  246. }
  247. /**
  248. * 比较时间是否相等
  249. */
  250. dateEqual(before, after) {
  251. // 计算截止时间
  252. before = new Date(before.replace('-', '/').replace('-', '/'))
  253. // 计算详细项的截止时间
  254. after = new Date(after.replace('-', '/').replace('-', '/'))
  255. if (before.getTime() - after.getTime() === 0) {
  256. return true
  257. } else {
  258. return false
  259. }
  260. }
  261. /**
  262. * 获取日期范围内所有日期
  263. * @param {Object} begin
  264. * @param {Object} end
  265. */
  266. geDateAll(begin, end) {
  267. var arr = []
  268. var ab = begin.split('-')
  269. var ae = end.split('-')
  270. var db = new Date()
  271. db.setFullYear(ab[0], ab[1] - 1, ab[2])
  272. var de = new Date()
  273. de.setFullYear(ae[0], ae[1] - 1, ae[2])
  274. var unixDb = db.getTime() - 24 * 60 * 60 * 1000
  275. var unixDe = de.getTime() - 24 * 60 * 60 * 1000
  276. for (var k = unixDb; k <= unixDe;) {
  277. k = k + 24 * 60 * 60 * 1000
  278. arr.push(this.getDate(new Date(parseInt(k))).fullDate)
  279. }
  280. return arr
  281. }
  282. /**
  283. * 计算阴历日期显示
  284. */
  285. getlunar(year, month, date) {
  286. return CALENDAR.solar2lunar(year, month, date)
  287. }
  288. /**
  289. * 设置打点
  290. */
  291. setSelectInfo(data, value) {
  292. this.selected = value
  293. this._getWeek(data)
  294. }
  295. /**
  296. * 获取多选状态
  297. */
  298. setMultiple(fullDate) {
  299. let {
  300. before,
  301. after
  302. } = this.multipleStatus
  303. if (!this.range) return
  304. if (before && after) {
  305. this.multipleStatus.before = ''
  306. this.multipleStatus.after = ''
  307. this.multipleStatus.data = []
  308. } else {
  309. if (!before) {
  310. this.multipleStatus.before = fullDate
  311. } else {
  312. this.multipleStatus.after = fullDate
  313. if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) {
  314. this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus
  315. .after);
  316. } else {
  317. this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus
  318. .before);
  319. }
  320. }
  321. }
  322. this._getWeek(fullDate)
  323. }
  324. /**
  325. * 获取每周数据
  326. * @param {Object} dateData
  327. */
  328. _getWeek(dateData) {
  329. const {
  330. year,
  331. month
  332. } = this.getDate(dateData)
  333. let firstDay = new Date(year, month - 1, 1).getDay()
  334. let currentDay = new Date(year, month, 0).getDate()
  335. let dates = {
  336. lastMonthDays: this._getLastMonthDays(firstDay, this.getDate(dateData)), // 上个月末尾几天
  337. currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天数
  338. nextMonthDays: [], // 下个月开始几天
  339. weeks: []
  340. }
  341. let canlender = []
  342. const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
  343. dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData))
  344. canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)
  345. let weeks = {}
  346. // 拼接数组 上个月开始几天 + 本月天数+ 下个月开始几天
  347. for (let i = 0; i < canlender.length; i++) {
  348. if (i % 7 === 0) {
  349. weeks[parseInt(i / 7)] = new Array(7)
  350. }
  351. weeks[parseInt(i / 7)][i % 7] = canlender[i]
  352. }
  353. this.canlender = canlender
  354. this.weeks = weeks
  355. }
  356. //静态方法
  357. // static init(date) {
  358. // if (!this.instance) {
  359. // this.instance = new Calendar(date);
  360. // }
  361. // return this.instance;
  362. // }
  363. }
  364. export default Calendar