StoreMessage.js 4.66 KB
// packageCustomer/components/StoreMessage/StoreMessage.js
import CustomerApi from '~/api/customerApi/index'
import { IMG_URL } from '~/utils/constantData'
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    merchantId: Number
  },

  data: {
    IMG_URL,
    activeNames: [],
    // 自定义折叠展开状态
    showBusiness: false,
    showBottom: false,
    // 动画对象
    businessAnimation: {},
    bottomAnimation: {},
    infoData: {},
    showPage: false
  },

  lifetimes: {
    attached() {
      this.getDetailData()
    }
  },

  methods: {
    // 兼容之前 collapse 的回调(不再使用)
    onCollapseChange(e) {
      this.setData({ activeNames: e.detail })
    },
    // 切换展业情况展开
    toggleBusiness(e) {
      const has = Number(e?.currentTarget?.dataset?.has || 0)
      if (!has) return
      
      const isExpanding = !this.data.showBusiness
      this.setData({ showBusiness: isExpanding })
      
      if (isExpanding) {
        this.expandPanel('business')
      } else {
        this.collapsePanel('business')
      }
    },
    // 切换兜底预警展开
    toggleBottom(e) {
      const has = Number(e?.currentTarget?.dataset?.has || 0)
      if (!has) return
      
      const isExpanding = !this.data.showBottom
      this.setData({ showBottom: isExpanding })
      
      if (isExpanding) {
        this.expandPanel('bottom')
      } else {
        this.collapsePanel('bottom')
      }
    },
    getDetailData() {
      let { merchantId } = this.data
      wx.$api.showLoading()
      if (!merchantId) {
        wx.$sentry.reportParamError(
          '门店信息参数异常: merchantId',
          merchantId
        )
      }
      CustomerApi.getStoreOverviewData({id: merchantId}).then(res => {
        wx.$api.hideLoading()
        res?.safetyIndexWarningList?.forEach(val => {
          val.cycleIndicator = this.formatPercent(val.cycleIndicator)
          val.fallbackIndicator = this.formatPercent(val.fallbackIndicator)
        })
        res?.displaySituationWarningList?.forEach(val => {
          val.cycleIndicator = this.formatPercent(val.cycleIndicator)
          val.fallbackIndicator = this.formatPercent(val.fallbackIndicator)
        })
        // 初始化展开状态:仅在有数据时允许展开
        const hasBusiness = (res.displaySituationWarningList || []).length > 0
        const hasBottom = (res?.safetyIndexWarningList || []).length > 0
        this.setData({
          showBusiness: hasBusiness ? false : false,
          showBottom: hasBottom ? false : false
        })
        this.setData({
          infoData: res || {},
          showPage: true
        })
      })
    },
    formatPercent(n) {
      const rate = Number(n || 0)
      return `${rate.toFixed(2)}%`
    },
    
    // 展开面板动画
    expandPanel(type) {
      const contentSelector = type === 'business' ? '.business-content' : '.bottom-content'
      const animationKey = type === 'business' ? 'businessAnimation' : 'bottomAnimation'
      
      // 先临时设置为auto高度以获取实际高度
      const tempAnimation = wx.createAnimation({
        duration: 0,
        timingFunction: 'ease'
      })
      tempAnimation.height('auto').opacity(1).step()
      this.setData({
        [animationKey]: tempAnimation.export()
      })
      
      // 下一帧获取内容实际高度并应用动画
      setTimeout(() => {
        wx.createSelectorQuery().in(this).select(contentSelector).boundingClientRect((rect) => {
          if (rect && rect.height > 0) {
            // 先设置为0,然后动画到实际高度
            const startAnimation = wx.createAnimation({
              duration: 0,
              timingFunction: 'ease'
            })
            startAnimation.height('0px').opacity(0).step()
            
            const endAnimation = wx.createAnimation({
              duration: 300,
              timingFunction: 'ease'
            })
            endAnimation.height(rect.height + 'px').opacity(1).step()
            
            this.setData({
              [animationKey]: startAnimation.export()
            })
            
            setTimeout(() => {
              this.setData({
                [animationKey]: endAnimation.export()
              })
            }, 50)
          }
        }).exec()
      }, 50)
    },
    
    // 收起面板动画
    collapsePanel(type) {
      const animationKey = type === 'business' ? 'businessAnimation' : 'bottomAnimation'
      
      const animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'ease'
      })
      
      animation.height('0px').opacity(0).step()
      this.setData({
        [animationKey]: animation.export()
      })
    }
  }
})