util.js 8.3 KB

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