reportMixin.js
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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) // 在此处定义挂载后的逻辑
}
};