calendar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // component/calendar/calendar.js
  2. import {
  3. DateUtil
  4. } from '../../../utils/getTime.js'
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. spotMap: { //标点的日期
  11. type: Object,
  12. value: {}
  13. },
  14. defaultTime: { //标记的日期,默认为今日
  15. type: String,
  16. value: ''
  17. },
  18. title: { //标题
  19. type: String,
  20. value: ''
  21. },
  22. goNow: { // 是否有快速回到今天的功能
  23. type: Boolean,
  24. value: true,
  25. }
  26. },
  27. /**
  28. * 组件的初始数据
  29. */
  30. data: {
  31. startTime: DateUtil.getStartDayOfWeek(),
  32. endTime: DateUtil.getEndDayOfWeek(),
  33. selectDay: {}, //选中时间
  34. nowDay: {}, //现在时间
  35. open: false,
  36. swiperCurrent: 1, //选中时间
  37. oldCurrent: 1, //之前选中时间
  38. dateList0: [], //0位置的日历数组
  39. dateList1: [], //1位置的日历数组
  40. dateList2: [], //2位置的日历数组
  41. swiperDuration: 500,
  42. swiperHeight: 0,
  43. backChange: false, //跳过change切换
  44. },
  45. /**
  46. * 组件的方法列表
  47. */
  48. methods: {
  49. toLeft: function (e) {
  50. const index = this.data.swiperCurrent - 1 < 0 ? 2 : this.data.swiperCurrent - 1;
  51. this.setData({
  52. swiperCurrent: index,
  53. });
  54. },
  55. toRight: function (e) {
  56. const index = this.data.swiperCurrent + 1 > 2 ? 0 : this.data.swiperCurrent + 1;
  57. this.setData({
  58. swiperCurrent: index,
  59. });
  60. },
  61. addZero(num) {
  62. if (num < 10) {
  63. num = "0" + num;
  64. }
  65. return num;
  66. },
  67. // 禁止用户滑动
  68. stopTouchMove: function() {
  69. return false;
  70. },
  71. swiperChange(e) { // 日历滑动时触发的方法
  72. if (this.data.backChange) {
  73. this.setData({
  74. backChange: false
  75. })
  76. return
  77. }
  78. //计算第三个索引
  79. let rest = 3 - this.data.swiperCurrent - this.data.oldCurrent
  80. let dif = this.data.swiperCurrent - this.data.oldCurrent
  81. let date
  82. if (dif === -2 || (dif > 0 && dif !== 2)) { //向右划的情况,日期增加
  83. if (this.data.open) {
  84. date = new Date(this.data.selectDay.year, this.data.selectDay.month)
  85. this.setMonth(date.getFullYear(), date.getMonth() + 1, undefined)
  86. this.getIndexList({
  87. setYear: this.data.selectDay.year,
  88. setMonth: this.data.selectDay.month,
  89. dateIndex: rest
  90. })
  91. } else {
  92. date = new Date(this.data.selectDay.year, this.data.selectDay.month - 1, this.data.selectDay.day + 7)
  93. this.setMonth(date.getFullYear(), date.getMonth() + 1, date.getDate())
  94. this.getIndexList({
  95. setYear: this.data.selectDay.year,
  96. setMonth: this.data.selectDay.month - 1,
  97. setDay: this.data.selectDay.day + 7,
  98. dateIndex: rest
  99. })
  100. let tempData = this.data[`dateList${this.data.swiperCurrent }`];
  101. let startTime = tempData[0].year + '-' + this.addZero(tempData[0].month) + '-' + this.addZero(tempData[0].day);
  102. let endTime = tempData[6].year + '-' + this.addZero(tempData[6].month) + '-' + this.addZero(tempData[6].day);
  103. this.setData({
  104. startTime: startTime,
  105. endTime: endTime,
  106. })
  107. this.triggerEvent("myTime", {
  108. data: this.data[`dateList${this.data.swiperCurrent }`]
  109. })
  110. this.data.swiperCurrent - this.data.oldCurrent
  111. }
  112. } else { //向左划的情况,日期减少
  113. if (this.data.open) {
  114. date = new Date(this.data.selectDay.year, this.data.selectDay.month - 2)
  115. this.setMonth(date.getFullYear(), date.getMonth() + 1, undefined)
  116. this.getIndexList({
  117. setYear: this.data.selectDay.year,
  118. setMonth: this.data.selectDay.month - 2,
  119. dateIndex: rest
  120. })
  121. } else {
  122. date = new Date(this.data.selectDay.year, this.data.selectDay.month - 1, this.data.selectDay.day - 7)
  123. this.setMonth(date.getFullYear(), date.getMonth() + 1, date.getDate())
  124. this.getIndexList({
  125. setYear: this.data.selectDay.year,
  126. setMonth: this.data.selectDay.month - 1,
  127. setDay: this.data.selectDay.day - 7,
  128. dateIndex: rest
  129. })
  130. let tempData = this.data[`dateList${this.data.swiperCurrent }`];
  131. let startTime = tempData[0].year + '-' + this.addZero(tempData[0].month) + '-' + this.addZero(tempData[0].day);
  132. let endTime = tempData[6].year + '-' + this.addZero(tempData[6].month) + '-' + this.addZero(tempData[6].day);
  133. this.setData({
  134. startTime: startTime,
  135. endTime: endTime,
  136. })
  137. this.triggerEvent("myTime", {
  138. data: this.data[`dateList${this.data.swiperCurrent }`]
  139. })
  140. }
  141. }
  142. this.setData({
  143. oldCurrent: this.data.swiperCurrent
  144. })
  145. this.setSwiperHeight(this.data.swiperCurrent )
  146. },
  147. setSwiperHeight(index) { // 根据指定位置数组的大小计算长度
  148. this.setData({
  149. swiperHeight: this.data[`dateList${index}`].length / 7 * 82 + 18
  150. })
  151. },
  152. //更新指定的索引和月份的列表
  153. getIndexList({
  154. setYear,
  155. setMonth,
  156. setDay = void 0,
  157. dateIndex
  158. }) {
  159. let appointMonth
  160. if (setDay)
  161. appointMonth = new Date(setYear, setMonth, setDay)
  162. else
  163. appointMonth = new Date(setYear, setMonth)
  164. let listName = `dateList${dateIndex}`
  165. this.setData({
  166. [listName]: this.dateInit({
  167. setYear: appointMonth.getFullYear(),
  168. setMonth: appointMonth.getMonth() + 1,
  169. setDay: appointMonth.getDate(),
  170. hasBack: true
  171. }),
  172. })
  173. },
  174. //设置月份
  175. setMonth(setYear, setMonth, setDay) {
  176. const day = Math.min(new Date(setYear, setMonth, 0).getDate(), this.data.selectDay.day)
  177. if (this.data.selectDay.year !== setYear || this.data.selectDay.month !== setMonth) {
  178. const data = {
  179. selectDay: {
  180. year: setYear,
  181. month: setMonth,
  182. day: setDay ? setDay : day
  183. },
  184. }
  185. if (!setDay) {
  186. data.open = true
  187. }
  188. this.setData(data, () => {
  189. this.triggerEvent("selectDay", {
  190. data: this.data.selectDay
  191. })
  192. })
  193. } else {
  194. const data = {
  195. selectDay: {
  196. year: setYear,
  197. month: setMonth,
  198. day: setDay ? setDay : day
  199. },
  200. }
  201. this.setData(data, () => {
  202. this.triggerEvent("selectDay", {
  203. data: this.data.selectDay
  204. })
  205. })
  206. }
  207. },
  208. //展开收起
  209. // openChange() {
  210. // this.setData({
  211. // open: !this.data.open
  212. // })
  213. // this.triggerEvent("aaa", {
  214. // a: 0
  215. // })
  216. // // 更新数据
  217. // const selectDate = new Date(this.data.selectDay.year, this.data.selectDay.month - 1, this.data.selectDay.day)
  218. // if (this.data.oldCurrent === 0) {
  219. // this.updateList(selectDate, -1, 2)
  220. // this.updateList(selectDate, 0, 0)
  221. // this.updateList(selectDate, 1, 1)
  222. // } else if (this.data.oldCurrent === 1) {
  223. // this.updateList(selectDate, -1, 0)
  224. // this.updateList(selectDate, 0, 1)
  225. // this.updateList(selectDate, 1, 2)
  226. // } else if (this.data.oldCurrent === 2) {
  227. // this.updateList(selectDate, -1, 1)
  228. // this.updateList(selectDate, 0, 2)
  229. // this.updateList(selectDate, 1, 0)
  230. // }
  231. // this.setSwiperHeight(this.data.oldCurrent)
  232. // },
  233. // 选中并切换今日日期
  234. // switchNowDate() {
  235. // const now = new Date()
  236. // const selectDate = new Date(this.data.selectDay.year, this.data.selectDay.month - 1, this.data.selectDay.day)
  237. // let dateDiff = (selectDate.getFullYear() - now.getFullYear()) * 12 + (selectDate.getMonth() - now.getMonth())
  238. // let diff = dateDiff === 0 ? 0 : dateDiff > 0 ? -1 : 1
  239. // const diffSum = (x) => (3 + (x % 3)) % 3
  240. // if (this.data.oldCurrent === 0) {
  241. // this.updateList(now, -1, diffSum(2 + diff))
  242. // this.updateList(now, 0, diffSum(0 + diff))
  243. // this.updateList(now, 1, diffSum(1 + diff))
  244. // } else if (this.data.oldCurrent === 1) {
  245. // this.updateList(now, -1, diffSum(0 + diff))
  246. // this.updateList(now, 0, diffSum(1 + diff))
  247. // this.updateList(now, 1, diffSum(2 + diff))
  248. // } else if (this.data.oldCurrent === 2) {
  249. // this.updateList(now, -1, diffSum(1 + diff))
  250. // this.updateList(now, 0, diffSum(2 + diff))
  251. // this.updateList(now, 1, diffSum(0 + diff))
  252. // }
  253. // this.setData({
  254. // swiperCurrent: diffSum(this.data.oldCurrent + diff),
  255. // oldCurrent: diffSum(this.data.oldCurrent + diff),
  256. // backChange: dateDiff !== 0,
  257. // })
  258. // this.setData({
  259. // selectDay: {
  260. // year: now.getFullYear(),
  261. // month: now.getMonth() + 1,
  262. // day: now.getDate()
  263. // }
  264. // }, () => {
  265. // this.triggerEvent("selectDay", this.data.selectDay)
  266. // })
  267. // this.setSwiperHeight(this.data.oldCurrent)
  268. // },
  269. //日历主体的渲染方法
  270. dateInit({
  271. setYear,
  272. setMonth,
  273. setDay = this.data.selectDay.day,
  274. hasBack = false
  275. } = {
  276. setYear: this.data.selectDay.year,
  277. setMonth: this.data.selectDay.month,
  278. setDay: this.data.selectDay.day,
  279. hasBack: false
  280. }) {
  281. let dateList = []; //需要遍历的日历数组数据
  282. let now = new Date(setYear, setMonth - 1) //当前月份的1号
  283. let startWeek = now.getDay(); //目标月1号对应的星期
  284. let resetStartWeek = startWeek == 0 ? 6 : startWeek - 1; //重新定义星期将星期天替换为6其余-1
  285. let dayNum = new Date(setYear, setMonth, 0).getDate() //当前月有多少天
  286. let forNum = Math.ceil((resetStartWeek + dayNum) / 7) * 7 //当前月跨越的周数
  287. let selectDay = setDay ? setDay : this.data.selectDay.day
  288. this.triggerEvent("getDateList", {
  289. setYear: now.getFullYear(),
  290. setMonth: now.getMonth() + 1
  291. })
  292. if (this.data.open) {
  293. //展开状态,需要渲染完整的月份
  294. for (let i = 0; i < forNum; i++) {
  295. const now2 = new Date(now)
  296. now2.setDate(i - resetStartWeek + 1)
  297. let obj = {};
  298. obj = {
  299. day: now2.getDate(),
  300. month: now2.getMonth() + 1,
  301. year: now2.getFullYear()
  302. };
  303. dateList[i] = obj;
  304. }
  305. } else {
  306. //非展开状态,只需要渲染当前周
  307. for (let i = 0; i < 7; i++) {
  308. const now2 = new Date(now)
  309. //当前周的7天
  310. now2.setDate(Math.ceil((selectDay + (startWeek - 1)) / 7) * 7 - 6 - (startWeek - 1) + i)
  311. let obj = {};
  312. obj = {
  313. day: now2.getDate(),
  314. month: now2.getMonth() + 1,
  315. year: now2.getFullYear()
  316. };
  317. dateList[i] = obj;
  318. }
  319. }
  320. if (hasBack) {
  321. return dateList
  322. }
  323. this.setData({
  324. dateList1: dateList
  325. })
  326. },
  327. //一天被点击时
  328. chooseDate(e) {
  329. const year = e.currentTarget.dataset.year
  330. const month = e.currentTarget.dataset.month
  331. const day = e.currentTarget.dataset.day
  332. let Today = year + '-' + month + '-' + day;
  333. this.triggerEvent("Today", {
  334. data: Today
  335. })
  336. const selectDay = {
  337. year: year,
  338. month: month,
  339. day: day,
  340. }
  341. if (this.data.open && (this.data.selectDay.year !== year || this.data.selectDay.month !== month)) {
  342. if ((year * 12 + month) > (this.data.selectDay.year * 12 + this.data.selectDay.month)) { // 下个月
  343. if (this.data.oldCurrent == 2)
  344. this.setData({
  345. swiperCurrent: 0
  346. })
  347. else
  348. this.setData({
  349. swiperCurrent: this.data.oldCurrent + 1
  350. })
  351. } else { // 点击上个月
  352. if (this.data.oldCurrent == 0)
  353. this.setData({
  354. swiperCurrent: 2
  355. })
  356. else
  357. this.setData({
  358. swiperCurrent: this.data.oldCurrent - 1
  359. })
  360. }
  361. this.setData({
  362. ['selectDay.day']: day
  363. }, () => {
  364. // this.triggerEvent("selectDay", { data: this.data.selectDay })
  365. })
  366. } else if (this.data.selectDay.day !== day) {
  367. this.setData({
  368. selectDay: selectDay
  369. }, () => {
  370. // this.triggerEvent("selectDay", { data: this.data.selectDay })
  371. })
  372. }
  373. this.triggerEvent("Today", {
  374. data: Today
  375. })
  376. },
  377. updateList(date, offset, index) {
  378. if (this.data.open) { //打开状态
  379. const setDate = new Date(date.getFullYear(), date.getMonth() + offset * 1) //取得当前日期的上个月日期
  380. this.getIndexList({
  381. setYear: setDate.getFullYear(),
  382. setMonth: setDate.getMonth(),
  383. dateIndex: index
  384. })
  385. } else {
  386. const setDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + offset * 7) //取得当前日期的七天后的日期
  387. this.getIndexList({
  388. setYear: setDate.getFullYear(),
  389. setMonth: setDate.getMonth(),
  390. setDay: setDate.getDate(),
  391. dateIndex: index
  392. })
  393. }
  394. },
  395. },
  396. lifetimes: {
  397. attached() {
  398. let now = this.data.defaultTime ? new Date(this.data.defaultTime) : new Date()
  399. let selectDay = {
  400. year: now.getFullYear(),
  401. month: now.getMonth() + 1,
  402. day: now.getDate()
  403. }
  404. this.setData({
  405. nowDay: {
  406. year: now.getFullYear(),
  407. month: now.getMonth() + 1,
  408. day: now.getDate()
  409. }
  410. })
  411. this.setMonth(selectDay.year, selectDay.month, selectDay.day)
  412. this.updateList(now, -1, 0)
  413. this.updateList(now, 0, 1)
  414. this.updateList(now, 1, 2)
  415. this.setSwiperHeight(1)
  416. }
  417. },
  418. observers: {}
  419. })