CarOwner.js 11 KB
import {
  discoveryApi,
  shopmallApi,
  couponsApi
} from "~/api/api";
import {
  decimal,
  centsToyuan
} from "~/utils/format";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    height: null,
    onlyHaveCarOwner: {
      type: Boolean
    },
    channelID: {
      type: Number,
      observer: function (val) {
        if (val) {
          // this.setList(val);
        }
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    curSubTabId: 1,
    tabList: [{
      id: 1,
      name: "商品兑换"
    },
    {
      id: 2,
      name: "油卡兑换"
    },
    {
      id: 3,
      name: "门店服务"
    },
    ],
    isShowTab: false,
    curCategoryIdx: 0,
    hasCarOwnerCouponFlag: false,
    toCategory: "toCategory-0",
    categoryList: [],
    categoryStyle: -1,
    goodsList: [],
    goodsItemHeight: 0,
    goodsItemTopList: [],
    curPlayIdx: 0,
    // leftList: [],
    // rightList: [],
    // leftHeight: 0,
    // rightHeight: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toChangeTab(e) {
      const {
        id
      } = e.currentTarget.dataset.tab;
      this.setData({
        curSubTabId: id,
      });
      if (id != 1) {
        wx.$eventbus.emit("showCart", false); // 油卡和门店服务不展示购物车
      }else{
        wx.$eventbus.emit("showCart", true)
      }
    },
    toSelectCategory(e) {
      const {
        index,
        item
      } = e.currentTarget.dataset;
      this.setData({
        curCategoryIdx: index,
        toSeri: `toCategory-${index}`,
        categoryStyle: item.categoryStyle || 0,
      });
      this.triggerEvent("showShoppingCar", item.categoryStyle);
      this.getCategoryGoodsList(item.id);
    },
    //获取分类
    getCategoryList() {
      const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
      const _params = {
        merchantId,
        categoryType: "2",
      };
      discoveryApi.getDiscoverChannelCategory(_params).then((res) => {
        if (res?.length) {
          this.setData({
            categoryList: res,
            categoryStyle: res[0].categoryStyle || 0,
          });
          this.triggerEvent("showShoppingCar", res[0].categoryStyle);
          this.getCategoryGoodsList(res[0].id);
        }
      });
    },
    // 判断显示是否有油卡、门店服务
    getDiscoverCount() {
      const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
      let _para = {
        merchantId
      };
      shopmallApi.getDiscoverCount(_para).then((res) => {
        let { tabList } = this.data,
          ids = [],
          data;
        if (res.oilCouponNum == 0) {
          ids.push(2)
        }
        if (res.merchantCouponNum == 0) {
          ids.push(3)
        }
        data = tabList.filter((item, index) => {
          return !ids.includes(item.id)
        })
        this.setData({ tabList: data, isShowTab: true })
      })
    },
    //获取商品列表
    async getCategoryGoodsList(categoryId) {
      const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
      let _para = {
        merchantId,
        categoryId,
        categoryType: "2",
      };
      const res = await discoveryApi.getDiscoverChannelCategoryGoods(_para);
      let isShowPointStyle = true
      if (res && res.length) {
        res.forEach((el) => {
          el.activityPrice = Number(centsToyuan(el.activityPrice));
          el.originalPrice = Number(centsToyuan(el.originalPrice));
          if (el.activityPrice) isShowPointStyle = false
          if(el.discoverGoodsSource==4){
            el.originalPrice = el.marketPrice
          }
        });
      } else {
        isShowPointStyle = false
      }
      const idx = res.findIndex(
        (item) => item.commerceVideo || item.goodsVideo
      );
      if (isShowPointStyle) {
        wx.$eventbus.emit("showCart", false); // 分类下商品活动价都是0时,展示积分兑换,底部结算按钮隐藏
      }
      this.setData({
        isShowPointStyle,
        goodsList: [...res] || [],
        curPlayIdx: idx > -1 ? idx : 0,
      });
    },
    scrollFn(e) {
      // console.log(e.scrollTop)
      // try {
      // let idx = this.data.goodsItemTopList.findIndex(item => e.scrollTop+300 < item)
      // this.setData({
      //     curPlayIdx: idx >-1 ? idx : 0
      // })
      // } catch (error) {
      //     console.log(error)
      // }

      // let curPlayId = 'video' + this.data.curPlayIdx
      // let videoContext = wx.createvideoContext(curPlayId);
      // videoContext.stop();
      if (this.data.categoryStyle != 3) return;
      const query = wx.createSelectorQuery().in(this);
      query
        .selectAll(".goods_box")
        .boundingClientRect((rect) => {
          if (rect?.length) {
            let curPlayIdx = -1;
            let goodsItemTopList = rect.map((item) => item.top);
            let idx = goodsItemTopList.findIndex((item) => item > 300);
            curPlayIdx = idx - 1 > -1 ? idx - 1 : 0;
            const _targetItem = this.data.goodsList[curPlayIdx] || {};
            if (!(_targetItem?.commerceVideo || _targetItem?.goodsVideo)) {
              for (let i = idx; i < this.data.goodsList.length; i++) {
                if (
                  this.data.goodsList[i]?.commerceVideo ||
                  this.data.goodsList[i]?.goodsVideo
                ) {
                  curPlayIdx = i;
                  break;
                }
              }
            }
            this.setData({
              curPlayIdx: curPlayIdx > -1 ? curPlayIdx : 0,
            });
          }
        })
        .exec();
    },
    // 跳转商品详情
    toGoodsDetail(e) {
      const {
        item
      } = e.currentTarget.dataset;
      const B_userInfo = wx.$storage.get("userInfo");
      if (B_userInfo && B_userInfo.userType) {
        wx.navigateTo({
          url: "/pages/Login/Login",
        });
        return;
      }
      if (item.commerceVideo && !this.data.isShowPointStyle) {
        const {
          coverUrl,
          commerceVideo,
          commerceDescribe,
          activityPrice,
          originalPrice,
          goodsName,
          goodsImage,
          id,
        } = item;
        wx.$router.go(
          `/packageShopmall/packageMall/pages/goodsVideo/goodsVideo`, {
          id,
          notIntercept: true,
          coverUrl: goodsImage,
          commerceVideo,
          commerceDescribe,
          activityPrice,
          originalPrice,
          goodsName,
          hasCouponFlag: this.data.hasCarOwnerCouponFlag,
        }
        );
      } else {
        if (item.discoverGoodsSource == 4) {
          const query = `?id=${item.id}&isCarOwner=${true}`
          wx.navigateTo({
            url: '/packageShopmall/packageMall/pages/platformShop/goodsDetail/index' + query,
          });
          return
        }
        wx.$router.go('/packageShopmall/packageMall/pages/detail/detail', {
          id: item.id,
          hasCouponFlag: this.data.hasCarOwnerCouponFlag,
          isShowPointStyle: this.data.isShowPointStyle,
          isCarOwner: true,
          notIntercept: true,
        });
      }
    },
    // imageLoadError(e) {
    //     this.imageLoaded()
    // },
    // imageLoaded(e) {
    //     console.log(e)
    //     let { goodsList, leftList, rightList } = this.data
    //     if (!goodsList?.length) return;
    //     let leftHeight = 0
    //     let rightHeight = 0
    //     const query = wx.createSelectorQuery().in(this)
    //     query.select('.left-box').boundingClientRect();
    //     query.select('.right-box').boundingClientRect();
    //     query.exec((res) => {
    //         if (res[0]) {
    //             leftHeight = res[0].height; //获取左边列表的高度
    //             rightHeight = res[1].height; //获取右边列表的高度
    //         }
    //         console.log(leftHeight, rightHeight)
    //         const item = goodsList.shift()
    //         leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item);
    //         this.setData({
    //             leftList,
    //             rightList
    //         })
    //     });
    // },
    // 获取购物车列表
    async getBuyCarList() {
      const B_userInfo = wx.$storage.get("userInfo");
      if (B_userInfo && B_userInfo.userType) return;
      const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
      const res = await shopmallApi.getShoppingcartList({
        cartType: 4,
        merchantId,
      });
      wx.$eventbus.emit("getBuyCarList", res);
    },
    // 加入购物车
    async toAddBuuCar(e) {
      if (wx.$authStatus) {
        if (this.data.hasCarOwnerCouponFlag) {
          wx.$router.go(
            `/packageShopmall/packageMall/pages/getCoupon/getCoupon?discoverGoodsType=2&from=2`, {
            notIntercept: true
          }
          );
        } else {
          wx.navigateTo({
            url: "/pages/Login/Login",
          });
        }
        return;
      }
      const {
        item
      } = e.currentTarget.dataset;
      let _para = {
        goodsId: item.id,
        goodsNumber: 1,
        cartType: 4,
      };
      if(item.discoverGoodsSource==4){
        _para.carOwnerPlatformFlag = 1
        _para.merchantId = wx.$storage.get("srcm_shopmall_merchant_id")
        if(!item.activityPrice&&item.exchangeIntegralFlag)return wx.$api.showToast("该商品金额为0不支持加入购物车")
      }
      const res = await shopmallApi.addShoppingcart(_para);
      if (res) {
        wx.$api.showToast("添加成功");
        this.getBuyCarList();
      }
    },
    getCarOwnerCouponList() {
      const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
      couponsApi
        .getMerchantCouponsListApi({
          merchantId,
          discoverGoodsType: 2
        })
        .then((res) => {
          if (res?.length) {
            this.setData({
              hasCarOwnerCouponFlag: !!res?.length,
            });
          }
        });
    },
    onShow() {
      this.getBuyCarList();
    },
    // 去兑换--确认订单页面
    toDuiHuang(e) {
      if (wx.$authStatus) {
        if (this.data.hasCarOwnerCouponFlag) {
          wx.$router.go(
            `/packageShopmall/packageMall/pages/getCoupon/getCoupon?discoverGoodsType=2&from=2`, {
            notIntercept: true
          }
          );
        } else {
          wx.navigateTo({
            url: "/pages/Login/Login",
          });
        }
        return;
      }
      const {
        item
      } = e.currentTarget.dataset
      // 数据整理
      item.depositPrice = item.depositPrice || 0
      item.goodsNumber = 1
      item.goodsId = item.id
      wx.$router.go('/packageDiscovery/pages/pay/index', {
        goodsList: [item],
        deliveryType: 0,
        showAddr: item.deliveryType == 1 ? 0 : 1,
        buyType: 'pay',
        discoverGoodsSource: item.discoverGoodsSource,
        useJFtoPay: true
      })
    },
  },
  lifetimes: {
    ready: function () {
      this.getDiscoverCount()
      this.getCategoryList();
      const B_userInfo = wx.$storage.get("userInfo");
      if (B_userInfo && B_userInfo.userType) return;
      this.getBuyCarList();
      this.getCarOwnerCouponList();
    },
  },
});