util.js 8.2 KB

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