format-utils.js
4.72 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
export function pickField(obj, ...keys) {
if (!obj) return undefined
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]
if (obj[key] !== undefined && obj[key] !== null) return obj[key]
}
return undefined
}
export function formatMoney(val) {
if (val === null || val === undefined || val === '') return '0.00'
return Number(val).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}
export function formatNumber(val) {
if (val === null || val === undefined || val === '') return '0'
return Number(val).toLocaleString('zh-CN')
}
export function formatPercent(val, digits) {
const d = digits == null ? 2 : digits
return Number(val || 0).toFixed(d)
}
export function pad2(n) {
return String(n).padStart(2, '0')
}
/** 默认区间:当月 1 日 → 今天 */
export function getDefaultDateRange() {
const now = new Date()
const year = now.getFullYear()
const month = now.getMonth() + 1
const day = now.getDate()
return [`${year}-${pad2(month)}-01`, `${year}-${pad2(month)}-${pad2(day)}`]
}
export function dateToStatisticsMonth(dateStr) {
if (!dateStr) return ''
return String(dateStr).replace(/-/g, '').slice(0, 6)
}
export function barWidth(value, max, minPercent = 8) {
const m = Number(max) || 0
const v = Number(value) || 0
if (m <= 0) return minPercent
return Math.max(minPercent, Math.round((v / m) * 1000) / 10)
}
export function dealTypeTone(name) {
const n = String(name || '')
if (n.indexOf('女神') >= 0) return 'red'
if (n.indexOf('其他') >= 0) return 'blue'
if (n.indexOf('科美') >= 0) return 'purple'
if (n.indexOf('医美') >= 0) return 'pink'
if (n.indexOf('生美') >= 0) return 'blue'
return 'gray'
}
export function dealTypeHint(name) {
const n = String(name || '')
if (n.indexOf('女神') >= 0) return '女神卡引流卡成交'
if (n.indexOf('其他') >= 0) return '其他生美项目成交'
if (n.indexOf('科美') >= 0) return '科美项目成交'
if (n.indexOf('医美') >= 0) return '医美项目成交'
return ''
}
export function isFunnelPayload(data) {
if (!data || typeof data !== 'object') return false
return !!(
pickField(data, 'SummaryKpis', 'summaryKpis') ||
pickField(data, 'FunnelSteps', 'funnelSteps') ||
pickField(data, 'LifeBeautySplit', 'lifeBeautySplit') ||
pickField(data, 'GoddessUpgradeFlow', 'goddessUpgradeFlow')
)
}
export function unwrapAcquisitionFunnel(res) {
const candidates = [res, res && res.data, res && res.data && res.data.data]
for (let i = 0; i < candidates.length; i += 1) {
if (isFunnelPayload(candidates[i])) return candidates[i]
}
if (res && res.data && typeof res.data === 'object') return res.data
return res && typeof res === 'object' ? res : {}
}
export function normalizeAcquisitionFunnel(data) {
const payload = unwrapAcquisitionFunnel(data)
if (!payload || typeof payload !== 'object') {
return {
summaryKpis: {},
funnelSteps: [],
dropOffs: {},
dealTypeDistribution: [],
lifeBeautySplit: {},
dealTypeUpgradeRates: [],
goddessUpgradeFlow: {},
goddessUpgradeTopItems: [],
otherLifeUpgradeFlow: {},
otherLifeUpgradeTopItems: [],
techMedicalSupplement: {},
metricNote: '',
conclusions: []
}
}
const techMedical = pickField(payload, 'TechMedicalSupplement', 'techMedicalSupplement') || {}
const conclusions = pickField(payload, 'Conclusions', 'conclusions') || []
return {
summaryKpis: pickField(payload, 'SummaryKpis', 'summaryKpis') || {},
funnelSteps: pickField(payload, 'FunnelSteps', 'funnelSteps') || [],
dropOffs: pickField(payload, 'DropOffs', 'dropOffs') || {},
dealTypeDistribution: pickField(payload, 'DealTypeDistribution', 'dealTypeDistribution') || [],
lifeBeautySplit: pickField(payload, 'LifeBeautySplit', 'lifeBeautySplit') || {},
dealTypeUpgradeRates: pickField(payload, 'DealTypeUpgradeRates', 'dealTypeUpgradeRates') || [],
goddessUpgradeFlow: pickField(payload, 'GoddessUpgradeFlow', 'goddessUpgradeFlow') || {},
goddessUpgradeTopItems: pickField(payload, 'GoddessUpgradeTopItems', 'goddessUpgradeTopItems') || [],
otherLifeUpgradeFlow: pickField(payload, 'OtherLifeUpgradeFlow', 'otherLifeUpgradeFlow') || {},
otherLifeUpgradeTopItems: pickField(payload, 'OtherLifeUpgradeTopItems', 'otherLifeUpgradeTopItems') || [],
techMedicalSupplement: {
...techMedical,
techUpgradeTopItems: pickField(techMedical, 'TechUpgradeTopItems', 'techUpgradeTopItems') || [],
medicalUpgradeTopItems: pickField(techMedical, 'MedicalUpgradeTopItems', 'medicalUpgradeTopItems') || []
},
metricNote: pickField(payload, 'MetricNote', 'metricNote') || '',
conclusions: Array.isArray(conclusions) ? conclusions.filter(Boolean) : []
}
}