util.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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) {
  74. dd.setDate(dd.getDate() + AddDayCount)
  75. } else {
  76. dd.setMonth(dd.getMonth() + AddDayCount) // 获取AddDayCount天后的日期
  77. }
  78. break
  79. case 'year':
  80. dd.setFullYear(dd.getFullYear() + AddDayCount) // 获取AddDayCount天后的日期
  81. break
  82. }
  83. const y = dd.getFullYear()
  84. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  85. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  86. return {
  87. fullDate: y + '-' + m + '-' + d,
  88. year: y,
  89. month: m,
  90. date: d,
  91. day: dd.getDay()
  92. }
  93. }
  94. /**
  95. * 获取上月剩余天数
  96. */
  97. _getLastMonthDays(firstDay, full) {
  98. let dateArr = []
  99. for (let i = firstDay; i > 0; i--) {
  100. const beforeDate = new Date(full.year, full.month - 1, -i + 1).getDate()
  101. dateArr.push({
  102. date: beforeDate,
  103. month: full.month - 1,
  104. lunar: this.getlunar(full.year, full.month - 1, beforeDate),
  105. disable: true
  106. })
  107. }
  108. return dateArr
  109. }
  110. /**
  111. * 获取本月天数
  112. */
  113. _currentMonthDys(dateData, full) {
  114. let dateArr = []
  115. let fullDate = this.date.fullDate
  116. for (let i = 1; i <= dateData; i++) {
  117. let isinfo = false
  118. let nowDate = full.year + '-' + (full.month < 10 ?
  119. full.month : full.month) + '-' + (i < 10 ?
  120. '0' + i : i)
  121. // 是否今天
  122. let isDay = fullDate === nowDate
  123. // 获取打点信息
  124. let info = this.selected && this.selected.find((item) => {
  125. if (this.dateEqual(nowDate, item.date)) {
  126. return item
  127. }
  128. })
  129. // 日期禁用
  130. let disableBefore = true
  131. let disableAfter = true
  132. if (this.startDate) {
  133. let dateCompBefore = this.dateCompare(this.startDate, fullDate)
  134. disableBefore = this.dateCompare(dateCompBefore ? this.startDate : fullDate, nowDate)
  135. }
  136. if (this.endDate) {
  137. let dateCompAfter = this.dateCompare(fullDate, this.endDate)
  138. disableAfter = this.dateCompare(nowDate, dateCompAfter ? this.endDate : fullDate)
  139. }
  140. let multiples = this.multipleStatus.data
  141. let checked = false
  142. let multiplesStatus = -1
  143. if (this.range) {
  144. if (multiples) {
  145. multiplesStatus = multiples.findIndex((item) => {
  146. return this.dateEqual(item, nowDate)
  147. })
  148. }
  149. if (multiplesStatus !== -1) {
  150. checked = true
  151. }
  152. }
  153. let data = {
  154. fullDate: nowDate,
  155. year: full.year,
  156. date: i,
  157. multiple: this.range ? checked : false,
  158. beforeMultiple: this.dateEqual(this.multipleStatus.before, nowDate),
  159. afterMultiple: this.dateEqual(this.multipleStatus.after, nowDate),
  160. month: full.month,
  161. lunar: this.getlunar(full.year, full.month, i),
  162. disable: !disableBefore || !disableAfter,
  163. isDay
  164. }
  165. if (info) {
  166. data.extraInfo = info
  167. }
  168. dateArr.push(data)
  169. }
  170. return dateArr
  171. }
  172. /**
  173. * 获取下月天数
  174. */
  175. _getNextMonthDays(surplus, full) {
  176. let dateArr = []
  177. for (let i = 1; i < surplus + 1; i++) {
  178. dateArr.push({
  179. date: i,
  180. month: Number(full.month) + 1,
  181. lunar: this.getlunar(full.year, Number(full.month) + 1, i),
  182. disable: true
  183. })
  184. }
  185. return dateArr
  186. }
  187. /**
  188. * 获取当前日期详情
  189. * @param {Object} date
  190. */
  191. getInfo(date) {
  192. if (!date) {
  193. date = new Date()
  194. }
  195. const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate)
  196. return dateInfo
  197. }
  198. /**
  199. * 比较时间大小
  200. */
  201. dateCompare(startDate, endDate) {
  202. // 计算截止时间
  203. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  204. // 计算详细项的截止时间
  205. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  206. if (startDate <= endDate) {
  207. return true
  208. } else {
  209. return false
  210. }
  211. }
  212. /**
  213. * 比较时间是否相等
  214. */
  215. dateEqual(before, after) {
  216. // 计算截止时间
  217. before = new Date(before.replace('-', '/').replace('-', '/'))
  218. // 计算详细项的截止时间
  219. after = new Date(after.replace('-', '/').replace('-', '/'))
  220. if (before.getTime() - after.getTime() === 0) {
  221. return true
  222. } else {
  223. return false
  224. }
  225. }
  226. /**
  227. * 获取日期范围内所有日期
  228. * @param {Object} begin
  229. * @param {Object} end
  230. */
  231. geDateAll(begin, end) {
  232. var arr = []
  233. var ab = begin.split('-')
  234. var ae = end.split('-')
  235. var db = new Date()
  236. db.setFullYear(ab[0], ab[1] - 1, ab[2])
  237. var de = new Date()
  238. de.setFullYear(ae[0], ae[1] - 1, ae[2])
  239. var unixDb = db.getTime() - 24 * 60 * 60 * 1000
  240. var unixDe = de.getTime() - 24 * 60 * 60 * 1000
  241. for (var k = unixDb; k <= unixDe;) {
  242. k = k + 24 * 60 * 60 * 1000
  243. arr.push(this.getDate(new Date(parseInt(k))).fullDate)
  244. }
  245. return arr
  246. }
  247. /**
  248. * 计算阴历日期显示
  249. */
  250. getlunar(year, month, date) {
  251. return CALENDAR.solar2lunar(year, month, date)
  252. }
  253. /**
  254. * 设置打点
  255. */
  256. setSelectInfo(data, value) {
  257. this.selected = value
  258. this._getWeek(data)
  259. }
  260. /**
  261. * 获取多选状态
  262. */
  263. setMultiple(fullDate) {
  264. let {
  265. before,
  266. after
  267. } = this.multipleStatus
  268. if (!this.range) return
  269. if (before && after) {
  270. this.multipleStatus.before = ''
  271. this.multipleStatus.after = ''
  272. this.multipleStatus.data = []
  273. } else {
  274. if (!before) {
  275. this.multipleStatus.before = fullDate
  276. } else {
  277. this.multipleStatus.after = fullDate
  278. if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) {
  279. this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus.after);
  280. } else {
  281. this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus.before);
  282. }
  283. }
  284. }
  285. this._getWeek(fullDate)
  286. }
  287. /**
  288. * 获取每周数据
  289. * @param {Object} dateData
  290. */
  291. _getWeek(dateData) {
  292. const {
  293. fullDate,
  294. year,
  295. month,
  296. date,
  297. day
  298. } = this.getDate(dateData)
  299. let firstDay = new Date(year, month - 1, 1).getDay()
  300. let currentDay = new Date(year, month, 0).getDate()
  301. let dates = {
  302. lastMonthDays: this._getLastMonthDays(firstDay, this.getDate(dateData)), // 上个月末尾几天
  303. currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天数
  304. nextMonthDays: [], // 下个月开始几天
  305. weeks: []
  306. }
  307. let canlender = []
  308. const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
  309. dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData))
  310. canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)
  311. let weeks = {}
  312. // 拼接数组 上个月开始几天 + 本月天数+ 下个月开始几天
  313. for (let i = 0; i < canlender.length; i++) {
  314. if (i % 7 === 0) {
  315. weeks[parseInt(i / 7)] = new Array(7)
  316. }
  317. weeks[parseInt(i / 7)][i % 7] = canlender[i]
  318. }
  319. this.canlender = canlender
  320. this.weeks = weeks
  321. }
  322. //静态方法
  323. // static init(date) {
  324. // if (!this.instance) {
  325. // this.instance = new Calendar(date);
  326. // }
  327. // return this.instance;
  328. // }
  329. }
  330. export default Calendar