WeeklyConversionRate.js
3.42 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
142
// packageStudyCenter/pages/myTask/components/WeeklyConversionRate/WeeklyConversionRate.js
Component({
properties: {
isFlag: {
type: Boolean,
value: false
},
dataInfo: {
type: Object,
value: {},
observer: function(newVal) {
this.updateAllData(true)
}
},
isHmzx: {
type: Boolean,
value: false
},
isYb: {
type: Boolean,
value: false
},
isBeisen: {
type: Boolean,
value: false
}
},
data: {
barList: [
{
label: '我的',
value: '74.3',
key: 'myConversionRatio',
class: 'my'
},
{
label: 'Top1',
value: '94.5',
key: 'topOneConversionRatio',
class: 'top1'
}
],
tabIndex: 0,
tabList: [],
currentConversionData: {},
barStyles: []
},
observers: {
'tabIndex': function(tabIndex) {
this.updateAllData();
}
},
lifetimes: {
attached() {
// 初始化时更新
this.updateAllData(true);
}
},
methods: {
updateAllData(updateTabFlag = false) {
const { dataInfo, tabIndex, barList } = this.data;
const weekConversionRatio = dataInfo && dataInfo.weekConversionRatio
// 更新tabList
if (updateTabFlag) this.updateTabList()
// 计算当前数据
let currentConversionData = {};
if (weekConversionRatio && this.data.tabList.length > 0 && this.data.tabList[tabIndex]) {
const key = this.data.tabList[tabIndex].value;
currentConversionData = weekConversionRatio[key] || {};
}
// 计算进度条样式
const barStyles = barList.map(item => {
const value = this.getNumberValue(currentConversionData[item.key]);
const width = Math.min(100, Math.max(2, value));
const left = value > 100 ? 100 : value;
const displayValue = value.toFixed(1);
return {
width: `${width}%`,
left: `calc(${left}%)`,
text: `${displayValue}%`
};
});
this.setData({
currentConversionData,
barStyles
});
},
updateTabList() {
const weekConversionRatio = this.properties.dataInfo && this.properties.dataInfo.weekConversionRatio
if (!weekConversionRatio) {
this.setData({ tabList: [] });
return;
}
const order = [0, 1, 2];
const tabArr = order
.filter(key => weekConversionRatio.hasOwnProperty(key))
.map(key => {
let title = '';
if (key == 0) title = '轻改';
else if (key == 1) title = '权益';
else if (key == 2) title = '延保';
else title = `类型${key}`;
return {
title,
value: key
};
});
this.setData({ tabList: tabArr }, () => {
// tabList更新后,检查是否需要调整tabIndex
if (tabArr.length > 0) {
const yanbaoIndex = tabArr.findIndex(item => item.value == 2);
let newTabIndex = 0;
if (yanbaoIndex !== -1) {
newTabIndex = yanbaoIndex;
}
if (this.data.tabIndex !== newTabIndex) {
this.setData({ tabIndex: newTabIndex });
}
}
});
},
getNumberValue(value) {
return Number(value) || 0
},
handleTabClick(e) {
const index = e.currentTarget.dataset.index
this.setData({ tabIndex: index })
}
}
})