// pages/specialDiscount/specialDiscount.js var http = require('../../utils/http.js'); var util = require('../../utils/util.js'); Page({ /** * 页面的初始数据 */ data: { discountList: [], //优惠活动列表 current: 1, // 当前页数 pages: 1, // 总页数 endTimeList: [], //结束时间 timer: '' }, // 获取优惠活动列表 getDiscountPage (cur) { wx.showLoading(); var ths = this; http.request({ url: "/marking/discount/getDiscountList", data: { current: cur, //当前页 size: 10 }, method: "GET", callBack: (res) => { var list = []; if (res.current == 1) { list = res.records; } else { list = ths.data.discountList; Array.prototype.push.apply(list, res.records); } var endTimeList = []; list.forEach(item => { endTimeList.push({ endTime: item.endTime }); }) this.setData({ discountList: list, pages: res.pages, endTimeList: endTimeList, current: res.current }) this.countTime() wx.hideLoading() } }) }, // 倒计时 countTime () { var endTimeList = this.data.endTimeList endTimeList.forEach((item, index) => { // 获取当前时间 let date = new Date() let now = date.getTime() // 设置截止时间 let end = util.dateToTimestamp(item.endTime) // 时间差 let leftTime = end - now // 定义变量 d,h,m,s保存倒计时的时间 if (leftTime >= 0) { // 天 item.day = Math.floor(leftTime / 1000 / 60 / 60 / 24) // 时 let h = Math.floor(leftTime / 1000 / 60 / 60 % 24) item.hour = h < 10 ? '0' + h : h // 分 let m = Math.floor(leftTime / 1000 / 60 % 60) item.min = m < 10 ? '0' + m : m // 秒 let s = Math.floor(leftTime / 1000 % 60) item.second = s < 10 ? '0' + s : s } else { item.day = 0 item.hour = '00' item.min = '00' item.second = '00' } }) this.setData({ endTimeList: endTimeList }) // 递归每秒调用countTime方法,显示动态时间效果, this.setData({ timer: setTimeout(this.countTime, 1000) }) }, //跳转活动详情 toDiscountDetail (e) { var discountId = e.currentTarget.dataset.discountid wx.navigateTo({ url: '../discountDetail/discountDetail?discountId=' + discountId }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getDiscountPage(1) //默认加载第一页 }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { clearTimeout(this.data.timer) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { if (this.data.current < this.data.pages) { this.getDiscountPage(this.data.current + 1); } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })