SetMeal.js 7.49 KB
// components/SetMeal/SetMeal.js
import api from "../../tabWorkOrder/api";
import NP from "number-precision";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) this.getMerchantPackageGoods();
      },
    },
    chooseGoodsList: {
      type: Array,
      observer: function (n) {
        console.log(n);
        if (n) this.filterData(n);
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsData: [],
    selectData: [],
    matchPackageName: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    filterData() {
      let {
        selectData,
        chooseGoodsList
      } = this.data;
      if (chooseGoodsList.length) {
        selectData = chooseGoodsList.filter((s) => {
          if (s.isType) {
            return s;
          }
        });
        this.setData({
          selectData,
        });
      }
    },
    choose(e) {
      let {
        goodsData
      } = this.data, {
        index
      } = e.currentTarget.dataset;
      let thisPage = wx.$router.getPageInstance();
      goodsData[index].active = !goodsData[index].active;
      this.setData({
          goodsData,
        },
        () => {
          this.handleSelectData();
        }
      );
    },
    addCount(e) {
      let {
        index,
        count,
        item
      } = e.currentTarget.dataset;
      let target = `goodsData[${index}].count`;
      if (!item.count) item.count = 1;
      item.count += count;
      if (item.count < 1) item.count = 1;
      if (item.count > 999) item.count = 999;
      let total = item.price * item.count;
      item.totalPrice = wx.$util.decimal(total, 3);
      this.setData({
          [target]: item.count,
          [`goodsData[${index}].totalPrice`]: item.totalPrice,
        },
        () => {
          this.handleSelectData();
        }
      );
    },
    input(e) {
      let {
        goodsData
      } = this.data;
      let {
        index
      } = e.currentTarget.dataset;
      let target = `goodsData[${index}].count`;
      let {
        value
      } = e.detail;
      if (value < 1) value = 1;
      if (value > 999) value = 999;
      let total = wx.$util.decimal(goodsData[index].price * value, 2);
      this.setData({
          [target]: Number(value),
          [`goodsData[${index}].totalPrice`]: total,
        },
        () => {
          this.handleSelectData();
        }
      );
    },
    handleSelectData() {
      let {
        selectData,
        goodsData,
        matchPackageName
      } = this.data;
      if (!matchPackageName) {
        selectData = goodsData.filter((item) => {
          if (item.active) {
            return item;
          }
        });
      } else {
        if (selectData.length === 0) {
          selectData = goodsData.filter((item) => {
            if (item.active) {
              return item;
            }
          });
        } else {
          goodsData.forEach((item, index) => {
            if (item.active) {
              if (!selectData.find((s) => s.packageCode == item.packageCode)) {
                selectData.push(item);
              }
            } else {
              if (selectData.find((s) => s.packageCode == item.packageCode)) {
                selectData.splice(index, 1);
              }
            }
          });
        }
      }
      let selectDataTotal = selectData.reduce((prev, cur) => {
        prev = NP.plus(prev + cur.count);
        return prev;
      }, 0);
      console.log("selectData", selectData);
      this.setData({
        selectData,
        selectDataTotal,
      });
    },
    getMerchantPackageGoods() {
      let userInfo = wx.$util.getUserInfo();
      let merchantId, params;
      if (!userInfo.isSuper) {
        merchantId = userInfo?.shopMerchantId;
      }
      params = {
        merchantId,
        matchPackageName: this.data.matchPackageName,
      };
      try {
        api.getGoodsList(params).then((res) => {
          if (res.code == 0) {
            res.data.forEach((item) => {
              item.active = false;
              item.goodsList.forEach((g) => {
                g.price = g.sellPrice;
              });
              item.price = item.goodsList.reduce((prev, cur) => {
                prev = NP.plus(prev + cur.price);
                return prev;
              }, 0);
              item.upgradePrice = item.goodsList.reduce((prev, cur) => {
                // console.log("cur.price ", cur.price);
                prev = NP.plus(prev + cur.price);
                // console.log("prev ", prev);
                return prev;
              }, 0);
              // console.log(NP.plus(0.1, 0.2));
              item.price = NP.round(item.price, 2);
              item.upgradePrice = NP.round(item.upgradePrice, 2);
              // console.log("item.upgradePrice ", item.upgradePrice);
              item.count = 1;
              let total = NP.times(item.price, item.count);
              item.totalPrice = wx.$util.decimal(total, 2);
              item.isType = "meal";
            });
            this.setData({
                goodsData: res.data,
              },
              () => {
                this.setGoodsActive();
              }
            );
          }
        });
      } catch (err) {
        console.log(err);
        new Error(err);
      }
    },
    setGoodsActive() {
      let {
        chooseGoodsList,
        goodsData,
        selectData
      } = this.data;
      if (selectData.length == 0) {
        goodsData.map((e) => {
          e.active = false;
        });
      } else {
        goodsData.map((e, index) => {
          e.active = false;
          selectData.map((s) => {
            if (s.packageCode == e.packageCode) {
              Object.assign(e, s);
            }
          });
        });
      }
      this.setData({
          goodsData,
        },
        () => {
          this.handleSelectData();
        }
      );
    },
    searchGoods(e) {
      let {
        value
      } = e.detail;
      this.data.matchPackageName = value;
      this.getMerchantPackageGoods();
    },
    getChooseList(selectData, alreadyData) {
      let arr = [];
      // 遍历array1,将对象以id为键存入Map
      arr = alreadyData.filter(item => {
        if (!item.isType) {
          return item
        }
      })
      // map.set(item.id || item.packageCode, item);

      return arr.concat(selectData)
      // selectData.forEach((item) => {
      //   if (map.has(item.packageCode)) {
      //     map.set(
      //       item.packageCode,
      //       Object.assign(item, {
      //         price: map.get(item.packageCode).price,
      //       })
      //     );
      //   } else {
      //     map.set(item.packageCode, item);
      //   }
      // });
      // return Array.from(map.values());
    },
    confirm() {
      let {
        chooseGoodsList,
        selectData
      } = this.data;
      let chooseList = this.getChooseList(selectData, chooseGoodsList);
      let thisPage = wx.$router.getPageInstance();
      if (!selectData.length) return wx.$util.showToast("请勾选套餐");
      this.setData({
        show: false,
      });
      thisPage.setData({
        packageBox: false,
      });
      thisPage?.setGoodsTotal && thisPage.setGoodsTotal(chooseList);
      thisPage.setData({
        goodsList: chooseList,
      });
      this.triggerEvent("confirm", chooseList);
    },
    cancel() {
      let thisPage = wx.$router.getPageInstance();
      this.setData({
        show: false,
      });
      thisPage.setData({
        packageBox: false,
      });
    },
  },
});