PotentialSection.js 7.54 KB
// pages/tab/customer/components/PotentialSection/PotentialSection.js
import { IMG_URL } from '~/utils/constantData'
import CustomerApi from '~/api/customerApi/index'

// 预计算安全的月份时间戳,避免初始 0 值
const __NOW__ = new Date()
const __CUR_YM_TS__ = new Date(__NOW__.getFullYear(), __NOW__.getMonth(), 1).getTime()
const __MIN_YM_TS__ = new Date(__NOW__.getFullYear(), __NOW__.getMonth() - 23, 1).getTime()

Component({
  options: {
    styleIsolation: 'shared'
  },
  properties: {
    showAddIcon: Boolean,
    navBarHeight: Number,
    searchObj: Object,
    userInfo: Object,
    personParams: Object
  },

  data: {
	searchKeyword:'',
    IMG_URL,
    // 列表与分页(保持原有结构)
    listTabs: ['全部', '跟进中', '已签约',  '未签约'],
    customerList: [],
    currentPage: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
    statusIndex: 0,
    boardMonthText: `${__NOW__.getMonth() + 1}月`,
    boardScopeText: '全部',
    dashboard: {},
    boardMonthActive: true,
    boardScopeActive: false,
    monthPickerShow: false,
    minMonthDate: __MIN_YM_TS__,
    maxMonthDate: __CUR_YM_TS__,
    monthPickerValue: __CUR_YM_TS__,
    // 新增:暂存用户正在选择的值,确认后写入
    pendingMonthValue: 0,

    // 保持原有的状态标签映射
    statusText: {
      1: { label: '跟进中', color: '#FF9D0A' },
      3: { label: '已签约', color: '#35C376' },
      2: { label: '未签约', color: '#F24724' },
      4: { label: '已绑定', color: '#347BFF' }
    }
  },

  lifetimes: {
    attached() {
    }
  },

  methods: {
	onSearchConfirm(e){
		let {value} = e.detail
        this.setData({
			searchKeyword:value
		},()=>{
			this.resetAndLoad(() => {
				this.triggerEvent('needScrollTop', this.data.initialTabTop)
			  })
		})
	},
    getPageData() {
      // 获取数据看板数据
      this.getStatic()
      // 获取潜客列表数据
      this.resetAndLoad()
    },
    getParams() {
      let { boardScopeActive, monthPickerValue, searchObj, statusIndex, userInfo, personParams } = this.data
      const statusMap = {
        0: 0, // 全部
        1: 1, // 待跟进
        2: 3, // 已签约
        3: 2, // 已绑定
        // 4: 2  // 未签约
      }
      const obj = {
		storeName:this.data.searchKeyword,
        isAll: boardScopeActive ? 1 : 0,
        year: boardScopeActive ? undefined : new Date(monthPickerValue).getFullYear(),
        month: boardScopeActive ? undefined : new Date(monthPickerValue).getMonth() + 1,
        mainBrandId: searchObj?.selectedMainBrand?.[0]?.id,
        subBrandId: searchObj?.selectedMainBrand?.[0]?.children[0]?.id,
        cooperationMode: searchObj?.cooperationModelList?.value,
        businessTypeList: searchObj?.businessTypeList?.map(val => { if (val.value) return val.value }),
        flowSituation: searchObj?.trafficStatusList?.value,
        provinceId: searchObj?.provinces?.value,
        salesPersonId: personParams?.personId || searchObj?.salesStaffList?.value,
        potentialCustomersStatus: statusIndex == 0 ? undefined : statusMap[statusIndex], // 状态
        isSuperior: userInfo.loginRole == 1 && (personParams?.personId || searchObj?.salesStaffList?.value) ? 1 : 0,
        queryScoure: 3
      }
      return obj
    },
    getStatic() {
      let params = this.getParams()
      delete params.potentialCustomersStatus
      wx.$api.showLoading()
      CustomerApi.getPotentialStatic(params).then(res => {
        wx.$api.hideLoading()
        this.setData({
          dashboard: res || {}
        }, () => {
          this.recordTabTop()
        })
      })
    },
    recordTabTop() {
      const query = wx.createSelectorQuery().in(this)
      query.select('.tabbar-anchor').boundingClientRect()
      query.exec(res => {
        const top = res[0]?.top
        const height = res[0]?.height
        this.setData({
          initialTabTop: top,
          tabHeight: height
        })
      })
    },
    chooseBoard(e) {
      const { type } = e.currentTarget.dataset
      if (type === 'month') {
        // 打开月份选择器
        this.openMonthPicker()
      } else {
        this.setData({ boardMonthActive: false, boardScopeActive: true }, () => {
          this.getPageData()
        })
      }
    },

    openMonthPicker() {
      this.setData({ monthPickerShow: true, pendingMonthValue: this.data.monthPickerValue })
    },
    closeMonthPicker() {
      this.setData({ monthPickerShow: false })
    },
    popupConfirm() {
      const val = this.data.pendingMonthValue || this.data.monthPickerValue
      if (val) {
        const d = new Date(val)
        this.setData({
          monthPickerValue: val,
          boardMonthText: `${d.getMonth() + 1}月`,
          boardMonthActive: true,
          boardScopeActive: false
        }, () => {
          this.getPageData()
        })
      }
      this.closeMonthPicker()
    },
    cancel() {
      this.closeMonthPicker()
    },

    onMonthChange(e) {
      const values = e.detail.getValues()
      const year = Number(String(values?.[0] || '').replace(/[年月日时分]/g, ''))
      const month = Number(String(values?.[1] || '').replace(/[年月日时分]/g, ''))
      if (!year || !month) return
      const ts = new Date(year, month - 1, 1).getTime()
      this.setData({ pendingMonthValue: ts })
    },
    tabClick(e) {
      const index = typeof e?.detail === 'number' ? e.detail : 0
      this.setData({ statusIndex: index, copyCustomerList: [] }, () => {
        this.resetAndLoad(() => {
          this.triggerEvent('needScrollTop', this.data.initialTabTop)
        })
      })
    },
    resetAndLoad(cb) {
      this.setData({ currentPage: 1, hasMore: true, copyCustomerList: [] })
      this.loadMore(cb)
    },

    getMore() {
      this.loadMore()
    },

    async loadMore(cb) {
      let { hasMore, copyCustomerList } = this.data
      if (!hasMore) return
      this.setData({ loading: true })
      try {
        const { currentPage, pageSize } = this.data
        const params = this.getParams()
        wx.$api.showLoading()
        const res = await CustomerApi.getPotentialList({ currentPage, pageSize, ...params })
        wx.$api.hideLoading()
        const nextList = res?.list || []
        const total = res?.total || res?.count || 0
        const pages = res?.pages || (total ? Math.ceil(total / pageSize) : null)
        const hasMore = pages != null ? currentPage < pages : nextList.length === pageSize
        const data = copyCustomerList.concat(nextList)
        this.setData({
          customerList: data,
          copyCustomerList: data,
          currentPage: currentPage + 1,
          hasMore,
          loading: false
        })
        cb && cb()
      } catch (err) {
        this.setData({ loading: false, hasMore: false })
      }
      
    },
    // 新增潜客
    jump(e) {
      let { item, name } = e.currentTarget.dataset
      if (name) { // 结果反馈
        wx.navigateTo({
          url: `/packageCustomer/pages/resultFeedback/resultFeedback?query=${encodeURIComponent(JSON.stringify(item))}`
        })
      } else { // 新增编辑潜客
        wx.navigateTo({
          url: item && Object.keys(item)?.length > 0 ? `/packageCustomer/pages/createPotential/createPotential?pageType=edit&query=${encodeURIComponent(JSON.stringify(item))}` : `/packageCustomer/pages/createPotential/createPotential?pageType=add`
        })
      }
    },
    // 潜客详情
    goDetail(e) {
      let { item } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/packageCustomer/pages/potentialDetail/potentialDetail?query=${encodeURIComponent(JSON.stringify(item))}`
      })
    }
  }
 })