potentialDetail.js 9.11 KB
import { IMG_URL, COOPERATIONMODELIST, TRSFFICSTATUSLIST, SCENELIST, ROLELIST } from '~/utils/constantData'
import CustomerApi from '~/api/customerApi/index'
import { getUserInfo } from '~/utils/cache/userInfo'
import dayjs from 'dayjs'
Page({
  data: {
    IMG_URL: IMG_URL,
    userInfo: {},
    navStyle: 'background: #fff;',
    showVisitButton: true,
    infoBoardItems: [], // 信息看板数据
    listTabs: ['拜访记录', '资料详情'],
    tabIndex: 0,
    scrollTop: 0, // 监听页面滚动位置
    initialTabTop: 0, // 初始tab的top值
    setScrollTop: 0, // scroll-view的滚动位置
    statusText: {
      1: {label: '跟进中', color: '#FF9D0A'},
      3: {label: '已签约', color: '#35C376'},
      2: {label: '未签约', color: '#F24724'},
      4: {label: '已绑定', color: '#347BFF'}
    },
    visitRecords: [],
    pageNum: 1,
    pageSize: 10,
    bgUrl: `${IMG_URL}/customer/nothing.png`,
    cardItemObj: [{
      label: '拜访时间',
      key: 'visitTime'
    }, {
      label: '联系人',
      key: 'contactPerson'
    }, {
      label: '拜访内容',
      key: 'visitContent'
    }, {
      label: '拜访签到',
      key: 'checkInLocation'
    }, {
      label: '拜访照片',
      key: 'visitPhotos',
      type: 'image'
    }],
    infoDetail: {}
  },

  onReady() {
    // 记录tab的位置 请求接口后再记录一次位置 数据加载 假数据
    this.recordTabTop()
  },

  onLoad(options) {
    let { query } = options
    if (query) {
      const pageQuery = JSON.parse(decodeURIComponent(query))
      this.setData({pageQuery})
    }
    this.setData({
      userInfo: getUserInfo()
    })
  },

  onShow() {
    // 获取潜客详情数据
    this.getPotentialDetail()
    // 初始加载数据
    this.resetAndLoad()
    this.measureLayout()
  },

  getPotentialDetail() {
    let { pageQuery } = this.data
    wx.$api.showLoading()
    if (!pageQuery.id) {
      wx.$sentry.reportParamError(
        '潜客详情参数异常: id',
        pageQuery
      )
    }
    CustomerApi.getCustomerData({id: pageQuery.id}).then(res => {
      wx.$api.hideLoading()
      this.setData({showPage: true})
      // 处理数据回显
      this.progressData(res)
    })
  },

  // 图片预览
  previewImage(e) {
    let { url } = e.currentTarget.dataset
    wx.previewImage({
      urls: [url] 
    })
  },

  progressData(data) {
    // 添加基础数据校验
    if (!data || typeof data !== 'object') {
      wx.$sentry.reportError(
        '潜客详情数据异常: data',
        data
      )
      this.setData({
        infoDetail: {}
      })
      return
    }
  
    try {
      // 主营品牌
      data.mainCategory = ''
      if (Array.isArray(data?.salePotentialCustomerBrandDTOList)) {
        data.mainCategory = data.salePotentialCustomerBrandDTOList.map(val => {
          try {
            if (!val || typeof val !== 'object') return ''
            const mainBrand = val.mainBrandName || ''
            const subBrand = val.subBrandName || ''
            return mainBrand || subBrand ? `${mainBrand}${subBrand}` : ''
          } catch (error) {
            wx.$sentry.reportError(
              '主营品牌数据异常: data.salePotentialCustomerBrandDTOList',
              data.salePotentialCustomerBrandDTOList
            )
            return ''
          }
        }).filter(item => item) // 过滤空字符串
        .join('/') || ''
      }
      // 合作模式
      data.coopMode = ''
      if (data.cooperationMode !== undefined && data.cooperationMode !== null) {
        try {
          data.coopMode = COOPERATIONMODELIST?.find(v => v?.value == data.cooperationMode)?.name || ''
        } catch (error) {
          wx.$sentry.reportError(
            '合作模式数据异常: data.cooperationMode',
            data.cooperationMode
          )
        }
      }
      // 流量情况
      data.flowScale = ''
      if (data.flowSituation !== undefined && data.flowSituation !== null) {
        try {
          data.flowScale = TRSFFICSTATUSLIST?.find(val => val?.value == data.flowSituation)?.name || ''
        } catch (error) {
          wx.$sentry.reportError(
            '流量情况数据异常: data.flowSituation',
            data.flowSituation
          )
        }
      }
      // 业务类型
      data.bizType = ''
      if (Array.isArray(data?.businessTypeList)) {
        data.bizType = data.businessTypeList.map(val => {
          try {
            const sceneItem = SCENELIST?.find(v => v?.value == val)
            return sceneItem?.name || ''
          } catch (error) {
            wx.$sentry.reportError(
              '业务类型数据异常: data.businessTypeList',
              data.businessTypeList
            )
            return ''
          }
        }).filter(item => item) // 过滤空字符串
        .join('/') || ''
      }
      // 人员类型
      if (Array.isArray(data?.salePotentialCustomerPersonnelInfoDTOList)) {
        data.salePotentialCustomerPersonnelInfoDTOList.forEach(val => {
          try {
            if (val && typeof val === 'object') {
              val.roleName = ROLELIST?.find(v => v?.value == val.personnelType)?.label || ''
            }
          } catch (error) {
            wx.$sentry.reportError(
              '人员信息数据异常: data.salePotentialCustomerPersonnelInfoDTOList',
              data.salePotentialCustomerPersonnelInfoDTOList
            )
            val.roleName = ''
          }
        })
      }
      // 店面门头照片
      data.storePhotoList = []
      if (typeof data.storePhoto === 'string') {
        try {
          data.storePhotoList = data.storePhoto.split(',')
        } catch (error) {
          wx.$sentry.reportError(
            '店面照片数据异常: data.storePhoto',
            data.storePhoto
          )
        }
      }
  
      // 设置数据,添加错误处理
      this.setData({
        infoDetail: data
      }, () => {
        try {
          this.buildInfoBoard && this.buildInfoBoard()
          this.recordTabTop && this.recordTabTop()
        } catch (error) {
          console.error('执行回调函数时出错:', error)
        }
      })
  
    } catch (error) {
      wx.$sentry.reportParamError(
        '潜客详情数据异常: data',
        data
      )
      // 确保即使出错也有基本的数据结构
      this.setData({
        infoDetail: {
          ...data,
          mainCategory: '',
          coopMode: '',
          flowScale: '',
          bizType: '',
          salePotentialCustomerPersonnelInfoDTOList: [],
          storePhotoList: []
        }
      })
    }
  },

  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
      })
    })
  },

  buildInfoBoard() {
    const { infoDetail } = this.data
    const items = [
      { label: '主营品牌', value: infoDetail.mainCategory },
      { label: '合作模式', value: infoDetail.coopMode },
      { label: '拜访次数', value: infoDetail.visitCount != null ? `${infoDetail.visitCount}` : '', highlight: true },
      { label: '流量情况', value: infoDetail.flowScale },
      [3, 4].includes(infoDetail.potentialCustomersStatus) ? { label: '预计展业时间', value: infoDetail.expectedOperationDate ? dayjs(infoDetail.expectedOperationDate).format('YYYY-MM-DD') : '' } : null, // 已签约或已绑定展示
      [3, 4].includes(infoDetail.potentialCustomersStatus) ? { label: '分润规则', value: infoDetail.profitRule } : null, // 已签约或已绑定展示
      { label: '业务类型', value: infoDetail.bizType, span: 2, full: [3, 4].includes(infoDetail.potentialCustomersStatus), clampLines: 1, threshold: 16 },
      [3, 4].includes(infoDetail.potentialCustomersStatus) ? { label: '上架商品', value: infoDetail.productShelfInfo, full: true } : null
    ].filter(Boolean)
    this.setData({ infoBoardItems: items })
  },

  // 加载逻辑
  resetAndLoad() {
    let { pageQuery } = this.data
    if (!pageQuery.id) {
      wx.$sentry.reportParamError(
        '拜访记录参数异常: id',
        pageQuery
      )
    }
    CustomerApi.getVisitList({id: pageQuery.id}).then(res => {
      this.setData({
        visitRecords: res || []
      })
    })
  },

  navbarReady(e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
  },

  tabClick(e) {
    const index = typeof e?.detail === 'number' ? e.detail : 0
    this.setData({ 
      tabIndex: index
    })
    if (index == 0) this.resetAndLoad()
    wx.nextTick(() => { this.measureLayout() })
  },



  onScroll(e) {
    const scrollTop = e.detail.scrollTop
    this.setData({ scrollTop })
  },

  measureLayout(cb) {
    const { scrollTop, initialTabTop, navBarHeight } = this.data
    if (scrollTop >= (initialTabTop - navBarHeight)) {
      this.setData({
        setScrollTop: (initialTabTop - navBarHeight)
      })
    }
  },

  onCreateVisit() {
    let { pageQuery } = this.data
    wx.navigateTo({
      url: `/packageCustomer/pages/createVisit/createVisit?id=${pageQuery.id}`
    })
  }
});