mixins.js 1.09 KB
import request from '@/utils/request'
import dayjs from 'dayjs'

/**
 * KPI穿透组件公共mixin
 */
export const kpiDrillMixin = {
  methods: {
    formatMoney(v) {
      const num = Number(v || 0)
      return num.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
    },
    buildDateRange() {
      const { startTime, endTime } = this.filters || {}
      if (!startTime || !endTime) return null
      const start = dayjs(startTime).format('YYYY-MM-DD')
      const end = dayjs(endTime).format('YYYY-MM-DD')
      const startTs = dayjs(`${start} 00:00:00`).valueOf()
      const endTs = dayjs(`${end} 23:59:59`).valueOf()
      return { start, end, startTs, endTs }
    },
    getStoreId() {
      return (this.filters && this.filters.storeIds && this.filters.storeIds.length === 1)
        ? this.filters.storeIds[0]
        : undefined
    },
    getMonth() {
      return this.filters && this.filters.month
        ? this.filters.month.toString()
        : (this.buildDateRange() ? dayjs(this.buildDateRange().start).format('YYYYMM') : dayjs().format('YYYYMM'))
    }
  }
}