StoreSection.js 6.83 KB
import { IMG_URL } from '~/utils/constantData'
import CustomerApi from '~/api/customerApi/index'
import dayjs from 'dayjs'
 
// 预计算安全的月份时间戳,避免初始 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: {
    navBarHeight: Number,
    searchObj: Object,
    userInfo: Object,
    personParams: Object
  },

  data: {
	searchKeyword:'',
    IMG_URL,
    // 门店数据看板(示例数据)
    boardMonthText: `${__NOW__.getMonth() + 1}月`,
    boardScopeText: '全部',
    boardMonthActive: true,
    boardScopeActive: false,

    // 月份选择器
    monthPickerShow: false,
    minMonthDate: __MIN_YM_TS__,
    maxMonthDate: __CUR_YM_TS__,
    monthPickerValue: __CUR_YM_TS__,
    pendingMonthValue: 0,
    // 列表与分页
    storeList: [],
    currentPage: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
    // 门店状态 TabBar
    listTabs: ['全部', '新店', '老店','关店'],
    statusIndex: 0
  },

  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 obj = {
		merchantName: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,
        storeType: statusIndex ? statusIndex : undefined,
        isSuperior: userInfo.loginRole == 1 && (personParams?.personId || searchObj?.salesStaffList?.value) ? 1 : 0,
        queryScoure: 3
      }
      return obj
    },
    getStatic() {
      const params = this.getParams()
      delete params.storeType
      wx.$api.showLoading()
      CustomerApi.getStoreStatic(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 })
    },

    // TabBar 点击(门店状态筛选)
    tabClick(e) {
      const index = typeof e?.detail === 'number' ? e.detail : 0
      this.setData({ statusIndex: index, storeList: [] }, () => {
        this.resetAndLoad(() => {
          this.triggerEvent('needScrollTop', this.data.initialTabTop)
        })
      })
    },

    // 列表加载逻辑
    resetAndLoad(cb) {
      this.setData({ currentPage: 1, hasMore: true, storeList: [] })
      this.loadMore(cb)
    },

    getMore() {
      this.loadMore()
    },

    async loadMore(cb) {
      let { hasMore } = 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.getStorePageList({ currentPage, pageSize, ...params })
        wx.$api.hideLoading()
        const nextList = (res.list || []).map(val => {
          val.salesUser = val.salePersonnelInfoDTOList?.map(v => { return v.personnelName })?.join('/')
          val.signedTime = val.signedTime ? dayjs(val.signedTime).format('YYYY-MM-DD') : ''
          val.expectedOperationDate = val.expectedOperationDate ? dayjs(val.expectedOperationDate).format('YYYY-MM-DD') : ''
          return val
        })
        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
        this.setData({
          storeList: this.data.storeList.concat(nextList),
          currentPage: currentPage + 1,
          hasMore,
          loading: false
        })
      } catch (err) {
        this.setData({ loading: false, hasMore: false })
      }
      cb && cb()
    },
    goDetail(e) {
      let { item } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/packageCustomer/pages/merchantDetail/merchantDetail?query=${encodeURIComponent(JSON.stringify(item))}`
      })
    }
  }
})