index.js 11.8 KB
Newer Older
李嘉林 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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() {
程默 committed
42
    console.log('倒计时')
李嘉林 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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
程默 committed
113
    },
李嘉林 committed
114 115
    k: {
      type: String
李嘉林 committed
116 117 118 119
    }
  },
  // 组件数据
  data: {
120
    isStart: true, // 活动是否开始
李嘉林 committed
121 122
    loaded: false,
    isContain: false,
李嘉林 committed
123 124 125 126 127 128 129 130 131 132
    isPageHidden: false, // 页面是否处于隐藏状态
    timeData: {},
    isLoading: true,
    deleteFlag: false, //活动未禁用
    newTime: new Date().getTime(),
    timer: null,
    hideAll: false,
    days: 0,
    hours: 0,
    minute: 0,
李嘉林 committed
133
    second: 0,
李嘉林 committed
134 135
    goodsList: [],
    selectActivityValue:"",
136
    activityInfo: {}, // 活动信息
李嘉林 committed
137 138 139 140 141 142
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {
李嘉林 committed
143 144 145 146 147 148
      let {
        goodsList,
        activityCode,
        selectActivityValue,
        dimension
      } = this.data.datas.componentData;
149
      let { deleteFlag, endTimeData, isStart } = this.data;
李嘉林 committed
150
      let _this = this;
李嘉林 committed
151 152 153 154 155 156 157 158 159 160
      if (activityCode == "") {
        return;
      }
      let query = {
        singleItemActivityId: activityCode,
        whetherShowSoldOutGoods: 1
      };
      promoteApi.getSingleItemActivityV2(query).then(res => {
        if (res.data.code == 200) {
          let copyData = res.data.data;
161
          this.setData({ activityInfo: res.data.data });
162 163
          // 未开始和进行中的活动
          deleteFlag = ![1, 2].includes(+copyData.activityStatus);
李嘉林 committed
164 165 166
          if (dimension == 0 && copyData.goodsItems.length == 0) {
            deleteFlag = true;
          }
167 168 169 170 171 172 173 174
          const now = new Date().getTime();
          if (new Date(copyData.startTime.replace(/-/g, "/")).getTime() > now) {
            isStart = false;
            endTimeData = new Date(copyData.startTime.replace(/-/g, "/")).getTime() - now;
          } else {
            isStart = true;
            endTimeData = new Date(copyData.endTime.replace(/-/g, "/")).getTime() - now;
          }
李嘉林 committed
175
          this.setData({ endTime: endTimeData });
176
          this.setData({ isStart });
李嘉林 committed
177
          this.setData({ deleteFlag });
李嘉林 committed
178
          console.log("------------155");
李嘉林 committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
          // 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);
李嘉林 committed
195 196 197
          if (copyData.goodsItems && copyData.goodsItems.length > 0) {
            this.setData({ isLoading: false });
          }
李嘉林 committed
198 199
          copyData.goodsItems.forEach((item, index) => {
            item.productImgUrl = DFSImg(item.imgUrl);
李嘉林 committed
200
            item.productImgUrl1 = DFSImg(item.imgUrl);
李嘉林 committed
201 202 203
            item.minPrice = parseFloat(item.discountPrice);
            item.maxPrice = parseFloat(item.salePrice);
          });
李嘉林 committed
204
          _this.setData({
李嘉林 committed
205 206 207
            goodsList: copyData.goodsItems,
            selectActivityValue: copyData.singleItemActivityName
          });
李嘉林 committed
208
          console.log(_this.data.selectActivityValue, "--------------197");
李嘉林 committed
209 210 211 212 213 214 215 216 217 218 219 220 221
        }
      });
    },
    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) {
李嘉林 committed
222
        this.setData({ isLoading: false });
李嘉林 committed
223 224 225 226
        return;
      }
      promoteApi.query_single_item_activity_list_by_ids(ids).then(res => {
        if (res.data.code == 200) {
李嘉林 committed
227
          let list = res.data.data || [];
李嘉林 committed
228 229 230
          if (list.length > 0) {
            let hideNum = 0;
            list.forEach((item, index) => {
李嘉林 committed
231 232 233 234 235
              if (
                new Date(item.endTime.replace(/-/g, "/")).getTime() -
                  new Date().getTime() <=
                0
              ) {
236
                // item.isEnd = true;
李嘉林 committed
237 238
                hideNum += 1;
              } else {
239
                // item.isEnd = false;
李嘉林 committed
240
              }
李嘉林 committed
241 242 243 244 245 246 247
              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(
李嘉林 committed
248 249 250 251
                item.singleItemActivityCoverImage,
                400,
                400,
                1
李嘉林 committed
252 253 254 255 256 257 258
              );
              item.isStart =
                new Date(item.startTime.replace(/-/g, "/")).getTime() >
                new Date().getTime()
                  ? true
                  : false;
            });
李嘉林 committed
259 260 261 262 263 264 265
            let newList = list.filter(item => {
              return (
                new Date(item.endTime.replace(/-/g, "/")).getTime() -
                  new Date().getTime() >
                0
              );
            });
266
            this.setData({ "datas.componentData.actList": newList });
李嘉林 committed
267
            if (hideNum == actList.length) {
李嘉林 committed
268
              this.setData({ hideAll: true });
李嘉林 committed
269
            }
李嘉林 committed
270
            this.setData({ isLoading: false });
李嘉林 committed
271 272 273 274
          }
        }
      });
    },
李嘉林 committed
275
    getScroll() {
276
      return;
277
      this.getData();
李嘉林 committed
278
      this.getEleInfo(() => {
程默 committed
279 280 281
      });
    },
    // 获取当前组件位置信息
李嘉林 committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295
    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();
程默 committed
296
    },
李嘉林 committed
297
    onChange(e) {
李嘉林 committed
298 299 300 301 302 303 304
      let { days, hours, minutes, seconds } = e.detail;
      let detail = {
        days: days<10?'0'+days:days,
        hours: hours<10?'0'+hours:hours,
        minutes: minutes<10?'0'+minutes:minutes,
        seconds: seconds<10?'0'+seconds:seconds,
      }
305 306 307 308 309 310 311 312
      // 活动刚刚开始
      const { isStart, activityInfo } = this.data;
      if (days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0 && !isStart) {
          this.setData({ isStart: true });
          this.setData({
            endTime: new Date(activityInfo.endTime.replace(/-/g, "/")).getTime() - (new Date().getTime())
          });
      }
李嘉林 committed
313
      this.setData({ timeData: detail });
李嘉林 committed
314 315
    },
    onclickProduct(e) {
李嘉林 committed
316 317 318 319 320
      console.log(
        e.currentTarget.dataset.items,
        "-------------------------226"
      );
      let {
侯体倬 committed
321 322 323
        shopName,
        productId,
        productName,
李嘉林 committed
324
        terminalProductId,
侯体倬 committed
325
        terminalGoodsMixId
李嘉林 committed
326
      } = e.currentTarget.dataset.items;
侯体倬 committed
327 328 329 330 331 332 333
      app.trackCpn(e, this.data.datas.componentName, {
        shopName,
        productId,
        productName,
        terminalProductId,
        terminalGoodsMixId
      })
李嘉林 committed
334 335 336 337
      $themeToLink({
        type: 1,
        link: `/goods/${productId}?limitedLime=true&endTime=${
          this.data.datas.componentData.endTime
李嘉林 committed
338
        }&terminalProductId=${terminalProductId? terminalProductId: ''}&terminalGoodsMixId=${terminalGoodsMixId}`
李嘉林 committed
339
      });
李嘉林 committed
340 341
    },
    onclickOrder(e) {
李嘉林 committed
342 343 344 345 346
      let {
        terminalGoodsMixId,
        terminalGoodsId,
        goodsId
      } = e.currentTarget.dataset.items;
侯体倬 committed
347 348 349 350 351
      app.trackCpn(e, this.data.datas.componentName, {
        terminalGoodsMixId,
        terminalGoodsId,
        goodsId
      })
李嘉林 committed
352 353 354 355
      $themeToLink({
        type: 1,
        link: `/order/orderConfirm?productGoodsMixId=${terminalGoodsMixId}&productGoodsId=${terminalGoodsId}&goodsId=${goodsId}&qty=1&goodsString=null`
      });
356 357
    },
    getData() {
358
      // if (this.loaded) return;
李嘉林 committed
359
      this.loaded = true;
李嘉林 committed
360 361 362 363 364 365
      let { dimension } = this.data.datas.componentData;
      if (dimension == 0) {
        this.init();
      } else if (dimension == 1) {
        this.initArticityList();
      }
366 367 368 369 370
    }
  },
  // 组件生命周期
  lifetimes: {
    created() {},
李嘉林 committed
371
    ready() {
李嘉林 committed
372 373
      this.loaded = false;
      this.isContain = false;
374
      this.getData();
李嘉林 committed
375
      this.getEleInfo(() => {
程默 committed
376
      });
李嘉林 committed
377 378 379 380 381 382 383 384 385
    },
    moved() {},
    detached() {}
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
李嘉林 committed
386 387
      this.loaded = false;
      this.isContain = false;
388
      this.getData();
李嘉林 committed
389
      this.getEleInfo(() => {
程默 committed
390
      });
李嘉林 committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
      const { isPageHidden } = this.data;
      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true
      });

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {}
  }
};

Component(componentOptions)