calendar.js 7.6 KB

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