calendar.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // components/calendar/calendar.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. spot: {
  8. type: Array,
  9. value: []
  10. },
  11. defaultTime: {
  12. type: String,
  13. value: ''
  14. }
  15. },
  16. /**
  17. * 组件的初始数据
  18. */
  19. data: {
  20. dateList: [], //日历主体渲染数组
  21. selectDay: {}, //选中时间
  22. open: true, //默认展开
  23. curMonth: '',
  24. eventList: []
  25. },
  26. /**
  27. * 组件的方法列表
  28. */
  29. methods: {
  30. //月份转为汉字
  31. formatMonth(month) {
  32. var arr = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
  33. return arr[parseInt(month, 10) - 1] + '月';
  34. },
  35. /**
  36. * 时间戳转化为年 月 日 时 分 秒
  37. * time: 需要被格式化的时间,可以被new Date()解析即可
  38. * format:格式化之后返回的格式,年月日时分秒分别为Y, M, D, h, m, s,这个参数不填的话则显示多久前
  39. */
  40. formatTime(time, format) {
  41. function formatNumber(n) {
  42. n = n.toString()
  43. return n[1] ? n : '0' + n
  44. }
  45. function getDate(time, format) {
  46. const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
  47. const returnArr = []
  48. const date = new Date(time)
  49. returnArr.push(date.getFullYear())
  50. returnArr.push(formatNumber(date.getMonth() + 1))
  51. returnArr.push(formatNumber(date.getDate()))
  52. returnArr.push(formatNumber(date.getHours()))
  53. returnArr.push(formatNumber(date.getMinutes()))
  54. returnArr.push(formatNumber(date.getSeconds()))
  55. for (const i in returnArr) {
  56. format = format.replace(formateArr[i], returnArr[i])
  57. }
  58. return format
  59. }
  60. function getDateDiff(time) {
  61. let r = ''
  62. const ft = new Date(time)
  63. const nt = new Date()
  64. const nd = new Date(nt)
  65. nd.setHours(23)
  66. nd.setMinutes(59)
  67. nd.setSeconds(59)
  68. nd.setMilliseconds(999)
  69. const d = parseInt((nd - ft) / 86400000)
  70. switch (true) {
  71. case d === 0:
  72. const t = parseInt(nt / 1000) - parseInt(ft / 1000)
  73. switch (true) {
  74. case t < 60:
  75. r = '刚刚'
  76. break
  77. case t < 3600:
  78. r = parseInt(t / 60) + '分钟前'
  79. break
  80. default:
  81. r = parseInt(t / 3600) + '小时前'
  82. }
  83. break
  84. case d === 1:
  85. r = '昨天'
  86. break
  87. case d === 2:
  88. r = '前天'
  89. break
  90. case d > 2 && d < 30:
  91. r = d + '天前'
  92. break
  93. default:
  94. r = getDate(time, 'Y-M-D')
  95. }
  96. return r
  97. }
  98. if (!format) {
  99. return getDateDiff(time)
  100. } else {
  101. return getDate(time, format)
  102. }
  103. },
  104. //picker设置月份
  105. editMonth(e) {
  106. const arr = e.detail.value.split("-")
  107. const year = parseInt(arr[0])
  108. const month = parseInt(arr[1])
  109. this.setMonth(year, month)
  110. },
  111. //上月切换按钮点击
  112. lastMonth() {
  113. const lastMonth = new Date(this.data.selectDay.year, this.data.selectDay.month - 2)
  114. const year = lastMonth.getFullYear()
  115. const month = lastMonth.getMonth() + 1
  116. this.setMonth(year, month)
  117. },
  118. //下月切换按钮点击
  119. nextMonth() {
  120. const nextMonth = new Date(this.data.selectDay.year, this.data.selectDay.month)
  121. const year = nextMonth.getFullYear()
  122. const month = nextMonth.getMonth() + 1
  123. this.setMonth(year, month)
  124. },
  125. //设置月份
  126. setMonth(setYear, setMonth, setDay) {
  127. if (this.data.selectDay.year !== setYear || this.data.selectDay.month !== setMonth) {
  128. const day = Math.min(new Date(setYear, setMonth, 0).getDate(), this.data.selectDay.day)
  129. const time = new Date(setYear, setMonth - 1, setDay ? setDay : day)
  130. const data = {
  131. curMonth: this.formatMonth(setMonth),
  132. selectDay: {
  133. year: setYear,
  134. month: setMonth,
  135. day: setDay ? setDay : day,
  136. dateString: this.formatTime(time, "Y-M-D")
  137. }
  138. }
  139. if (!setDay) {
  140. data.open = true
  141. }
  142. this.setData(data)
  143. this.dateInit(setYear, setMonth)
  144. this.setSpot()
  145. this.triggerEvent("change", this.data.selectDay)
  146. }
  147. },
  148. //展开收起
  149. openChange() {
  150. this.setData({
  151. open: !this.data.open
  152. })
  153. this.triggerEvent("aaa", { a: 0 })
  154. this.dateInit()
  155. this.setSpot()
  156. },
  157. //设置日历底下是否展示小圆点
  158. setSpot() {
  159. const timeArr = this.data.eventList.map(item => {
  160. return item.attenceDate
  161. })
  162. this.data.dateList.forEach(item => {
  163. if (timeArr.indexOf(item.dateString) !== -1) {
  164. item.spot = true
  165. } else {
  166. item.spot = false
  167. }
  168. })
  169. this.setData({
  170. dateList: this.data.dateList
  171. })
  172. },
  173. //日历主体的渲染方法
  174. dateInit(setYear = this.data.selectDay.year, setMonth = this.data.selectDay.month) {
  175. let dateList = []; //需要遍历的日历数组数据
  176. let now = new Date(setYear, setMonth - 1)//当前月份的1号
  177. let startWeek = now.getDay(); //目标月1号对应的星期
  178. let dayNum = new Date(setYear, setMonth, 0).getDate() //当前月有多少天
  179. let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的周数
  180. if (this.data.open) {
  181. //展开状态,需要渲染完整的月份
  182. for (let i = 0; i < forNum; i++) {
  183. const now2 = new Date(now)
  184. now2.setDate(i - startWeek + 1)
  185. let obj = {};
  186. obj = {
  187. day: now2.getDate(),
  188. month: now2.getMonth() + 1,
  189. year: now2.getFullYear(),
  190. dateString: this.formatTime(now2, "Y-M-D")
  191. };
  192. dateList[i] = obj;
  193. }
  194. } else {
  195. //非展开状态,只需要渲染当前周
  196. for (let i = 0; i < 7; i++) {
  197. const now2 = new Date(now)
  198. //当前周的7天
  199. now2.setDate(Math.ceil((this.data.selectDay.day + startWeek) / 7) * 7 - 6 - startWeek + i)
  200. let obj = {};
  201. obj = {
  202. day: now2.getDate(),
  203. month: now2.getMonth() + 1,
  204. year: now2.getFullYear(),
  205. dateString: this.formatTime(now2, "Y-M-D")
  206. };
  207. dateList[i] = obj;
  208. }
  209. }
  210. this.setData({
  211. dateList: dateList
  212. })
  213. },
  214. //一天被点击时
  215. selectChange(e) {
  216. const year = e.currentTarget.dataset.year
  217. const month = e.currentTarget.dataset.month
  218. const day = e.currentTarget.dataset.day
  219. const dateString = e.currentTarget.dataset.dateString
  220. const selectDay = {
  221. year: year,
  222. month: month,
  223. day: day,
  224. dateString: dateString
  225. }
  226. if (this.data.selectDay.year !== year || this.data.selectDay.month !== month) {
  227. this.setMonth(year, month, day)
  228. } else if (this.data.selectDay.day !== day) {
  229. this.setData({
  230. selectDay: selectDay
  231. })
  232. this.triggerEvent("change", this.data.selectDay)
  233. }
  234. }
  235. },
  236. lifetimes: {
  237. attached() {
  238. let now = this.data.defaultTime ? new Date(this.data.defaultTime) : new Date()
  239. let selectDay = {
  240. year: now.getFullYear(),
  241. month: now.getMonth() + 1,
  242. day: now.getDate(),
  243. dateString: this.formatTime(now, "Y-M-D")
  244. }
  245. this.setMonth(selectDay.year, selectDay.month, selectDay.day)
  246. }
  247. },
  248. observers: {
  249. spot: function (spot) {
  250. this.setData({ 'eventList': spot })
  251. this.setSpot()
  252. }
  253. }
  254. })