eventCalendar.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. const util = require("../../utils/util.js");
  2. Component({
  3. properties: {
  4. sid: {
  5. type: Number,
  6. value: -1,
  7. observer: function (newVal){
  8. this.setData({ sid: newVal });
  9. }
  10. },
  11. calendarInfo:{
  12. type: Array,
  13. value: [],
  14. observer: function (newVal){
  15. this.setData({ calendarInfo: newVal });
  16. this.calendarInfo()
  17. }
  18. },
  19. presellOpen:{
  20. type: Number,
  21. value: 0,
  22. observer: function (newVal){
  23. this.setData({ presellOpen: newVal });
  24. }
  25. }
  26. },
  27. data: {
  28. sid:-1,
  29. s1Sid:-1,
  30. dateList: [], //日历主体渲染数组
  31. selectDay: {}, //选中时间
  32. open:true, //展开、折叠日历
  33. monthList:[],
  34. selectedMonth:0,
  35. calendarInfo:[], //日历信息
  36. //selectDay:{},//日历选中日期
  37. presellOpen:0
  38. },
  39. methods: {
  40. // 解析月份列表
  41. calendarInfo() {
  42. let date={},monthList=[];
  43. this.data.calendarInfo.map((el,i) => {
  44. let dayVal=0,defalutDays=false;
  45. if (el.days && el.days.length>0) {
  46. el.days.forEach(it => {
  47. if (it.active) {
  48. dayVal=parseInt(it.days);
  49. defalutDays=true;
  50. this.setData({selectedMonth:i})
  51. let arr = it.month.split('-');
  52. date.year=parseInt(arr[0]);
  53. date.month=parseInt(arr[1]);
  54. date.day=parseInt(it.days);
  55. date.dateString=it.month+'-'+it.days;
  56. }
  57. });
  58. if (!defalutDays) {
  59. // dayVal=parseInt(el.days[0].days);
  60. dayVal = this.getDefaultDay(el.days,0);
  61. }
  62. }
  63. monthList.push({date:el.month+'-'+dayVal,month:el.month.split('-')[1]})
  64. });
  65. this.setData({monthList})
  66. this.setMonth(date.year, date.month, date.day,this.data.selectedMonth,this.data.sid)
  67. },
  68. // 递归遍历获取默认选中天数
  69. getDefaultDay(arr,i){
  70. if (arr.length>0 && i<arr.length) {
  71. if (arr[i].actNum>0 || arr[i].waitActNum>0) {
  72. return arr[i].days;
  73. } else {
  74. return this.getDefaultDay(arr,i+1);
  75. }
  76. }else{
  77. return 0;
  78. }
  79. },
  80. /**
  81. * 时间戳转化为年 月 日 时 分 秒
  82. * time: 需要被格式化的时间,可以被new Date()解析即可
  83. * format:格式化之后返回的格式,年月日时分秒分别为Y, M, D, h, m, s,这个参数不填的话则显示多久前
  84. */
  85. formatTime(time, format) {
  86. function formatNumber(n) {
  87. n = n.toString()
  88. return n[1] ? n : '0' + n
  89. }
  90. function getDate(time, format) {
  91. const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
  92. const returnArr = []
  93. const date = new Date(time)
  94. returnArr.push(date.getFullYear())
  95. returnArr.push(formatNumber(date.getMonth() + 1))
  96. returnArr.push(formatNumber(date.getDate()))
  97. returnArr.push(formatNumber(date.getHours()))
  98. returnArr.push(formatNumber(date.getMinutes()))
  99. returnArr.push(formatNumber(date.getSeconds()))
  100. for (const i in returnArr) {
  101. format = format.replace(formateArr[i], returnArr[i])
  102. }
  103. return format
  104. }
  105. function getDateDiff(time) {
  106. let r = ''
  107. const ft = new Date(time)
  108. const nt = new Date()
  109. const nd = new Date(nt)
  110. nd.setHours(23)
  111. nd.setMinutes(59)
  112. nd.setSeconds(59)
  113. nd.setMilliseconds(999)
  114. const d = parseInt((nd - ft) / 86400000)
  115. switch (true) {
  116. case d === 0:
  117. const t = parseInt(nt / 1000) - parseInt(ft / 1000)
  118. switch (true) {
  119. case t < 60:
  120. r = '刚刚'
  121. break
  122. case t < 3600:
  123. r = parseInt(t / 60) + '分钟前'
  124. break
  125. default:
  126. r = parseInt(t / 3600) + '小时前'
  127. }
  128. break
  129. case d === 1:
  130. r = '昨天'
  131. break
  132. case d === 2:
  133. r = '前天'
  134. break
  135. case d > 2 && d < 30:
  136. r = d + '天前'
  137. break
  138. default:
  139. r = getDate(time, 'Y-M-D')
  140. }
  141. return r
  142. }
  143. if (!format) {
  144. return getDateDiff(time)
  145. } else {
  146. return getDate(time, format)
  147. }
  148. },
  149. //picker设置月份
  150. editMonth(e) {
  151. let i = e.currentTarget.dataset.index;
  152. const arr = e.currentTarget.dataset.item.split("-");
  153. const year = parseInt(arr[0])
  154. const month = parseInt(arr[1])
  155. const day = parseInt(arr[2])
  156. this.setMonth(year, month,day,i,'',1)
  157. },
  158. //上月切换按钮点击
  159. lastMonth() {
  160. const lastMonth = new Date(this.data.selectDay.year, this.data.selectDay.month - 2)
  161. const year = lastMonth.getFullYear()
  162. const month = lastMonth.getMonth() + 1
  163. this.setMonth(year, month)
  164. },
  165. //下月切换按钮点击
  166. nextMonth() {
  167. const nextMonth = new Date(this.data.selectDay.year, this.data.selectDay.month)
  168. const year = nextMonth.getFullYear()
  169. const month = nextMonth.getMonth() + 1
  170. this.setMonth(year, month)
  171. },
  172. //设置月份
  173. setMonth(setYear, setMonth, setDay,index, sid,bol) {
  174. if (!util.isEmpty(index)) {
  175. this.setData({selectedMonth:index})
  176. }
  177. // if (this.data.selectDay.year !== setYear || this.data.selectDay.month !== setMonth || this.data.selectDay.day !== setDay || this.data.s1Sid != sid) {
  178. const data = {
  179. selectDay: {
  180. year: setYear,
  181. month: setMonth,
  182. day: setDay ? setDay : 0,
  183. dateString: setYear+'-'+setMonth+'-'+(setDay||0)
  184. }
  185. }
  186. if (!setDay) {
  187. data.open = true;
  188. }
  189. this.setData({selectDay:data.selectDay,s1Sid:sid})
  190. this.dateInit(setYear, setMonth)
  191. this.data.selectDay.bol=bol;
  192. this.triggerEvent("change", this.data.selectDay)
  193. // }
  194. },
  195. // //展开收起
  196. // openChange() {
  197. // this.setData({
  198. // open: !this.data.open
  199. // })
  200. // this.triggerEvent("aaa", { a: 0 })
  201. // this.dateInit()
  202. // },
  203. //日历主体的渲染方法
  204. dateInit(setYear = this.data.selectDay.year, setMonth = this.data.selectDay.month) {
  205. console.log(setYear,setMonth)
  206. let dateList = []; //需要遍历的日历数组数据
  207. let now = new Date(setYear, setMonth - 1)//当前月份的1号
  208. let startWeek = now.getDay(); //目标月1号对应的星期
  209. let dayNum = new Date(setYear, setMonth, 0).getDate() //当前月有多少天
  210. let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的天数(包括当前月天数周所涉及的上下月日期)
  211. if (this.data.open) {
  212. //展开状态,需要渲染完整的月份
  213. for (let i = 0; i < forNum; i++) {
  214. const now2 = new Date(now)
  215. let obj = {};
  216. if (i>=startWeek && i<(dayNum+startWeek)) {
  217. now2.setDate(i - startWeek + 1)
  218. let dayStatus=this.matchDay({y:now2.getFullYear(),m:now2.getMonth()+1,d:now2.getDate()})
  219. obj = {
  220. day: now2.getDate(),
  221. month: now2.getMonth() + 1,
  222. year: now2.getFullYear(),
  223. dateString: this.formatTime(now2, "Y-M-D")
  224. };
  225. if (!util.isEmpty(dayStatus)) {
  226. obj.actNum=dayStatus.actNum;
  227. obj.waitActNum=dayStatus.waitActNum;
  228. }
  229. } else {
  230. obj = {}
  231. }
  232. dateList[i] = obj;
  233. }
  234. } else {
  235. //非展开状态,只需要渲染当前周
  236. for (let i = 0; i < 7; i++) {
  237. const now2 = new Date(now)
  238. //当前周的7天
  239. now2.setDate(Math.ceil((this.data.selectDay.day + startWeek) / 7) * 7 - 6 - startWeek + i)
  240. let obj = {};
  241. obj = {
  242. day: now2.getDate(),
  243. month: now2.getMonth() + 1,
  244. year: now2.getFullYear(),
  245. dateString: this.formatTime(now2, "Y-M-D")
  246. };
  247. dateList[i] = obj;
  248. }
  249. }
  250. this.setData({
  251. dateList: dateList
  252. })
  253. },
  254. // 匹配日历-天
  255. matchDay(date){
  256. let obj={};
  257. this.data.calendarInfo.map(el => {
  258. let date1 = el.month.split('-');
  259. if (date.y==date1[0] && date.m==parseInt(date1[1])) {
  260. el.days.forEach(it => {
  261. if (date.d==it.days) {
  262. obj = it;
  263. }
  264. });
  265. }
  266. });
  267. return obj;
  268. },
  269. //一天被点击时
  270. selectChange(e) {
  271. if (util.isEmpty(e.currentTarget.dataset.actnum) && util.isEmpty(e.currentTarget.dataset.waitactnum)) {
  272. return false;
  273. }
  274. const year = e.currentTarget.dataset.year
  275. const month = e.currentTarget.dataset.month
  276. const day = e.currentTarget.dataset.day
  277. const dateString = e.currentTarget.dataset.dateString
  278. const selectDay = {
  279. year: year,
  280. month: month,
  281. day: day,
  282. dateString: dateString
  283. }
  284. if (this.data.selectDay.year !== year || this.data.selectDay.month !== month) {
  285. this.setMonth(year, month, day)
  286. } else if (this.data.selectDay.day !== day) {
  287. this.setData({
  288. selectDay: selectDay
  289. })
  290. this.data.selectDay.bol = 1;
  291. this.triggerEvent("change", this.data.selectDay)
  292. }
  293. }
  294. },
  295. observers: {
  296. spot: function (spot) {
  297. // this.setSpot()
  298. }
  299. }
  300. })