util.js 8.7 KB

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