SideSelectByPagePopup.js 10.8 KB
// components/SideSelectPopup/SideSelectPopup.js
import api from '~/api/api'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: function (n) {
        if (n) {
          let userInfo = wx.$util.getUserInfo() || {}
          this.setData({
            userInfo,
            searchValue: '',
            edit: false,
            customSearchFlag: false,
            showSelect: false,
            nothing: false,
            pageNum: 1
          })
          this.searchEvent(true)
        }
      }
    },
    hasTabBar: {
      type: Boolean,
      value: false
    },
    storeList: {
      type: Array,
      value: []
    },
    subIndex: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    storagesceneType: '',
    IMG_URL,
    userInfo: {},
    inputFocus: false,
    searchValue: '',
    selectedMerchantId: '',
    searchSelectData: ['搜门店', '搜客户'],
    searchSelectIndex: 0,
    showSelect: false,
    customStoreList: [],
    statusText: ['待跟进', '已升级', '战败', '待支付'],
    customSearchFlag: false,
    pageNum: 1,
    scrollTop: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closeMenu() {
      this.setData({
        show: false
      })
    },
    // 查询条件类型切换
    selectTap() {
      this.setData({
        showSelect: !this.data.showSelect
      });
    },
    optionTap(e) {
      let { index } = e.currentTarget.dataset
      this.setData({
        searchSelectIndex: index,
        showSelect: !this.data.showSelect,
        searchValue: '',
        nothing: false,
        pageNum: 1
      })
      this.searchEvent(true)
    },
    closeSelect() {
      this.setData({
        showSelect: false
      })
    },
    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.searchEvent(true)
    },
    clearInputText() {
      this.setData({
        searchValue: '',
        edit: false,
        nothing: false,
        pageNum: 1
      })
      this.searchEvent(true)
    },
    scrollonReachBottom() {
      this.searchEvent()
    },
    uniqueByProperty(arr, prop) {
      const map = new Map();
      arr.forEach(obj => {
        map.set(obj[prop], obj);
      });
      return Array.from(map.values());
    },
    searchEvent(isReset = false) {
      let { searchValue, selectedMerchantId, searchSelectIndex, pageNum, nothing } = this.data
      let userInfo = wx.$util.getUserInfo()
      this.setData({
        customSearchFlag: false
      })
      if (searchSelectIndex == 0) { // 搜门店
        let userInfo = wx.$util.getUserInfo()
        api.getCurShopList({
          pageNum: pageNum,
          pageSize: 20,
          merchantNameOrId: searchValue,
          sceneType: userInfo?.sceneType
        }).then(res => {
          let { data } = res
          let tempList = isReset ? data?.list : [...this.data.storeRangeList, ...data?.list]
          if (!searchValue) {
            if (!tempList.some(v => v.merchantName === '全部')) {
              tempList.unshift({ merchantName: '全部' })
            }
            if (isReset) this.setData({ scrollTop: 0 })
            if (!tempList.some(v => v.merchantId === userInfo?.shopMerchantId) && userInfo?.shopMerchantId) {
              tempList.push({ merchantId: userInfo?.shopMerchantId, merchantName: userInfo?.shopMerchantName });
            }
          }
          tempList = this.uniqueByProperty(tempList, 'merchantId')
          tempList.forEach(v => v.actived = (userInfo?.shopMerchantId === v.merchantId) || (!userInfo?.shopMerchantId && !v.merchantId))
          this.setData({
            storeRangeList: tempList,
            pageNum: pageNum + 1,
            total: data?.total,
            nothing: data?.total == tempList.length,
          })
        })
      } else { // 搜客户
        let params = {
          pageNum: pageNum,
          pageSize: 10,
          keyword: searchValue,
          sellerMerchantId: userInfo?.isSuper || userInfo?.loginRole == 3 || userInfo.loginRole == 5 ? '' : (userInfo?.merchantId || '')
        }
        if (nothing) return
        api.searchOrderCustomer(params).then(res => {
          let { code, data, message } = res
          if (code == 0) {
            data?.list.forEach(val => {
              // upgradeFlag Y已升级 N已战败
              // listStatus 0待跟进 1已升级 2战败 3待支付
              val.listStatus = val.upgradeFlag == 'Y' ? 1 : val.upgradeFlag == 'N' ? 2 : (!val.upgradeFlag && val.status == 1) ? 0 : (!val.upgradeFlag && val.status == 2) ? 3 : ''
              val.merchantId = userInfo?.isSuper ? val.sellerMerchantId : val.merchantId
              val.merchantName = userInfo?.isSuper ? val.sellerMerchantName : val.merchantName
            })
            let tempList = isReset ? data?.list : this.data.customStoreList.concat(data?.list)
            this.setData({
              customStoreList: tempList,
              pageNum: pageNum + 1,
              total: data?.total,
              nothing: data?.total == tempList.length,
            })
          } else {
            wx.$util.showToast(message)
          }
          this.setData({
            customSearchFlag: true
          })
        })
      }
    },
    showTabBar() {
      if (this.data.hasTabBar) wx.showTabBar({ animation: true })
    },
    hideTabBar() {
      if (this.data.hasTabBar) wx.hideTabBar({ animation: true })
    },
    selectStore(e) {
      let { item } = e.currentTarget.dataset
      if (item.actived) return this.closeMenu()
      let { userInfo, storeRangeList } = this.data
      // 切换4s店
      wx.$util.showLoading()
      this.closeMenu()
      api.selectMerchantScene({
        merchantId: item?.merchantId,
        merchantName: item?.merchantName,
        sceneType: userInfo?.sceneType
      })
        .then(res => {
          let { data, message } = res
          if (data) {
            wx.$util.setUserInfo({ ...wx.$util.getUserInfo(), subLoginUser: data.subLoginUser })
            wx.$util.hideLoading()
            this.eventCallback(item, userInfo?.sceneType, false, data)
          } else {
            wx.$util.showToast(message)
          }
          let now = new Date()
          api.loginRecordSave({
            staffUuid: userInfo.uuid,
            loginTime: now.format('yyyy-MM-dd hh:mm:ss'),
            merchantId: item?.merchantId,
            merchantName: item?.merchantName
          }, 'sidepop')
        })
    },
    eventCallback(item, sceneType, isSuper, data) {
      const thisPage = wx.$router.getPageInstance()
      let { searchSelectIndex } = this.data
      let userInfo = wx.$util.getUserInfo()
      if (userInfo.merchantAccountList && userInfo.merchantAccountList.length) {
        try {
          userInfo.merchantAccountList.map(e => {
            if (e == 6 || e == 8) throw true
          })
        } catch (error) {
          userInfo.merchantType = error
        }
      }
      let obj = {}
      if (isSuper) {
        switch (sceneType) {
          case 0:
            obj = {
              ...userInfo,
              shopMerchantName: item.merchantName,
              shopMerchantId: item.merchantId,
              default4SStoreInfo: {
                merchantId: item.merchantId,
                merchantName: item.merchantName
              }
            }
            wx.$util.setUserInfo(obj)
            thisPage.setData({
              userInfo: obj
            })
            break
          case 2:
            obj = {
              ...userInfo,
              shopMerchantName: item.merchantName,
              shopMerchantId: item.merchantId,
              default4SStoreInfo_newCarConversion: {
                merchantId: item.merchantId,
                merchantName: item.merchantName
              }
            }
            wx.$util.setUserInfo(obj)
            thisPage.setData({
              userInfo: obj
            })
            break
          case 3:
            obj = {
              ...userInfo,
              shopMerchantName: item.merchantName,
              shopMerchantId: item.merchantId,
              default4SStoreInfo_crowdFunding: {
                merchantId: item.merchantId,
                merchantName: item.merchantName
              }
            }
            wx.$util.setUserInfo(obj)
            thisPage.setData({
              userInfo: obj
            })
            break
        }
      } else {
        obj = {
          ...userInfo,
          shopMerchantName: item.merchantName,
          shopMerchantId: item.merchantId,
          default4SStoreInfo: {
            merchantId: item.merchantId,
            merchantName: item.merchantName
          },
          default4SStoreInfo_newCarConversion: {
            merchantId: item.merchantId,
            merchantName: item.merchantName
          },
          default4SStoreInfo_crowdFunding: {
            merchantId: item.merchantId,
            merchantName: item.merchantName
          },
          loginRoleOnSelectMerchant: data.loginRoleOnSelectMerchant
        }
        wx.$util.setUserInfo(obj)
        thisPage.setData({
          userInfo: obj
        })
      }
      wx.$receptionLoaded = false // 切换门店后刷新首页接待记录数据
      wx.$util.refreshPageData()
      wx.__HAS_SHOW_MODAL = false
      // 除精品升级外 切换门店刷新数据
      thisPage.initList && thisPage.initList()
      // 精品升级首页数据刷新
      thisPage.initData && thisPage.initData()
      // 搜客户
      if (searchSelectIndex == 1) {
        this.goPage(item)
      }
    },
    // 跳转到工单详情/预约详情
    goPage(item) {
      // status 1待跟进(有inviteRecordId跳转预约详情 微信支付跳转工单确认页面)3待支付(跳转工单确认页面)constructionFlag(已施工页面)
      let pageName = (item.status == 1 && item?.inviteRecordId) ? 'tabDate.detail' : (item.status == 3 || (item.status == 1 && item.selectPayType == 1)) ? 'tabWorkOrder.confirmOrder' : item.constructionFlag == 'Y' && item.status != 10 ? 'tabWorkOrder.constructionOrderDetail' : 'tabWorkOrder.orderDetail'
      wx.$router.go(`${pageName}?id=${item?.inviteRecordId}&orderCode=${item?.orderCode}`)
    }
  }
})