CustomerInfo.js 5.16 KB
/**
 * @file 客户档案选择弹框:分页拉取工单客户列表,不调 setData 外的页面实例。
 */
import api from '~/api/api.js'
import {
  DEFAULT_PAGE_SIZE,
  canLoadMorePage,
  mapCustomerListWithCarType
} from './customerInfo.pagination.js'

Component({
  properties: {
    show: {
      type: Boolean,
      observer(n) {
        if (n) {
          wx.$util.pointRecord(19)
          this.init()
          return
        }
        this.resetList()
        this.setData({ orderInfo: {} })
      }
    },
    merchantId: {
      type: Number,
      observer(n) {
        if (n) {
          // 销售顾问切换门店时重新拉取顾客数据
          this.init()
        }
      }
    },
    apiType: Number
  },

  data: {
    orderInfo: {},
    customerList: [],
    pageNum: 1,
    total: 0,
    nothing: false,
    keyword: '',
    loading: false
  },

  pageLifetimes: {
    show() {},
    hide() {}
  },

  methods: {
    /** 阻止弹框列表滚到底后 touchmove 穿透到底层页面 */
    noScroll() {},

    /**
     * 首次打开 / 搜索 / 切换门店入口,重置后加载第一页
     * @param {string} [keyword] 搜索关键字
     */
    init(keyword) {
      const { apiType } = this.data
      if (apiType == 2) {
        this.loadCustomerFileList(keyword)
        return
      }
      this.resetList(keyword)
      this.loadList()
    },

    /**
     * 重置分页状态(搜索或重新筛选时)
     * @param {string} [keyword]
     */
    resetList(keyword = '') {
      this.setData({
        pageNum: 1,
        total: 0,
        nothing: false,
        keyword: keyword || '',
        customerList: []
      })
    },

    /**
     * 按当前 pageNum 请求一页并追加到 customerList
     */
    loadList() {
      const { merchantId, pageNum, keyword, loading } = this.data
      if (loading) return

      const userInfo = wx.$util.getUserInfo()
      const params = {
        merchantId: merchantId || userInfo?.shopMerchantId,
        pageNum,
        pageSize: 20,
        statusList: [1],
        keyword
      }

      this.setData({ loading: true })
      wx.$util.showLoading()

      api.getWorkPackageGoodsList(params).then((res) => {
        wx.$util.hideLoading()
        const { data, code } = res
        let { customerList, total } = this.data

        if (code == 0 && data) {
          total = data.total || 0
          const pageItems = mapCustomerListWithCarType(data.list || [])
          customerList = pageNum === 1 ? pageItems : customerList.concat(pageItems)
        }

        const nothing = !canLoadMorePage(total, pageNum, DEFAULT_PAGE_SIZE)

        this.setData({
          customerList,
          total,
          nothing,
          loading: false
        })
      }).catch(() => {
        wx.$util.hideLoading()
        this.setData({ loading: false })
      })
    },

    /**
     * apiType=2 时走客户档案独立接口,保持原全量逻辑
     * @param {string} [keyword]
     */
    loadCustomerFileList(keyword) {
      const { merchantId } = this.data
      const userInfo = wx.$util.getUserInfo()
      let customerList = []

      wx.$util.showLoading()
      api.getCustomerFileList({
        currentPage: 1,
        merchantId: merchantId || userInfo?.shopMerchantId,
        pageSize: 999999,
        search: keyword
      }).then(({ code, data }) => {
        wx.$util.hideLoading()
        if (code == 0 && data?.list?.length) {
          data.list.forEach((item) => {
            customerList.push({
              customer: item,
              carType: `${item.carBrand || ''}-${item.carChildsBrandName || ''}-${item.carSeriesName || ''}`
            })
          })
        }
        this.setData({ customerList })
      }).catch(() => {
        wx.$util.hideLoading()
      })
    },

    searchCar(e) {
      const { value } = e.detail
      this.init(value)
    },

    /** scroll-view 触底加载下一页 */
    scrolltolower() {
      const { total, pageNum, nothing, loading } = this.data
      if (loading || nothing) return

      if (canLoadMorePage(total, pageNum, DEFAULT_PAGE_SIZE)) {
        this.setData({ pageNum: pageNum + 1 }, () => this.loadList())
      } else {
        this.setData({ nothing: true })
      }
    },

    choose(e) {
      const { item, index } = e.currentTarget.dataset
      let { customerList } = this.data
      customerList = customerList.map((row) => {
        row.active = false
        return row
      })
      customerList[index].active = true
      const orderInfo = item
      orderInfo.orderSource = 4
      const deleteList = ['associateOrderCode', 'merchantDistributionList', 'expenseCategoryList', 'profitAllocatinList']
      for (let i = 0; i < deleteList.length; i++) {
        delete orderInfo[deleteList[i]]
      }
      this.setData({
        customerList,
        orderInfo
      })
    },

    /** 取消时通知父页面关闭,便于 page-meta 解锁 */
    onPopupCancel() {
      this.triggerEvent('cancel')
    },

    confirm() {
      const { orderInfo, customerList } = this.data
      const flag = customerList.some((item) => item.active)
      if (!flag) {
        return wx.$util.showToast('请选择客户')
      }

      this.triggerEvent('confirm', orderInfo)
    }
  }
})