onLoad: function(options) { var that = this wx.setNavigationBarTitle({ title: options.title, }) this.setData({ title: options.title, menuIdx: options.menuIdx, }) console.log(this.data.title) wx.showLoading({ title: '加载中', }) let uid = wx.getStorageSync('uid') || ""; this.setData({ uid: uid, }) this.getList() this.timer = this.timer || setInterval(this.refreshListTime.bind(this),1000); }, getList() { let that = this let url = '' let menuIdx = "" let obj = {} if (this.data.title == '我的拼团') { if (this.data.menuIdx > 0) { menuIdx = parseInt(that.data.menuIdx) } else { menuIdx = "" } url = '/api/sg/list.html' obj = { uid: this.data.uid, status: menuIdx, } http(url, obj).then(res => { wx.hideLoading(); if (res.code == 1) { let list = res.data; this.setData({ list: list, listLength: list.length, }); this.refreshListTime(); } }) } }, // 计算倒计时 // 获取当前时间,同时得到活动结束时间数组 refreshListTime(){ let newTime = new Date().getTime(); newTime = parseInt(newTime / 1000); let list = this.data.list; list.forEach((order) => { // 对结束时间进行处理渲染到页面 // order.overtime = 1586096462 let endTime = order.overtime // let endTime = new Date(o).getTime(); let objects = {}; // 如果活动未结束,对时间进行处理 let time = endTime - newTime; if (time > 0 && order.status == 1) { // 获取天、时、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hour = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); hour = hour + day * 24 hour = formatNumber(hour) min = formatNumber(min) sec = formatNumber(sec) order.hour = hour; order.min = min; order.sec = sec; order.pstatus = true; } else { // 活动已结束,全部设置为'00' order.hour = '00'; order.min = '00'; order.sec = '00'; order.pstatus = false; } }); this.setData({ list: list, }); },
const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n }