let d = null; const app = getApp(); const { promoteApi, DFSImg, $themeToLink } = app; // 倒计时方法 if (typeof xcsoft == "undefined") var xcsoft = new function() {}(); xcsoft.countdown = function(a, b, c) { d = new Object(); var e = 0; if (typeof a == "object") { var f = parseInt(new Date().getTime() / 1000); var g = a.startTime ? parseInt(a.startTime) : 0; g = g == 0 ? f : g; var h = a.endTime; var x = g - f; d.decimal = parseInt(a.msec ? a.msec : 0); } else { var f = 0; var x = 0; d.decimal = 0; var h = a; var i = h.toString(); if (i.indexOf(".") > 0) { d.decimal = i.split(".")[1]; if (d.decimal > 3) { d.decimal = 3; } } } d.time = h; d.finish = false; if (isNaN(h)) { var j = h.substring(0, 19); j = j.replace(/-/g, "/"); d.time = new Date(j).getTime() / 1000; } var k = d.decimal == 0 ? 100 : 100; d.day = 0; d.hour = 0; d.minute = 0; d.second = 0; d.t = setInterval(function() { console.log('倒计时') e = new Date().getTime(); f = parseInt(e / 1000 + x); var l = d.time - f; if (l <= 0) { e = 0; l = 0; } if (l >= 0) { d = xcsoft.getTimeObject(d, l); } if (d.decimal == 1) { d.msecZero = d.msec = parseInt(10 - (e % 1000) / 100); if (d.msec == 10 || l == 0) { d.msecZero = d.msec = 0; } } else if (d.decimal == 2) { d.msecZero = d.msec = parseInt(100 - (e % 1000) / 10); if (d.msec < 10) { d.msecZero = "0" + d.msec; } else if (d.msec == 100 || l == 0) { d.msec = 0; d.msecZero = "0" + d.msec; } } else { d.msecZero = d.msec = parseInt(1000 - e % 1000); if (d.msec < 10) { d.msecZero = "00" + d.msec; } else if (d.msec < 100) { d.msecZero = "0" + d.msec; } else if (d.msec == 1000 || l == 0) { d.msec = 0; d.msecZero = "00" + d.msec; } } if (b) { b(d); } if (l <= 0 || d.stop == true) { clearInterval(d.t); d.finish = true; if (c) c(d); } }, k); }; xcsoft.getTimeObject = function(a, l) { var b = 60; var c = b * b; var d = 24 * c; a.days = Math.floor(l / d); a.year = Math.floor(a.days / 365); a.day = Math.floor(a.days % 365); a.hour = Math.floor((l % d) / c); a.minute = Math.floor((l - (a.days * d + a.hour * c)) / b); a.second = Math.floor(l % b); a.dayZero = a.day < 10 ? "0" + a.day : a.day; a.daysZero = a.days < 10 ? "0" + a.days : a.days; a.hourZero = a.hour < 10 ? "0" + a.hour : a.hour; a.minuteZero = a.minute < 10 ? "0" + a.minute : a.minute; a.secondZero = a.second < 10 ? "0" + a.second : a.second; return a; }; const componentOptions = { // 组件选项 options: { multipleSlots: true }, behaviors: [], properties: { datas: { type: Object }, k: { type: String } }, // 组件数据 data: { loaded: false, isContain: false, isPageHidden: false, // 页面是否处于隐藏状态 timeData: {}, isLoading: true, deleteFlag: false, //活动未禁用 newTime: new Date().getTime(), timer: null, hideAll: false, days: 0, hours: 0, minute: 0, second: 0, goodsList: [], selectActivityValue:"", }, // 数据监听器 observers: {}, // 组件方法 methods: { init() { let { goodsList, activityCode, selectActivityValue, dimension } = this.data.datas.componentData; let { deleteFlag, endTimeData } = this.data; let _this = this; if (activityCode == "") { return; } let query = { singleItemActivityId: activityCode, whetherShowSoldOutGoods: 1 }; promoteApi.getSingleItemActivityV2(query).then(res => { if (res.data.code == 200) { let copyData = res.data.data; deleteFlag = copyData.activityStatus != 2 ? true : false; if (dimension == 0 && copyData.goodsItems.length == 0) { deleteFlag = true; } endTimeData = new Date(copyData.endTime.replace(/-/g, "/")).getTime() - new Date().getTime(); console.log(endTimeData, "----------------------150"); this.setData({ endTime: endTimeData }); this.setData({ deleteFlag }); console.log("------------155"); // setTimeout(() => { // xcsoft.countdown(_this.data.datas.componentData.endTime, function (time) { // console.log("--------------157") // let { days, hours, minute, second } = _this.data; // days = time.dayZero; // hours = time.hourZero; // minute = time.minuteZero; // second = time.secondZero; // // _this.setData({ days, hours, minute, second }); // }, function(time) { // //倒计时结束后的操作 // }); // setTimeout(() => { // this.setData({ isLoading: true }); // }, 100); // }, 100); this.setData({ isLoading: false }); copyData.goodsItems.forEach((item, index) => { item.productImgUrl = DFSImg(item.imgUrl); item.productImgUrl1 = DFSImg(item.imgUrl); item.minPrice = parseFloat(item.discountPrice); item.maxPrice = parseFloat(item.salePrice); }); _this.setData({ goodsList: copyData.goodsItems, selectActivityValue: copyData.singleItemActivityName }); console.log(_this.data.selectActivityValue, "--------------197"); } }); }, initArticityList() { let ids = []; let { actList } = this.data.datas.componentData; let { isLoading } = this.data; if (actList.length > 0) { ids = actList.map(item => { return item.id; }); } if (ids.length == 0) { this.setData({ isLoading: false }); return; } promoteApi.query_single_item_activity_list_by_ids(ids).then(res => { if (res.data.code == 200) { let list = res.data.data || []; if (list.length > 0) { let hideNum = 0; list.forEach((item, index) => { if ( new Date(item.endTime.replace(/-/g, "/")).getTime() - new Date().getTime() <= 0 ) { // item.isEnd = true; hideNum += 1; } else { // item.isEnd = false; } item.startTimeTimeStamp = new Date(item.startTime.replace(/-/g, "/")).getTime() - new Date().getTime(); item.endTimeTimeStamp = new Date(item.endTime.replace(/-/g, "/")).getTime() - new Date().getTime(); item.singleItemActivityCoverImage = DFSImg( item.singleItemActivityCoverImage, 400, 400, 1 ); item.isStart = new Date(item.startTime.replace(/-/g, "/")).getTime() > new Date().getTime() ? true : false; }); let newList = list.filter(item => { return ( new Date(item.endTime.replace(/-/g, "/")).getTime() - new Date().getTime() > 0 ); }); this.setData({ "datas.componentData.actList": newList }); if (hideNum == actList.length) { this.setData({ hideAll: true }); } this.setData({ isLoading: false }); } } }); }, getScroll() { this.getEleInfo(() => { this.getData(); }); }, // 获取当前组件位置信息 getEleInfo(cb) { if (this.loaded || this.isContain) return; this.isContain = true; let { windowHeight } = wx.getSystemInfoSync(); let query = wx.createSelectorQuery().in(this); query .select(".time-limited-discount") .boundingClientRect(rect => { if (rect.top < windowHeight && rect.top + rect.height > 0) { cb && cb(); } this.isContain = false; }) .exec(); }, onChange(e) { this.setData({ timeData: e.detail }); }, onclickProduct(e) { console.log( e.currentTarget.dataset.items, "-------------------------226" ); let { shopName, productId, productName, terminalProductId, terminalGoodsMixId } = e.currentTarget.dataset.items; app.trackCpn(e, this.data.datas.componentName, { shopName, productId, productName, terminalProductId, terminalGoodsMixId }) $themeToLink({ type: 1, link: `/goods/${productId}?limitedLime=true&endTime=${ this.data.datas.componentData.endTime }&terminalProductId=${terminalProductId}&terminalGoodsMixId=${terminalGoodsMixId}` }); }, onclickOrder(e) { let { terminalGoodsMixId, terminalGoodsId, goodsId } = e.currentTarget.dataset.items; app.trackCpn(e, this.data.datas.componentName, { terminalGoodsMixId, terminalGoodsId, goodsId }) $themeToLink({ type: 1, link: `/order/orderConfirm?productGoodsMixId=${terminalGoodsMixId}&productGoodsId=${terminalGoodsId}&goodsId=${goodsId}&qty=1&goodsString=null` }); }, getData() { if (this.loaded) return; this.loaded = true; let { dimension } = this.data.datas.componentData; if (dimension == 0) { this.init(); } else if (dimension == 1) { this.initArticityList(); } } }, // 组件生命周期 lifetimes: { created() {}, ready() { this.loaded = false; this.isContain = false; this.getEleInfo(() => { this.getData(); }); }, moved() {}, detached() {} }, definitionFilter() {}, // 页面生命周期 pageLifetimes: { // 页面被展示 show() { this.loaded = false; this.isContain = false; this.getEleInfo(() => { this.getData(); }); const { isPageHidden } = this.data; // show事件发生前,页面不是处于隐藏状态时 if (!isPageHidden) { return; } // 重新执行定时器等操作 }, // 页面被隐藏 hide() { this.setData({ isPageHidden: true }); // 清除定时器等操作 }, // 页面尺寸变化时 resize() {} } }; Component(componentOptions)