calendar.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. const app = getApp();
  2. const util = require('../../../utils/util.js');
  3. Page({
  4. data: {
  5. activeData:[],//活动列表
  6. // 新版
  7. selectDay:{
  8. year:new Date().getFullYear(),
  9. month:new Date().getMonth() + 1,
  10. day:new Date().getDate()
  11. },
  12. today: {},//今天
  13. todaySecond: new Date().getTime(),//今天的秒
  14. haveActDays:[],//当日有活动的天
  15. imgPath: util.config.imgPath,
  16. },
  17. onLoad: function () {
  18. let date = new Date()
  19. this.setData({
  20. today: this.formatDate()
  21. });
  22. this.generateAllDays(date.getFullYear(), date.getMonth() + 1)
  23. this.setNavigationBarTitle()
  24. },
  25. setNavigationBarTitle(){
  26. wx.setNavigationBarTitle({
  27. title: app.globalData.city.city + '活动日历',
  28. })
  29. },
  30. digit: function (n) {
  31. if (n == null || n === "" || n == undefined) {
  32. return "";
  33. } else {
  34. return parseInt(n)>=10?n:'0'+n;
  35. }
  36. },
  37. //生成日期格式:2019-5-7
  38. formatDate: function (date = new Date()) {
  39. // 年月日
  40. var year = date.getFullYear()
  41. var month = date.getMonth() + 1
  42. var day = date.getDate()
  43. // 返回值
  44. let obj={year,month,day,dateStr:[year, this.digit(month), this.digit(day)].join('-')
  45. }
  46. return obj;
  47. },
  48. //生成当前需要显示的全部日期
  49. generateAllDays(year, month) {
  50. let that =this,detaultDay=-1,thisMonth = this.currentMonthDays(year, month)
  51. util.ajax({
  52. func: "article/calendar/getCalendarList",
  53. data: { "starDate": thisMonth[0].date, "endDate": thisMonth[thisMonth.length-1].date },
  54. load:false
  55. }, function (res) {
  56. if (res.code == 0) {
  57. let data = res.data;
  58. if (!util.isObjEmpty(data)&&data.length>0) {
  59. for (let i = 0; i < data.length; i++) {
  60. const el = data[i];
  61. let sec=new Date(el.date).getTime();
  62. if (sec>=that.data.todaySecond) {
  63. detaultDay=el.date.split('-')[2];
  64. that.getActiveDetail(el.date);
  65. break;
  66. }
  67. }
  68. }
  69. that.setData({haveActDays:data,'selectDay.day':parseInt(detaultDay)});
  70. that.dateInit()
  71. }else{
  72. util.showTips(res.reason);
  73. }
  74. })
  75. },
  76. //生成本月的值
  77. currentMonthDays(year, month) {
  78. const numOfDays = this.getNumOfDays(year, month)
  79. return this.generateDays(year, month, numOfDays)
  80. },
  81. //生成本月的上,或者中,或者下
  82. generateDays(year, month, daysNum, option = {startNum: 1, notCurrent: false}) {
  83. const weekMap = ['一', '二', '三', '四', '五', '六', '日']
  84. let days = []
  85. for (let i = option.startNum; i <= daysNum; i++) {
  86. let week = weekMap[new Date(year, month - 1, i).getUTCDay()]
  87. let day = i;
  88. days.push({
  89. date: `${year}-${month}-${day}`,
  90. event: false,
  91. day,
  92. week,
  93. month,
  94. year,
  95. dateSecond: new Date(`${year}/${month}/${day}`).getTime()
  96. })
  97. }
  98. return days
  99. },
  100. getNumOfDays(year, month, day = 0) {
  101. return new Date(year, month, day).getDate()
  102. },
  103. getActiveDetail(date){
  104. let that = this;
  105. util.ajax({
  106. func: "article/calendar/getArticleCalendarList",
  107. data: {date},
  108. load:false
  109. }, function (res) {
  110. if (res.code == 0) {
  111. res.data.map(el => {
  112. if (!util.isEmpty(el.discount)) {
  113. let arr =[];
  114. if (el.discount.indexOf('+')==-1) {
  115. arr.push(el.discount);
  116. } else {
  117. arr=el.discount.split('+');
  118. }
  119. el.discount=arr;
  120. }
  121. });
  122. let activeData = res.data;
  123. that.setData({ activeData})
  124. } else {
  125. util.showTips(res.reason);
  126. }
  127. })
  128. },
  129. onShareAppMessage() {
  130. return {
  131. title: '活动日历,按日历查活动',
  132. path: '/pages/public/calendar/calendar'
  133. }
  134. },
  135. /**
  136. * 时间戳转化为年 月 日 时 分 秒
  137. * time: 需要被格式化的时间,可以被new Date()解析即可
  138. * format:格式化之后返回的格式,年月日时分秒分别为Y, M, D, h, m, s,这个参数不填的话则显示多久前
  139. */
  140. formatTime(time, format) {
  141. function formatNumber(n) {
  142. n = n.toString()
  143. return n[1] ? n : '0' + n
  144. }
  145. function getDate(time, format) {
  146. const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
  147. const returnArr = []
  148. const date = new Date(time)
  149. returnArr.push(date.getFullYear())
  150. returnArr.push(formatNumber(date.getMonth() + 1))
  151. returnArr.push(formatNumber(date.getDate()))
  152. returnArr.push(formatNumber(date.getHours()))
  153. returnArr.push(formatNumber(date.getMinutes()))
  154. returnArr.push(formatNumber(date.getSeconds()))
  155. for (const i in returnArr) {
  156. format = format.replace(formateArr[i], returnArr[i])
  157. }
  158. return format
  159. }
  160. function getDateDiff(time) {
  161. let r = ''
  162. const ft = new Date(time)
  163. const nt = new Date()
  164. const nd = new Date(nt)
  165. nd.setHours(23)
  166. nd.setMinutes(59)
  167. nd.setSeconds(59)
  168. nd.setMilliseconds(999)
  169. const d = parseInt((nd - ft) / 86400000)
  170. switch (true) {
  171. case d === 0:
  172. const t = parseInt(nt / 1000) - parseInt(ft / 1000)
  173. switch (true) {
  174. case t < 60:
  175. r = '刚刚'
  176. break
  177. case t < 3600:
  178. r = parseInt(t / 60) + '分钟前'
  179. break
  180. default:
  181. r = parseInt(t / 3600) + '小时前'
  182. }
  183. break
  184. case d === 1:
  185. r = '昨天'
  186. break
  187. case d === 2:
  188. r = '前天'
  189. break
  190. case d > 2 && d < 30:
  191. r = d + '天前'
  192. break
  193. default:
  194. r = getDate(time, 'Y-M-D')
  195. }
  196. return r
  197. }
  198. if (!format) {
  199. return getDateDiff(time)
  200. } else {
  201. return getDate(time, format)
  202. }
  203. },
  204. //上月切换按钮点击
  205. lastMonth() {
  206. const lastMonth = new Date(this.data.selectDay.year, this.data.selectDay.month - 2)
  207. const year = lastMonth.getFullYear()
  208. const month = lastMonth.getMonth() + 1
  209. this.setData({'selectDay.year':year,'selectDay.month':month,activeData:[]})
  210. this.generateAllDays(year, month)
  211. },
  212. //下月切换按钮点击
  213. nextMonth() {
  214. const nextMonth = new Date(this.data.selectDay.year, this.data.selectDay.month)
  215. const year = nextMonth.getFullYear()
  216. const month = nextMonth.getMonth() + 1
  217. this.setData({'selectDay.year':year,'selectDay.month':month,activeData:[]})
  218. this.generateAllDays(year, month)
  219. },
  220. //设置月份
  221. setMonth(setYear, setMonth, setDay,index, sid,bol) {
  222. if (!util.isEmpty(index)) {
  223. this.setData({selectedMonth:index})
  224. }
  225. const data = {
  226. selectDay: {
  227. year: setYear,
  228. month: setMonth,
  229. day: setDay ? setDay : 0,
  230. dateString: setYear+'-'+setMonth+'-'+(setDay||0)
  231. }
  232. }
  233. if (!setDay) {
  234. data.open = true;
  235. }
  236. this.setData({selectDay:data.selectDay,s1Sid:sid})
  237. this.dateInit(setYear, setMonth)
  238. this.data.selectDay.bol=bol;
  239. this.triggerEvent("change", this.data.selectDay)
  240. },
  241. //日历主体的渲染方法
  242. dateInit(setYear = this.data.selectDay.year, setMonth = this.data.selectDay.month) {
  243. let dateList = []; //需要遍历的日历数组数据
  244. let now = new Date(setYear, setMonth - 1)//当前月份的1号
  245. let startWeek = now.getDay(); //目标月1号对应的星期
  246. let dayNum = new Date(setYear, setMonth, 0).getDate() //当前月有多少天
  247. let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的天数(包括当前月天数周所涉及的上下月日期)
  248. //展开状态,需要渲染完整的月份
  249. for (let i = 0; i < forNum; i++) {
  250. const now2 = new Date(now)
  251. let obj = {};
  252. if (i>=startWeek && i<(dayNum+startWeek)) {
  253. now2.setDate(i - startWeek + 1)
  254. let dayStatus=this.matchDay({y:now2.getFullYear(),m:now2.getMonth()+1,d:now2.getDate()})
  255. obj = {
  256. day: now2.getDate(),
  257. month: now2.getMonth() + 1,
  258. year: now2.getFullYear(),
  259. dateString: this.formatTime(now2, "Y-M-D"),
  260. dateSecond:new Date(this.formatTime(now2, "Y-M-D")).getTime()
  261. };
  262. if (!util.isEmpty(dayStatus)&&dayStatus>0) {
  263. obj.count=dayStatus;
  264. }
  265. } else {
  266. obj = {}
  267. }
  268. dateList[i] = obj;
  269. }
  270. this.setData({
  271. dateList: dateList
  272. })
  273. },
  274. // 匹配日历-天
  275. matchDay(date){
  276. let count=0;
  277. if (!util.isObjEmpty(this.data.haveActDays)) {
  278. this.data.haveActDays.map(el => {
  279. let date1 = el.date.split('-');
  280. if (date.y==date1[0] && date.m==parseInt(date1[1])&&date.d==parseInt(date1[2])) {
  281. count=el.count;
  282. }
  283. });
  284. }
  285. return count;
  286. },
  287. //一天被点击时
  288. selectChange(e) {
  289. const year = e.currentTarget.dataset.year
  290. const month = e.currentTarget.dataset.month
  291. const day = e.currentTarget.dataset.day
  292. const dateString = e.currentTarget.dataset.dateString
  293. const selectDay = {
  294. year: year,
  295. month: month,
  296. day: day,
  297. dateString: dateString
  298. }
  299. if (this.data.selectDay.year == year && this.data.selectDay.month == month && this.data.selectDay.day == day) {
  300. return false;
  301. }else{
  302. this.getActiveDetail(dateString)
  303. }
  304. this.setData({selectDay})
  305. }
  306. })