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

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

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

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

Component(componentOptions)