// packageA/mycomponent/goodsComponent/timeLimit/timeLimit.js const app = getApp(); Component({ /** * 组件的属性列表 */ properties: { goodsInfo: { type: null, }, goodsType: { type: null, }, popPrice: { type: null, }, gooddatas: { type: null }, start_time: { type: null }, end_time: { type: null }, isendTime: { type: null }, group_price: { type: String, value: '0' }, }, observers: { goodsInfo: function (arr) { this.startBuy(arr); }, 'isendTime,gooddatas,start_time,end_time': function (isendTime, gooddatas, start_time, end_time) { // console.log(isendTime, gooddatas.status, start_time, 'time') } }, lifetimes: { attached(e) { let start_time = new Date().getTime() / 1000; this.setData({ nowTime: start_time }); let language = wx.getStorageSync("langIndex"); this.setData({ language: language.en, }); //* 限时购自定义名称 if(this.data.goodsInfo.has_one_goods_limit_buy&&this.data.goodsInfo.has_one_goods_limit_buy.status&&this.data.goodsInfo.has_one_goods_limit_buy.display_name){ this.setData({ limitedTimeCustomName:this.data.goodsInfo.has_one_goods_limit_buy.display_name }); } } }, /** * 组件的初始数据 */ data: { isBuy: false, isTime: false, isBegTime: false, begTimeStr: "", //计算后的倒计时 countDownObj: { day: "00", hou: "00", min: "00", sec: "00", }, //限时购开始时间 beginTime: 0, //显示购结束时间 endTime: 0, group_price: "", nowTime:"", timestamp:Date.parse(new Date()) / 1000, limitedTimeCustomName:"限时购" //* 自定义限时购名称 }, /** * 组件的方法列表 */ methods: { startBuy(data) { console.log(this.data.goodsType,'36'); if(this.data.goodsType == 'grabGroup'){ if(!app._isTextEmpty(data.start_time)){ this.data.beginTime = this.data.start_time; this.data.endTime = this.data.end_time; //判断倒计时模块是否开启 this._timeCompare(this.data.beginTime); } }else{ if (!app._isTextEmpty(data.has_one_goods_limit_buy)) { this.setData({ isBuy: data.has_one_goods_limit_buy.status == 1 ? true : false, }); this.triggerEvent("onBuy", { isBuy: this.data.isBuy }); this.data.beginTime = data.has_one_goods_limit_buy.start_time; this.data.endTime = data.has_one_goods_limit_buy.end_time * 1000; //判断倒计时模块是否开启 this._timeCompare(this.data.beginTime); } } }, //获取时间-限时购 _timeCompare(begin) { let beginTime = new Date(parseInt(begin) * 1000); let now = new Date(); if (now >= beginTime) { this.setData({ isTime: true, }); //开启倒计时 this._countDown(); } else { this.setData({ isTime: false, isBegTime: true, begTimeStr: begin * 1000, }); //开启距离开始倒计时 this.beginDown(); } }, //倒计时 _countDown: function () { let newTime = new Date().getTime(); let endTime = this.data.endTime; 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), }; } else { //活动已结束,全部设置为'00' obj = { day: "00", hou: "00", min: "00", sec: "00", }; } this.setData({ countDownObj: obj, }); if (endTime - newTime < 0) { this.setData({ countDownObj: { day: "00", hou: "00", min: "00", sec: "00", }, }); } else { setTimeout(this._countDown.bind(this), 1000); } }, //倒计时 beginDown: function () { let newTime = new Date().getTime(); let endTime = this.data.begTimeStr; 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), }; } else { //活动已结束,全部设置为'00' this.setData({ isTime: true, isBegTime: false, }); obj = { day: "00", hou: "00", min: "00", sec: "00", }; } this.setData({ beginDownObj: obj, }); if (endTime - newTime < 0) { this.setData({ isTime: true, isBegTime: false, }); this.setData({ beginDownObj: { day: "00", hou: "00", min: "00", sec: "00", }, }); } else { setTimeout(this.beginDown.bind(this), 1000); } }, //小于10的格式化函数 _timeFormat(param) { //小于10的格式化函数 return param < 10 ? "0" + param : param; }, }, });