index.js 12.4 KB
Newer Older
李嘉林 committed
1
const app = getApp();
柳士祥 committed
2 3
const { goodsApi, DFSImg, $themeToLink, $themeAddToCard,log } = app;

李嘉林 committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true
  },
  behaviors: [],
  properties: {
    datas: {
      type: Object
    },
    items: {
      type: Object
    },
    indexs: {
      type: Number
    }
  },
  // 组件数据
  data: {
李嘉林 committed
23
    isPageHidden: false, // 页面是否处于隐藏状态
李嘉林 committed
24 25
    timeData: {},
    imgFillType: "widthFix",
HouTiZhuo committed
26 27 28 29 30
    secKillTimer1: null,
    secKillTimer2: null,
    secRestTime: {}, // 秒杀倒计时
    isBeginSecKill: false, // 是否开始秒杀
    isLessTenHour: false, // 秒杀活动小于 10 小时
李嘉林 committed
31 32
    isMoreThanOneDay: false, // 秒杀活动大于 1 天
    moreThanOneDay: 0, // 秒杀活动天数
HouTiZhuo committed
33
    currentSecKillInfo: {}, // 当前秒杀信息
hxx committed
34 35
    priceTextStyle: 'flex-start',
    cutPrice: "", // 砍价价格
侯体倬 committed
36 37
    minPrice: 0,
    minGoodsSuggestedRetailPrice: 0,
侯体倬 committed
38
    commissionColor: "#ff9051", // 佣金默认颜色
李嘉林 committed
39 40
  },
  // 数据监听器
HouTiZhuo committed
41 42
  observers: {
    items(val) {
hxx committed
43 44 45
      if (val && val.cutActivityGoodsList && val.cutActivityGoodsList.length != 0) {
        this.setData({ cutPrice: Number(val.cutActivityGoodsList[0].minPrice).toFixed(2) })
      }
HouTiZhuo committed
46 47 48 49 50
      if (val && val.secondKillActivityInfoGoodsList && !this.data.currentSecKillInfo.id) {
        if (val.secondKillActivityInfoGoodsList.length > 0) {
          this.getSecKillInfo(val);
        }
      }
侯体倬 committed
51 52 53 54 55 56 57 58 59 60 61 62 63
      // 价格设置
      const minPrice = +this.data.minPrice;
      if (+val.minPrice < minPrice || minPrice == 0) {
        this.setData({ minPrice: this.keepTwoNum(val.minPrice || 0) });
      }
      const minGoodsSuggestedRetailPrice = +this.data.minGoodsSuggestedRetailPrice;
      if (+val.minGoodsSuggestedRetailPrice < minGoodsSuggestedRetailPrice
        || minGoodsSuggestedRetailPrice == 0) {
        this.setData({
          minGoodsSuggestedRetailPrice: this.keepTwoNum(val.minGoodsSuggestedRetailPrice || 0)
        })
      }
    },
HouTiZhuo committed
64
  },
李嘉林 committed
65 66
  // 组件方法
  methods: {
柳士祥 committed
67 68
    init() {
      let datas = this.data.datas
李嘉林 committed
69
      let { imgFillType } = this.data;
柳士祥 committed
70 71 72
      if(datas.componentData.angleSignImg && datas.componentData.angleSignImg.lenght != 0){
        datas.componentData.angleSignImg = DFSImg(datas.componentData.angleSignImg,150,150,1)
      }
侯体倬 committed
73 74 75
      if (datas.componentData.commissionColor) {
        this.setData({ commissionColor: datas.componentData.commissionColor });
      }
李嘉林 committed
76 77 78 79 80
      if (datas.componentData.style === 'rowList' || datas.componentData.style === 'heap') {
        imgFillType = "aspectFit"
      } else if (datas.componentData.style == 'waterfall' && datas.componentData.imgShowMode == "1") {
        imgFillType = "aspectFill"
      } else {
李嘉林 committed
81 82
        // imgFillType = "widthFix"
        imgFillType = "heightFix"
李嘉林 committed
83
      }
柳士祥 committed
84
      this.setData({
李嘉林 committed
85 86
        datas,
        imgFillType
柳士祥 committed
87 88
      })
      log.info('datas');
HouTiZhuo committed
89 90 91 92 93 94
      const textStyle = {
        'left': 'flex-start',
        'center': 'center',
        'right': 'flex-end'
      };
      this.setData({ priceTextStyle: textStyle[this.data.datas.componentData['priceTextAlign']] })
柳士祥 committed
95
    },
HouTiZhuo committed
96 97 98 99 100 101 102
    // 获取当前秒杀信息
    getSecKillInfo(val) {
      if (val.secondKillActivityInfoGoodsList.length === 0) return;
      const tempObj = val.secondKillActivityInfoGoodsList[0];
      const item = val.goodsItems.find(item => item.goodsId == tempObj.goodsId);
      tempObj.spikePrice = Number(tempObj.spikePrice).toFixed(2);
      tempObj.originPrice = Number(item.salePrice).toFixed(2);
HouTiZhuo committed
103
      tempObj.formatTime = this.formatDate(tempObj.startTime);
HouTiZhuo committed
104
      this.setData({ currentSecKillInfo: tempObj })
李嘉林 committed
105
      console.log('currentSecKillInfo', this.data.currentSecKillInfo);
HouTiZhuo committed
106
      const timestamp = new Date(this.data.currentSecKillInfo.startTime) - Date.now()
李嘉林 committed
107
      this.setData({ currentSecKillInfo: { ...this.data.currentSecKillInfo, restTime: this.getSaleTime(this.data.currentSecKillInfo.endTime) } })
HouTiZhuo committed
108
      if (timestamp > 0) {
HouTiZhuo committed
109 110 111 112 113 114 115 116
        this.setData({ isBeginSecKill: false });
        // 判断当前剩余时间是否小于 10 分钟
        if (timestamp < 10 * 60 * 1000) {
          this.calcSecKillIsBeginLoop();
        }
      } else {
        this.setData({ isBeginSecKill: true });
        this.calcSecKillIsLessTenMin();
李嘉林 committed
117
        this.calcMoreThanOneDay();
HouTiZhuo committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      }
    },
    // 判断秒杀活动是否开始
    calcSecKillIsBeginLoop() {
      clearTimeout(this.data.secKillTimer1);
      if (this.data.isBeginSecKill) return;
      const secKillTimer1 = setTimeout(() => {
        if (new Date(this.data.currentSecKillInfo.startTime) - Date.now() > 0) {
          this.setData({ isBeginSecKill: true })
        }
      }, 1000);
      this.setData({ secKillTimer1 });
    },
    // 判断秒杀活动是否小于 10 小时
    calcSecKillIsLessTenMin() {
      clearTimeout(this.data.secKillTimer2);
      if (this.data.isLessTenHour) return;
      const secKillTimer2 = setTimeout(() => {
        if (new Date(this.data.currentSecKillInfo.endTime) - Date.now() <= 36000000) {
          this.setData({ isLessTenHour: true });
          this.setData({ currentSecKillInfo: { ...this.data.currentSecKillInfo, restTime: this.getSaleTime(this.data.currentSecKillInfo.endTime) } })
        }
      }, 1000);
      this.setData({ secKillTimer2 })
    },
李嘉林 committed
143 144 145 146 147 148
    calcMoreThanOneDay() {
      if (new Date(this.data.currentSecKillInfo.endTime) - Date.now() > 86400000) {
        this.setData({ isMoreThanOneDay: true });
        this.setData({ moreThanOneDay: Math.floor((new Date(this.data.currentSecKillInfo.endTime) - Date.now()) / 86400000) })
      }
    },
HouTiZhuo committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    formatDate(str) {
      if (!str) return;
      const date = new Date(str.replace(/-/g, '/')); // 将横线替换为斜杠
      const month = date.getMonth() + 1; // 获取月份(注意要加 1)
      const day = date.getDate(); // 获取日期
      const hour = date.getHours(); // 获取小时
      const min = date.getMinutes(); // 获取分钟

      return `${month}${day}${hour}:${min.toString().padStart(2, '0')}开始`;
    },
    onSecRestTimeChange(e) {
      const detail = e.detail;
      detail.hours = detail.hours.toString().padStart(2, '0');
      detail.minutes = detail.minutes.toString().padStart(2, '0');
      detail.seconds = detail.seconds.toString().padStart(2, '0');
      this.setData({ secRestTime: detail });
    },
    // 秒杀结束
    handleSecFinish() {
      this.setData({ isBeginSecKill: false });
侯体倬 committed
169
      this.setData({ 'items.secondKillActivityInfoGoodsList': null })
HouTiZhuo committed
170
    },
李嘉林 committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    getSaleTime(val) {
      return new Date(val.replace(/-/g, "/")).getTime() - new Date().getTime();
    },
    showVipPrice(index) {
      // 索引 0 显示价格 1显示名称
      let showFlag = false;
      let vipPrice = this.items["minPriceAfterRecommendedCardPriceTable"];
      if (vipPrice && Number(vipPrice) < Number(this.items["minPrice"])) {
        showFlag = true;
      }
      console.log(showFlag, "------------760");
      return showFlag;
    },
    showVipTag(index) {
      // 索引 0 显示价格 1显示名称
      let showFlag = false;
      let vipPrice = this.items["minPriceAfterRecommendedCardPriceTable"];
      let showTag = this.items["recommendedCardMinPricePriceTableName"];
      if (
        vipPrice &&
        showTag &&
        Number(vipPrice) <= Number(this.items["minPrice"])
      ) {
        showFlag = true;
      }
      return showFlag;
李嘉林 committed
197 198 199
    },
    onChange(e) {
      this.setData({ timeData: e.detail });
李嘉林 committed
200
    },
侯体倬 committed
201
    onclickProduct(e) {
李嘉林 committed
202 203 204 205
      // 测试用↓
      // this.onclickBuynow(e);
      // return;
      // 测试用↑
李嘉林 committed
206 207
      let { goodsType } = this.data.datas.componentData;
      let {
侯体倬 committed
208
        shopName,
李嘉林 committed
209
        productId,
侯体倬 committed
210
        productName,
李嘉林 committed
211
        terminalProductId,
hxx committed
212 213
        terminalGoodsMixId,
        productSourceType
李嘉林 committed
214 215 216 217
      } = this.data.items;
      let linkVal = "";
      if (goodsType == 0) {
        linkVal = `/goods/${productId}?terminalProductId=${terminalProductId}`;
hxx committed
218 219 220
        if (productSourceType != undefined && productSourceType != null) {
          if (productSourceType == 0) linkVal += '&productSourceType=0';
        }
李嘉林 committed
221 222 223
      } else if (goodsType == 1) {
        linkVal = `/pointShop/goodsDetail?productId${productId}&terminalProductId=${terminalProductId}&terminalGoodsMixId=${terminalGoodsMixId}`;
      }
侯体倬 committed
224 225 226
      app.trackCpn(e, this.data.datas.componentName, {
        shopName,productId,productName,terminalProductId,terminalGoodsMixId
      })
李嘉林 committed
227 228 229 230 231
      $themeToLink({
        type: 1,
        link: linkVal
      });
    },
李嘉林 committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    // 收藏
    toStart(e) {
      let {items} = this.data;
      console.log(this.data.items,e,'----items')
      let query = {
        productId: items.productId,
        terminalProductId: items.terminalProductId,
      }
      if (!items.collectFlag) {
        // 收藏
        goodsApi.collect_goods(query).then(res1 => {
          let res = res1.data;
          if(res.code == 200 && res.data) {
            this.setData({
              "items.collectFlag": !items.collectFlag
            })
          }
        })
      } else {
        // 取消收藏
        goodsApi.uncollect_goods(query).then(res1 => {
          let res = res1.data;
          if(res.code == 200 && res.data) {
            this.setData({
              "items.collectFlag": !items.collectFlag
            })
          }
        })
      }
    },
侯体倬 committed
262
    onclickBuynow(e) {
李嘉林 committed
263
      let { btnType } = this.data.datas.componentData;
李嘉林 committed
264
      let _this = this;
李嘉林 committed
265 266 267
      let {
        minProductGoodsMixid,
        minProductGoodsId,
侯体倬 committed
268 269 270
        productName,
        minGoodsId,
        shopName
李嘉林 committed
271 272 273 274 275
      } = this.data.items;
      let linkVal = "";
      if (btnType < 6) {
        // 加入购物车
        console.log("加入购物车");
李嘉林 committed
276 277
        // $themeAddToCard(this.data.items);
        console.log(app.globalData.shopInfo.whetherOpenEnterprisesWantGoods - 0,'app.globalData.shopInfo=')
李嘉林 committed
278 279 280 281
        let { whetherToEnableQuickPurchase } = app.shop_configuration;
        if (whetherToEnableQuickPurchase == 1) {
          // 快速加购
          $themeAddToCard(this.data.items);
李嘉林 committed
282
        } else {
李嘉林 committed
283 284 285 286 287 288 289
          // 选规格加购
          if (app.globalData.shopInfo.whetherOpenEnterprisesWantGoods - 0 == 1) {
            // 开启企业要货加购
            app.openTwoBAddCart(_this.data.items);
          } else {
            app.openSelectGoods(_this.data.items);
          }
李嘉林 committed
290
        }
李嘉林 committed
291 292 293
      } else {
        // 进入确认订单
        linkVal = `/order/orderConfirm?productGoodsMixId=${minProductGoodsMixid}&productGoodsId=${minProductGoodsId}&goodsId=${minGoodsId}&qty=1&goodsString=null&source=3`;
李嘉林 committed
294 295 296 297 298 299 300
        //! 活动商品url加相关参数
        // 秒杀(isSecKill是否秒杀活动,secKillId秒杀活动id,secKillGoodsId秒杀活动商品id)
        console.log('currentSecKillInfo', this.data.currentSecKillInfo);
        if (this.data.currentSecKillInfo && this.data.currentSecKillInfo.activityStatus == 1) {
          linkVal += `&isSecKill=true&secKillId=${this.data.currentSecKillInfo.id}&secKillGoodsId=${this.data.currentSecKillInfo.goodsId}`;
        }
        console.log('linkVal', linkVal);
李嘉林 committed
301
      }
侯体倬 committed
302 303 304
      app.trackCpn(e, this.data.datas.componentName, {
        shopName,minGoodsId,productName,minProductGoodsId,minProductGoodsMixid
      })
李嘉林 committed
305 306
      $themeToLink({ type: 1, link: linkVal });
    },
侯体倬 committed
307
    toMerchants(e) {
李嘉林 committed
308
      let { orgId } = this.data.items;
侯体倬 committed
309
      app.trackCpn(e, this.data.datas.componentName, 'toMerchants')
李嘉林 committed
310 311 312
       $themeToLink({ type: 1, link: `/merchantsDetail/${orgId}` });

    },
313 314 315 316 317 318
    errorFunction(e) {
      this.setData({
        "items.productImgUrl":"https://cdn.mayi888.com/product/85pdScJ5ch.jpg?x-oss-process=image/resize,m_pad,limit_0,w_400,h_400",
        "items.coverImage":"https://cdn.mayi888.com/product/85pdScJ5ch.jpg?x-oss-process=image/resize,m_pad,limit_0,w_400,h_400",
      })
    },
侯体倬 committed
319
    keepTwoNum(num) {
侯体倬 committed
320
      num = Number(num);
侯体倬 committed
321
      return isNaN(num) ? 0 : num.toFixed(2);
侯体倬 committed
322
    }
李嘉林 committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  },
  // 组件生命周期
  lifetimes: {
    created() {},
    attached() {
      this.init();
    },
    ready() {},
    moved() {},
    detached() {}
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

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

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

Component(componentOptions)