reportMixin.js 4.56 KB
import { reportApi } from '@/api'
import reportComponent from '@/views/mobile/components/reportComponent.vue';
import reportLoading from '@/views/mobile/components/reportLoading.vue';
import utils from '@/utils/util'
import dayjs from 'dayjs'
export default {
  components: { reportComponent, reportLoading },
  data () {
    return {
      reportList: [null, null, null],
      query: {},
      dayjs,
      dataComplete: false
    };
  },
  computed: {
    validReportList () {
      // 过滤掉 undefined、null 等无效值
      const filtered = this.reportList.filter(item => item && Object.keys(item).length > 0)
      return filtered
    },
  },
  methods: {
    refreshReport () {
      this.getReportList(1)
      this.getReportList(2)
      this.getReportList(3)
    },
    convertMetricsFormat (data) {
      if (!data) {
        return []
      }

      // 兼容月报和周报两种数据格式
      const currentMonth = data['本月数据'] || data['本周数据']
      const lastMonth = data['上月数据'] || data['上周数据']
      const change = data['环比变化']

      if (!currentMonth || !lastMonth || !change) {
        return []
      }

      // 动态获取所有指标名称(从当前周期数据中获取 key)
      const metricNames = Object.keys(currentMonth)

      return metricNames.map(name => {
        const currentValue = currentMonth[name] || 0
        const lastValue = lastMonth[name] || 0
        const changeValue = change[name] || 0

        // 判断是否需要百分比单位
        const isPercentage = name.includes('率')
        const unit = isPercentage ? '%' : ''

        // 格式化数值
        let formattedCurrent = currentValue
        let formattedLast = lastValue
        let formattedChange = changeValue
        if (!isPercentage) {
          // 只对本身有小数的数值进行 toFixed 处理
          if (formattedCurrent % 1 !== 0) {
            formattedCurrent = Number(formattedCurrent).toFixed(2)
          }
          if (formattedLast % 1 !== 0) {
            formattedLast = Number(formattedLast).toFixed(2)
          }
          if (formattedChange % 1 !== 0) {
            formattedChange = Number(formattedChange).toFixed(2)
          }
        }
        return {
          name,
          currentValue: formattedCurrent,
          lastValue: formattedLast,
          change: Number(formattedChange),
          unit
        }
      })
    },
    getReportList (businessType) {
      let { reportType,
        periodKey,
        reportStartDate,
        reportEndDate } = this.query
      reportApi.getReportSummary({
        businessType,
        periodKey,
        reportType,
        periodStart: `${reportStartDate} 00:00:00`,
        periodEnd: `${reportEndDate} 23:59:59`
      }).then(({ code, data }) => {
        if (code == 0 && data) {
          let metchantListData = []
          data.businessList?.forEach(m => {
            if (m.summaryPayload) metchantListData.push(JSON.parse(m.summaryPayload))
          })
          data.brandList?.forEach(b => {
            console.log(JSON.parse(b.metricPayload))
            if (b.metricPayload) b.businessListData = this.convertMetricsFormat(JSON.parse(b.metricPayload))
          })
          this.$set(this.reportList, businessType - 1, {
            ...data,
            reportType,
            metchantListData,
            businessType,
            reportStartDate,
            reportEndDate
          })
          // console.log(this.reportList)
          this.dataComplete = true
        }
      }).catch(err => {
        console.error(`getReportList(${businessType}) 错误:`, err)
      })
    },
    shareReport () {
      const { reportStartDate, reportEndDate, reportType, corpId } = this.query,
        url = window.$fetchConfig.shareReportUrl
      utils.setClipboardData(`${url}?reportStartDate=${reportStartDate}&reportEndDate=${reportEndDate}&reportType=${reportType}&corpId=${corpId}`, '链接复制成功,快去粘贴吧~',true)
      window.parent.postMessage(
        { type: 'COPY_SUCCESS' },
        '*'
      )
    },
  },
  created () {
  },
  mounted () {
    const { reportStartDate, reportEndDate, reportType, corpId } = this.$route.query
    this.query = {
      reportType,
      periodKey: utils.getPeriodKey(reportStartDate, reportEndDate, reportType),
      reportStartDate,
      reportEndDate,
      corpId
    }
    this.getReportList(1)
    this.getReportList(2)
    this.getReportList(3)  // 在此处定义挂载后的逻辑
  }
};