index.js 10.6 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: {
李嘉林 committed
120 121
    loaded: false,
    isContain: false,
李嘉林 committed
122 123 124 125 126 127 128 129 130 131
    isPageHidden: false, // 页面是否处于隐藏状态
    timeData: {},
    isLoading: true,
    deleteFlag: false, //活动未禁用
    newTime: new Date().getTime(),
    timer: null,
    hideAll: false,
    days: 0,
    hours: 0,
    minute: 0,
李嘉林 committed
132
    second: 0,
李嘉林 committed
133 134
    goodsList: [],
    selectActivityValue:"",
李嘉林 committed
135 136 137 138 139 140
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {
李嘉林 committed
141 142 143 144 145 146
      let {
        goodsList,
        activityCode,
        selectActivityValue,
        dimension
      } = this.data.datas.componentData;
李嘉林 committed
147
      let { deleteFlag, endTimeData } = this.data;
李嘉林 committed
148
      let _this = this;
李嘉林 committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162
      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;
          }
李嘉林 committed
163 164 165
          endTimeData =
            new Date(copyData.endTime.replace(/-/g, "/")).getTime() -
            new Date().getTime();
李嘉林 committed
166 167 168 169

          console.log(endTimeData, "----------------------150");
          this.setData({ endTime: endTimeData });
          this.setData({ deleteFlag });
李嘉林 committed
170
          console.log("------------155");
李嘉林 committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
          // 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
187
          this.setData({ isLoading: false });
李嘉林 committed
188 189
          copyData.goodsItems.forEach((item, index) => {
            item.productImgUrl = DFSImg(item.imgUrl);
李嘉林 committed
190
            item.productImgUrl1 = DFSImg(item.imgUrl);
李嘉林 committed
191 192 193
            item.minPrice = parseFloat(item.discountPrice);
            item.maxPrice = parseFloat(item.salePrice);
          });
李嘉林 committed
194
          _this.setData({
李嘉林 committed
195 196 197
            goodsList: copyData.goodsItems,
            selectActivityValue: copyData.singleItemActivityName
          });
李嘉林 committed
198
          console.log(_this.data.selectActivityValue, "--------------197");
李嘉林 committed
199 200 201 202 203 204 205 206 207 208 209 210 211
        }
      });
    },
    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
212
        this.setData({ isLoading: false });
李嘉林 committed
213 214 215 216
        return;
      }
      promoteApi.query_single_item_activity_list_by_ids(ids).then(res => {
        if (res.data.code == 200) {
李嘉林 committed
217
          let list = res.data.data || [];
李嘉林 committed
218 219 220
          if (list.length > 0) {
            let hideNum = 0;
            list.forEach((item, index) => {
李嘉林 committed
221 222 223 224 225
              if (
                new Date(item.endTime.replace(/-/g, "/")).getTime() -
                  new Date().getTime() <=
                0
              ) {
226
                // item.isEnd = true;
李嘉林 committed
227 228
                hideNum += 1;
              } else {
229
                // item.isEnd = false;
李嘉林 committed
230
              }
李嘉林 committed
231 232 233 234 235 236 237
              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
238 239 240 241
                item.singleItemActivityCoverImage,
                400,
                400,
                1
李嘉林 committed
242 243 244 245 246 247 248
              );
              item.isStart =
                new Date(item.startTime.replace(/-/g, "/")).getTime() >
                new Date().getTime()
                  ? true
                  : false;
            });
李嘉林 committed
249 250 251 252 253 254 255
            let newList = list.filter(item => {
              return (
                new Date(item.endTime.replace(/-/g, "/")).getTime() -
                  new Date().getTime() >
                0
              );
            });
256
            this.setData({ "datas.componentData.actList": newList });
李嘉林 committed
257
            if (hideNum == actList.length) {
李嘉林 committed
258
              this.setData({ hideAll: true });
李嘉林 committed
259
            }
李嘉林 committed
260
            this.setData({ isLoading: false });
李嘉林 committed
261 262 263 264
          }
        }
      });
    },
李嘉林 committed
265
    getScroll() {
266
      this.getData();
李嘉林 committed
267
      this.getEleInfo(() => {
程默 committed
268 269 270
      });
    },
    // 获取当前组件位置信息
李嘉林 committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284
    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
285
    },
李嘉林 committed
286 287 288 289
    onChange(e) {
      this.setData({ timeData: e.detail });
    },
    onclickProduct(e) {
李嘉林 committed
290 291 292 293 294
      console.log(
        e.currentTarget.dataset.items,
        "-------------------------226"
      );
      let {
侯体倬 committed
295 296 297
        shopName,
        productId,
        productName,
李嘉林 committed
298
        terminalProductId,
侯体倬 committed
299
        terminalGoodsMixId
李嘉林 committed
300
      } = e.currentTarget.dataset.items;
侯体倬 committed
301 302 303 304 305 306 307
      app.trackCpn(e, this.data.datas.componentName, {
        shopName,
        productId,
        productName,
        terminalProductId,
        terminalGoodsMixId
      })
李嘉林 committed
308 309 310 311
      $themeToLink({
        type: 1,
        link: `/goods/${productId}?limitedLime=true&endTime=${
          this.data.datas.componentData.endTime
312
        }&terminalProductId=${terminalProductId}&terminalGoodsMixId=${terminalGoodsMixId}`
李嘉林 committed
313
      });
李嘉林 committed
314 315
    },
    onclickOrder(e) {
李嘉林 committed
316 317 318 319 320
      let {
        terminalGoodsMixId,
        terminalGoodsId,
        goodsId
      } = e.currentTarget.dataset.items;
侯体倬 committed
321 322 323 324 325
      app.trackCpn(e, this.data.datas.componentName, {
        terminalGoodsMixId,
        terminalGoodsId,
        goodsId
      })
李嘉林 committed
326 327 328 329
      $themeToLink({
        type: 1,
        link: `/order/orderConfirm?productGoodsMixId=${terminalGoodsMixId}&productGoodsId=${terminalGoodsId}&goodsId=${goodsId}&qty=1&goodsString=null`
      });
330 331
    },
    getData() {
332
      // if (this.loaded) return;
李嘉林 committed
333
      this.loaded = true;
李嘉林 committed
334 335 336 337 338 339
      let { dimension } = this.data.datas.componentData;
      if (dimension == 0) {
        this.init();
      } else if (dimension == 1) {
        this.initArticityList();
      }
340 341 342 343 344
    }
  },
  // 组件生命周期
  lifetimes: {
    created() {},
李嘉林 committed
345
    ready() {
李嘉林 committed
346 347
      this.loaded = false;
      this.isContain = false;
348
      this.getData();
李嘉林 committed
349
      this.getEleInfo(() => {
程默 committed
350
      });
李嘉林 committed
351 352 353 354 355 356 357 358 359
    },
    moved() {},
    detached() {}
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
李嘉林 committed
360 361
      this.loaded = false;
      this.isContain = false;
362
      this.getData();
李嘉林 committed
363
      this.getEleInfo(() => {
程默 committed
364
      });
李嘉林 committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
      const { isPageHidden } = this.data;
      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

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

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

Component(componentOptions)