Goods.js 7.86 KB
Component({
  options: {
    styleIsolation: 'isolated',//表示样式隔离,默认隔离
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) {
          this.setData({
            keyWord: ''
          })
          this.setGoodsActive();
        }
      },
    },
    goodsData: {
      type: Array,
      observer: function (n) {
        if (n.length > 0) {
          this.getOrderBaseGoodsList(n);
        }
      },
    },
    chooseGoodsList: {
      type: Array,
      observer: function (n) {
        this.setGoodsActive();
      },
    },
    minPrice: {
      type: Number,
      // value:0
    },
    isSolarFilm: Boolean,
    baseConfigData: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsList: [],
    keyWord: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setGoodsActive() {
      let { chooseGoodsList, goodsList } = this.data;
      if (chooseGoodsList.length == 0) {
        goodsList.map((e) => {
          e.upgradeGoodsList.map((c) => (c.active = false));
        });
      } else {
        let indexList = [];
        goodsList.map((e, index) => {
          e.upgradeGoodsList.map((c, ind) => {
            c.active = false;
            chooseGoodsList.map((s) => {
              if (s.merchantGoodsId == c.id || s.id == c.id) {
                c.count = s.count;
                indexList.push({
                  index,
                  ind,
                });
              }
            });
          });
        });
        indexList.map((e) => {
          goodsList[e.index].upgradeGoodsList[e.ind].active = true;
        });
      }
      this.setData({
        goodsList,
      });
    },
    getOrderBaseGoodsList(data) {
      this.setData({
        goodsList: data,
      });
    },
    // 删除数组重复项 都不保留
    removeDuplicates(arr, property) {
      console.log(arr, 'packageCode');
      var uniqueArray = [];
      for (var i = 0; i < arr.length; i++) {
        var value = arr[i][property];
        // 兼容套餐
        if ((!uniqueArray.some((obj) => obj[property] === value) || arr[i]?.packageCode)) {
          uniqueArray.push(arr[i]);
        }
      }
      return uniqueArray;
    },

    // 去重保留第二项
    arrayUnique(arr, name, packageCode = 'packageCode') {
      console.log(arr, 'arrayUnique');
      var hash = {};
      return arr.reduce(function (acc, cru, index) {
        // 兼容套餐逻辑
        if (!hash[cru[name]] || !hash[cru[packageCode]]) {
          hash[cru[name]] = { index: index };
          acc.push(cru);
        } else {
          acc.splice(hash[cru[name]]["index"], 1, cru);
        }
        return acc;
      }, []);
    },
    getChooseList() {
      let { goodsList, chooseGoodsList, isSolarFilm } = this.data;
      console.log(goodsList, chooseGoodsList, 'goodsList');
      let chooseList = [];
      // debugger
      // 太阳膜下单 需保留已选的商品
      if (isSolarFilm) {
        let upgradeGoodsList = [];
        goodsList.map((e) => {
          upgradeGoodsList = upgradeGoodsList.concat(e.upgradeGoodsList);
        });
        let selectActiveArr = [];
        for (var i = 0; i < upgradeGoodsList?.length; i++) {
          let val = upgradeGoodsList[i];
          if (val.active) selectActiveArr.push(val);
        }
        if (!selectActiveArr?.length) return wx.$util.showToast("请勾选商品");
        // 将已选商品与当前弹窗数据对比处理数据
        console.log(upgradeGoodsList, chooseGoodsList, 'chooseGoodsList');
        let arr = this.removeDuplicates(
          upgradeGoodsList.concat(chooseGoodsList),
          "id"
        );
        console.log(arr, 'arr');
        arr?.map((c) => {
          if (c.active) {
            if (!c.count) c.count = 1;
            if (chooseGoodsList.length > 0) {
              chooseGoodsList.map((x) => {
                if (x.id == c.id) {
                  c.price = x.price || 0;
                }
              });
            } else {
              c.price = c.upgradePrice || 0;
            }
            if (!c.price) c.price = c.upgradePrice;
            chooseList.push(c);
          }
        });
      } else {
        goodsList.map((e) => {
          e.upgradeGoodsList.map((c) => {
            if (c.active) {
              if (!c.count) c.count = 1;
              if (chooseGoodsList.length > 0) {
                chooseGoodsList.map((x) => {
                  if (x.id == c.id) {
                    c.price = x.price || 0;
                  }
                });
              } else {
                c.price = c.upgradePrice || 0;
              }
              if (!c.price) c.price = c.upgradePrice;
              console.log(c, 'isSolarFilm');
              chooseList.push(c);
            }
          });
        });
      }
      return this.arrayUnique(chooseList, "id");
    },
    choose(e) {
      let { item, index, ind } = e.currentTarget.dataset;
      let { active } = item;
      let { minPrice } = this.data;
      // item
      // console.log(item)
      // console.log(minPrice)
      // console.log(item.goodsInfo.shopMinPrice)
      // console.log(minPrice && minPrice < item.shopMinPrice)
      // 如果有设置订单价格 那就不能选单个成本价比总价还高的
      if (minPrice && minPrice / 100 < item.shopMinPrice) return false;
      // 太阳膜类商品 未配置部位不可选择
      // if (item?.goodsInfo?.goodsType == 1 && !item.merchantGoodsPosition) return false
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].active`;
      this.setData({
        [target]: !active,
      });
    },
    addCount(e) {
      let { index, count, item, ind } = e.currentTarget.dataset;
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`;
      if (!item.count) item.count = 1;
      item.count += count;
      if (item.count < 1) item.count = 1;
      if (item.count > 999) item.count = 999;
      this.setData({
        [target]: item.count,
      });
    },
    input(e) {
      let { index, ind } = e.currentTarget.dataset;
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`;
      let { value } = e.detail;
      if (value < 1) value = 1;
      if (value > 999) value = 999;
      this.setData({
        [target]: Number(value),
      });
    },
    searchGoods(e) {
      let { goodsData } = this.data;
      let { value } = e.detail;
      if (!value) {
        this.getOrderBaseGoodsList(goodsData);
        setTimeout(() => {
          this.setGoodsActive();
        }, 500);
        return;
      }
      let _DATA_ = JSON.parse(JSON.stringify(goodsData));
      let arr = [];
      _DATA_.map((i) => {
        let upgradeGoodsList = [];
        i.upgradeGoodsList.map((c) => {
          // console.log(c);
          let name =
            c.upgradeGoodsAlias || c.upgradeGoodsName || c.goodsInfo.goodsName;
          if (name.indexOf(value) != -1) {
            upgradeGoodsList.push(c);
          }
        });
        if (
          i.goodsTypeName.indexOf(value) != -1 ||
          upgradeGoodsList.length > 0
        ) {
          i.upgradeGoodsList =
            upgradeGoodsList.length > 0 ? upgradeGoodsList : i.upgradeGoodsList;
          arr.push(i);
        }
      });
      console.log(arr);
      this.setData({
        goodsList: arr,
      });
    },
    cancel() {
      this.setData({
        show: false,
      });
    },
    confirm() {
      let chooseList = this.getChooseList();

      let { goodsList } = this.data;
      if (goodsList.length < 1) return;
      if (!chooseList.length) return wx.$util.showToast("请勾选商品");

      let thisPage = wx.$router.getPageInstance();
      thisPage?.setGoodsTotal && thisPage.setGoodsTotal(chooseList);
      // console.log(chooseList)
      thisPage.setData({
        goodsList: chooseList,
      });
      this.triggerEvent("confirm", chooseList);
      this.setData({
        show: false,
      });
    },
  },
});