subShopMerchant.js 4.91 KB
import api from '~/api/api.js'

// components/subShopMerchant/subShopMerchant.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    title: String,
    selectedSelesMerchantId: {
      type: String,
      observer: function (n) {
        if (n) {
          this.getSubShopMerchantList(n)
        }
      }
    },
    selectedCustomerInfo: {
      type: String,
      observer: function (n) {
        // 选择客户 销售顾问所在门店无变化 需做校验
        let thisPage = wx.$router.getPageInstance()
        let { orderObj } = thisPage.data
        let { subShopMerchantList } = this.data
        if (n) {
          // 校验历史订单二级流量方是否在配置的二级流量方数据中包含 不包含清空二级流量方重新选择
          let hasSubShopMerchantId = subShopMerchantList.some(item=> item.subMerchantId == orderObj.subShopMerchantId)
          if (!hasSubShopMerchantId && subShopMerchantList.length) {
            delete orderObj?.subShopMerchantId
            delete orderObj?.subShopMerchantName
          }
          thisPage.setData({
            orderObj
          })
          this.setData({
            orderObj: {}
          })
        }
      }
    },
    selectedSubShopMerchantId: {
      type: String,
      observer: function () {
        let thisPage = wx.$router.getPageInstance()
        let { orderObj } = thisPage.data
        let { subShopMerchantList } = this.data
        subShopMerchantList.map(i => {
          i.active = false
          if (i.subMerchantId == orderObj.subShopMerchantId) {
            i.active = true
          }
          return i
        })
        this.setData({
          subShopMerchantList
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    subShopMerchantList: [],
    orderObj: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getSubShopMerchantList(id) {
      let thisPage = wx.$router.getPageInstance()
      let { requiredList, orderObj } = thisPage.data
      api.getSubShopMerchantList({merchantId: id}).then(res => {
        let {code, data} = res
        if (code == '0') {
          // 有二级流量方数据 添加必填二级流量方字段
          if (data?.length) {
            if (!requiredList?.includes('subShopMerchantId')) requiredList?.unshift('subShopMerchantId')
            data.map(i => {
              i.active = false
              if (i.subMerchantId == orderObj.subShopMerchantId) {
                i.active = true
              }
              return i
            })
            // 校验历史订单二级流量方是否在配置的二级流量方数据中包含 不包含清空二级流量方重新选择
            let hasSubShopMerchantId = data.some(item=> item.subMerchantId == orderObj.subShopMerchantId)
            if (!hasSubShopMerchantId) {
              delete orderObj?.subShopMerchantId
              delete orderObj?.subShopMerchantName
            }
            thisPage.setData({
              requiredList,
              isShowSubShopMerchant: true,
              orderObj
            })
            this.setData({
              subShopMerchantList: data,
              orderObj: {}
            })
          } else {
            // 无二级流量方字段时 清空选择的二级流量方字段
            delete orderObj?.subShopMerchantId
            delete orderObj?.subShopMerchantName
            if (requiredList?.includes('subShopMerchantId')) requiredList?.splice(requiredList?.indexOf('subShopMerchantId'), 1)
            thisPage.setData({
              isShowSubShopMerchant: false,
              requiredList,
              orderObj
            })
          }
        }
      })
    },
    changeMerchant(e) {
      let { index, item } = e.currentTarget.dataset
      let { subShopMerchantList, orderObj } = this.data
      let subShopMerchantId = item.subMerchantId
      if (subShopMerchantId == orderObj.subShopMerchantId) return
      let selectedSubShopMerchantObj = {
        subShopMerchantId: item.subMerchantId,
        subShopMerchantName: item.subMerchantName
      }
      orderObj = (orderObj || selectedSubShopMerchantObj) ? Object.assign(orderObj, selectedSubShopMerchantObj) : selectedSubShopMerchantObj
      subShopMerchantList.map(i => (i.active = false))
      subShopMerchantList[index].active = true
      this.setData({
        subShopMerchantList,
        orderObj
      })
    },
    /** 取消时通知父页面关闭弹框;内部 show 兼容未绑 cancel 的引用页 */
    onPopupCancel() {
      this.setData({ show: false })
      this.triggerEvent('cancel')
    },
    async confirm() {
      let data = this.data.orderObj
      let thisPage = wx.$router.getPageInstance()
      let { orderObj } = thisPage.data
      orderObj = (orderObj || data) ? Object.assign(orderObj, data) : orderObj
      thisPage.setData({
        orderObj
      })
      this.setData({
        show: false
      })
      this.triggerEvent('confirm', data)
    },
  }
})