customer.js 8.27 KB
// pages/tab/customer/customer.js
import { IMG_URL } from '~/utils/constantData'
import { getUserInfo } from '~/utils/cache/userInfo'
const navStyleTransparent = 'background-color: transparent;'
import reportPageTrackMixin from '~/mixins/reportPageTrackMixin'
const app = getApp()
Page({
  behaviors: [reportPageTrackMixin],
  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    navStyle: navStyleTransparent,
    userInfo: {},
    tabList: [{
      id: 1,
      name: '潜客'
    }, {
      id: 2,
      name: '门店'
    }],
    myTabIdx: 0,
    scrollIntoView: '',
    selectedScreenPotential: [{
      label: '主营品牌',
      valueText: '',
      key: 'selectedMainBrand'
    }, {
      label: '业务类型',
      valueText: '',
      key: 'businessTypeList'
    }, {
      label: '合作模式',
      valueText: '',
      key: 'cooperationModelList'
    }, {
      label: '流量情况',
      valueText: '',
      key: 'trafficStatusList'
    }, {
      label: '所属省份',
      valueText: '',
      key: 'provinces'
    }, {
      label: '销售人员',
      valueText: '',
      key: 'salesStaffList'
    }],
    selectedScreenStore: [{
      label: '主营品牌',
      valueText: '',
      key: 'selectedMainBrand'
    }, {
      label: '业务类型',
      valueText: '',
      key: 'businessTypeList'
    }, {
      label: '合作模式',
      valueText: '',
      key: 'cooperationModelList'
    }, {
      label: '流量情况',
      valueText: '',
      key: 'trafficStatusList'
    }, {
      label: '所属省份',
      valueText: '',
      key: 'provinces'
    }, {
      label: '销售人员',
      valueText: '',
      key: 'salesStaffList'
    }],
    pendingMonthValue: null,
    showAddIcon: true,
    navBarHeight: 0,
    navigationBarHeight: 0,
    scrollTop: 0,
    setScrollTop: 0,
    track_event_code: 'sales_customer_view_page',
    personParams: {} // 总监通过销售人员进入参数
  },
  // 总监有查看某个员工的潜客数据,手动触发时,将员工过滤条件清除
  onTabItemTap(e) {
    // 点击埋点
    wx.$track.setTrackData(0, 'sales_customer_page', {}, '')
    // 重新加载数据
    this.setData({
      myTabIdx: 0,
      scrollTop: 0,
      setScrollTop: 0,
      personParams: {}
    }, () => {
      this.onShow()
    })
  },

  // 导航栏就绪,记录高度并进行一次测量
  navbarReady(e) {
    const {height, navigationBarHeight} = e.detail
    this.setData({ 
      navBarHeight: height, 
      navigationBarHeight
    })
  },

  onReady() {
    const query = wx.createSelectorQuery().in(this)
    query.select('.fixed-header').boundingClientRect()
    query.exec(res => {
      const height = res[0]?.height
      this.setData({
        fixedHeight: height
      })
    })
  },
  onLoad(options) {
    this.setData({
      userInfo: getUserInfo()
    })
  },
  onShow() {
    // 总监从销售小结跳入
    const obj = app.getTabParams('customerObj')?.itemParams || {}
    if (Object.keys(obj)?.length) {
      const data = {name: obj.personName, value: obj.personId}
      this.setData({
        personParams: obj,
        'searchObj.salesStaffList': data, // 将带入的销售人员选中
        'storeSearchObj.salesStaffList': data // 将带入的销售人员选中
      }, () => {
        this.search()
      })
      return
    }
    // 从我的门店待跟进看板跳入
    const params = app.getTabParams('customer')
    let myTabIdx
    if (params) {
      this.setData({myTabIdx: Number(params.type)})
      myTabIdx = Number(params.type)
    } else {
      myTabIdx = this.data.myTabIdx
    }
    const component = myTabIdx == 0 ? 'potentialSection' : 'storeSection'
    this.setData({
      setScrollTop: 0
    })
    const coms = this.selectComponent(`#${component}`)
    if (coms.getPageData) coms.getPageData()
    this.onPageShow()
  },
  getMore() {
    let { myTabIdx } = this.data
    const component = myTabIdx == 0 ? 'potentialSection' : 'storeSection'
    const coms = this.selectComponent(`#${component}`)
    if (coms.getMore) coms.getMore()
  },
  onPageScroll(e) {
    const scrollTop = e.detail.scrollTop
    // 悬浮按钮:滚动中收起,停止后展开
    if (this._fabTimer) clearTimeout(this._fabTimer)
    this.setData({ showAddIcon: false, scrollTop })
    this._fabTimer = setTimeout(() => {
      this.setData({ showAddIcon: true })
    }, 120)
  },

  scrollToListTop(e) {
    let { scrollTop, navBarHeight, fixedHeight } = this.data
    wx.nextTick(() => {
      setTimeout(() => {
        let setScrollTop = e.detail - navBarHeight - fixedHeight
        if (scrollTop >= setScrollTop) {
          this.setData({
            setScrollTop: setScrollTop > 0 ? setScrollTop : 0
          })
        }
      }, 200)
    })
  },

  // 触底加载转发到当前组件
  onReachBottom() {
    const comp = this.data.myTabIdx === 0 ? this.selectComponent('#potentialSection') : this.selectComponent('#storeSection')
    comp && comp.loadMore && comp.loadMore()
  },

  // 筛选页面返回
  back() {
    this.setData({
      show_confirm: false,
    })
    wx.showTabBar()
  },

  // 筛选页面确认
  search() {
    // 当有页面带入参数时,判断再次选中的销售人员是否与带入相同,不同时清掉数据
    if ((Object.keys(this.data.personParams)?.length && this.data.searchObj?.salesStaffList?.value != this.data.personParams.personId) || (Object.keys(this.data.personParams)?.length && this.data.storeSearchObj?.salesStaffList?.value != this.data.personParams.personId)) {
      this.setData({
        personParams: {}
      })
    }
    const { selectedScreenPotential, selectedScreenStore, searchObj, storeSearchObj, myTabIdx } = this.data
    const sourceList = myTabIdx == 0 ? selectedScreenPotential : selectedScreenStore
    const valueObj = myTabIdx == 0 ? (searchObj || {}) : (storeSearchObj || {})
    const nextSelected = (sourceList || []).map(item => {
      const key = item.key
      const val = valueObj[key]
      let text = ''
      if (val) {
        if (key === 'businessTypeList') {
          // 业务类型为多选;当为“全部”或空时不回显
          if (Array.isArray(val)) {
            const filtered = val.filter(v => !(v?.name === '全部' || v?.value === ''))
            text = filtered.length ? filtered.map(v => v.name).join('/') : ''
          }
        } else if (key === 'selectedMainBrand') {
          // 主营品牌为数组;为空不回显
          text = Array.isArray(val)
            ? val.map(v => (`${v.name}${v?.children?.[0]?.name}`))
                .filter(Boolean)
                .join('/')
            : ''
        } else {
          // 单选项;选择“全部”或空值不回显
          text = (val?.name === '全部' || val?.value === '') ? '' : (val?.name || '')
        }
      }
      return { ...item, valueText: text }
    })
    if (Object.keys(this.data.personParams)?.length) {
      this.setData({
        selectedScreenPotential: nextSelected,
        selectedScreenStore: nextSelected
      })
    }
    if (myTabIdx == 0) {
      this.setData({ selectedScreenPotential: nextSelected, scrollTop: 0, setScrollTop: 0 }, () => {
        const potential = this.selectComponent('#potentialSection')
        if (potential) potential.getPageData()
      })
    } else {
      this.setData({ selectedScreenStore: nextSelected, scrollTop: 0, setScrollTop: 0 }, () => {
        const storeSection = this.selectComponent('#storeSection')
        if (storeSection) storeSection.getPageData()
      })
    }
  },

  // 选择筛选条件
  changeScreen() {
    this.setData({
      showRendering: true,
      show_confirm: true,
    });
    wx.hideTabBar()
  },

  createPotential() {
    wx.navigateTo({ url: '/packageCustomer/pages/createPotential/createPotential' })
  },

  changeMyTab(e) {
    const index = e?.currentTarget?.dataset?.id
    const nextIdx = typeof index === 'number' ? index : 0
    if (nextIdx === this.data.myTabIdx) return
    this.setData({ myTabIdx: nextIdx, scrollTop: 0, setScrollTop: 0 })
    wx.nextTick(() => {
      this.setData({ scrollTop: 0, setScrollTop: 0, scrollLeft: 0 })
      const component = nextIdx == 0 ? 'potentialSection' : 'storeSection'
      const coms = this.selectComponent(`#${component}`)
      if (coms.getPageData) coms.getPageData()
    })
  },
  onHide() {
    this.onPageHide()
  },
  onUnload() {
    this.onPageHide()
  }
})