util.js 8.2 KB

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