StoreSelectPopup.js 5.73 KB
import { IMG_URL } from '~/utils/imageBase.js'
// components/StoreSelectPopup/StoreSelectPopup.js
import api from '~/api/api'
import { normalizeOrderMerchantOptions } from '~/components/orderPromptPop/orderMerchantContext.helpers'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function(n) {
        if (n) {
          this.setData({
            scrollTop: 0,
            pageNum: 1,
            nothing: false,
            searchValue: ''
          })
          this.getList(true)
        }
      }
    },
    type: Number,
    lowPriceMode: {
      type: Boolean,
      value: false
    },
    selectedData: Array
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    pageNum: 1,
    scrollTop: 0,
    nothing: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getList(isReset = false) {
      let { type, pageNum, searchValue, nothing, selectedData } = this.data
      if (this.properties.lowPriceMode) {
        this.getLowPriceStoreList(type, searchValue, selectedData)
        return
      }
      // type 1园区店数据 2 4s店数据
      let requestEvent = type == 1 ? 'getYqMerchantListPage' : 'getMerchantListPage'
      let params = {
        pageSize: 20,
        pageNum: isReset ? 1 : pageNum,
        scene: 1,
        merchantName: searchValue
      }
      if (nothing) return
      api[requestEvent](params).then(res => {
        let { code, data, message } = res
        if (code == 0) {
          if (isReset) data?.list.unshift({ merchantId: '', merchantName: '全部', active: true })
          let tempList = isReset ? data?.list : this.data.storeRangeList.concat(data?.list)
          if (selectedData?.length) {
            tempList[0].active = false
            tempList.forEach(val => {
              val.active = selectedData.some(v => v.merchantId == val.merchantId)
            })
          }
          this.setData({
            storeRangeList: tempList,
            pageNum: pageNum + 1,
            total: data?.total,
            nothing: data?.total == (tempList.length - 1),
          })
        } else {
          wx.$util.showToast(message)
        }
      })
    },
    getLowPriceStoreList(type, searchValue, selectedData) {
      api
        .getShopWithYqList({
          merchantName: searchValue || '',
          sceneType: 1
        })
        .then(res => {
          const { code, message } = res || {}
          if (code != 0) {
            wx.$util.showToast(message || '门店列表加载失败')
            return
          }
          const shopList = normalizeOrderMerchantOptions(res?.data)
          let list = shopList
          if (type == 1) {
            const seen = new Set()
            list = shopList.reduce((result, item) => {
              const key = String(item.yqMerchantId)
              if (seen.has(key)) return result
              seen.add(key)
              result.push({
                merchantId: item.yqMerchantId,
                merchantName: item.yqMerchantName
              })
              return result
            }, [])
          }
          list = list.map(item => ({
            ...item,
            active: (selectedData || []).some(
              selected => selected.merchantId == item.merchantId
            )
          }))
          const allItem = {
            merchantId: '',
            merchantName: '全部',
            active: !(selectedData || []).length
          }
          this.setData({
            storeRangeList: [allItem, ...list],
            pageNum: 2,
            total: list.length,
            nothing: true
          })
        })
    },
    scrollonReachBottom() {
      this.getList()
    },
    open() {
      if (this.data.disabled) return
      this.setData({
        edit: true,
        inputFocus: true
      })
    },
    setValue(value) {
      if (this.properties.filterEmoji) {
        if (wx.$util.haveEmoji(value)) value = wx.$util.replaceEmoji(value)
      }
      this.setData({
        searchValue: value
      })
    },
    input(e) {
      let { value } = e.detail
      this.setValue(value)
      this.triggerEvent('input', {
        value
      })
    },
    inputConfifm(e) {
      this.emit(e)
    },
    emit(e) {
      let {
        detail: { value },
        type
      } = e
      this.setData({
        edit: false,
        nothing: false,
        pageNum: 1
      })
      this.setValue(value)
      this.getList(true)
    },
    choose(e) {
      let { item, index } = e.currentTarget.dataset
      let { active } = item
      let { storeRangeList } = this.data
      if (index > 0) {
        let target = `storeRangeList[${index}].active`
        this.setData({
          [target]: !active,
          'storeRangeList[0].active': false
        }, () => {
          this.setSelectedData()
        })
      } else {
        if (!active) {
          storeRangeList.map(val => { return val.active = false })
          storeRangeList[0].active = true
        }
        this.setData({storeRangeList}, () => {
          this.setSelectedData()
        })
      }
    },
    setSelectedData() {
      let { storeRangeList, selectedData } = this.data
      let arr = []
      // 已选的未分页加载出的数据置为选中状态
      let selectedArr = wx.$util.arrayUnique(selectedData.concat(storeRangeList), 'merchantId')
      selectedArr.forEach(val => {
        if (val.active && val.merchantId) arr.push(val)
      })
      if (!arr?.length) storeRangeList[0].active = true
      this.setData({
        selectedData: storeRangeList[0]?.active ? [] : arr, // 选择全部清除已选的门店数据
        storeRangeList
      })
    },
    hidePopup() {
      this.triggerEvent('cancel')
    },
    confirm() {
      let {selectedData} = this.data
      this.triggerEvent('confirm', selectedData)
    }
  }
})