index.js 9.95 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 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 133
    second: 0,
    goodsList:[],
李嘉林 committed
134 135 136 137 138 139
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {
李嘉林 committed
140 141 142 143 144 145
      let {
        goodsList,
        activityCode,
        selectActivityValue,
        dimension
      } = this.data.datas.componentData;
李嘉林 committed
146
      let { deleteFlag, endTimeData } = this.data;
李嘉林 committed
147
      let _this = this;
李嘉林 committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
      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
162 163 164
          endTimeData =
            new Date(copyData.endTime.replace(/-/g, "/")).getTime() -
            new Date().getTime();
李嘉林 committed
165 166 167 168

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

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

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

Component(componentOptions)