// pages/snapUpList/snapUpList.js const app = getApp() var http = require('../../utils/http.js'); var config = require('../../utils/config.js'); var util = require('../../utils/util.js'); Page({ /** * 页面的初始数据 */ data: { seckillList: [], // 秒杀商品列表 countDownList: [], // 秒杀倒计时列表 countDownEndTimeList:[], timer: '', current: 1, }, /** * 获取秒杀商品列表 */ getsklist() { http.request({ url: "/seckill/pageProd", method: "GET", data: { current: this.data.current, size: 5, lat: wx.getStorageSync('LATITUDE'), lon: wx.getStorageSync('LONGITUDE'), distance: wx.getStorageSync('DISTANCE')||0 }, callBack: (res) => { let result = [] result = res.records.filter(item => util.dateToTimestamp(item.endTime) > new Date().getTime()) this.setData({ seckillList: res.current == 1 ? result : this.data.seckillList.concat(result), pages: res.pages }) let endTimeList = []; // 倒计时数组 for (let i = 0; i < this.data.seckillList.length; i++) { var objs = {} objs.eTime = this.data.seckillList[i].endTime objs.sTime = this.data.seckillList[i].startTime objs.countType = null //1表示秒杀活动正在进行,0表示秒杀活动未开始 endTimeList.push(objs) } this.setData({ countDownEndTimeList: endTimeList }) this.countdown() } }) }, countdown() { // 获取当前时间,同时得到活动结束时间数组 let newTime = new Date().getTime(); let endTimeList = this.data.countDownEndTimeList; let countDownArr = []; // 对结束时间进行处理渲染到页面 endTimeList.forEach(o => { if (newTime - util.dateToTimestamp(o.sTime) > 0) { let endTime = util.dateToTimestamp(o.eTime); let obj = null; // 如果活动未结束,对时间进行处理 if (endTime - newTime > 0) { let time = (endTime - newTime) / 1000; // 获取天、时、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hou = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: `${this.timeFormat(hou)}`, min: `${this.timeFormat(min)}`, sec: `${this.timeFormat(sec)}`, type: 1 // 表示秒杀正在进行 } } // 活动已结束 else { obj = { day: '00', hou: '00', min: '00', sec: '00' } } countDownArr.push(obj); } // 活动未开始 else { let startTime = util.dateToTimestamp(o.sTime); let time = (startTime - newTime) / 1000; let obj = null; // 获取天、时、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hou = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: `${this.timeFormat(hou)}`, min: `${this.timeFormat(min)}`, sec: `${this.timeFormat(sec)}`, type: 0 // 表示秒杀还没开始 } countDownArr.push(obj); } }) // 渲染,然后每隔一秒执行一次倒计时函数 this.setData({ countDownList: countDownArr, timer: setTimeout(this.countdown, 1000) }) }, //小于10的格式化函数 timeFormat(times) { return Number(times) < 10 ? '0' + times : times; }, //跳转秒杀商品详情页 toSeckillDetaile: function(e) { var seckillId = e.currentTarget.dataset.seckillid; wx.navigateTo({ url: "../snapUpDetail/snapUpDetail?seckillid=" + seckillId, }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { this.getsklist(); //秒杀商品列表 }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { clearTimeout(this.data.timer) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { if(this.data.current < this.data.pages) { this.setData({ current: this.data.current + 1 }) this.getsklist() } }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })