StoreMessage.js
4.66 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// packageCustomer/components/StoreMessage/StoreMessage.js
import CustomerApi from '~/api/customerApi/index'
import { IMG_URL } from '~/utils/constantData'
Component({
options: {
addGlobalClass: true
},
properties: {
merchantId: Number
},
data: {
IMG_URL,
activeNames: [],
// 自定义折叠展开状态
showBusiness: false,
showBottom: false,
// 动画对象
businessAnimation: {},
bottomAnimation: {},
infoData: {},
showPage: false
},
lifetimes: {
attached() {
this.getDetailData()
}
},
methods: {
// 兼容之前 collapse 的回调(不再使用)
onCollapseChange(e) {
this.setData({ activeNames: e.detail })
},
// 切换展业情况展开
toggleBusiness(e) {
const has = Number(e?.currentTarget?.dataset?.has || 0)
if (!has) return
const isExpanding = !this.data.showBusiness
this.setData({ showBusiness: isExpanding })
if (isExpanding) {
this.expandPanel('business')
} else {
this.collapsePanel('business')
}
},
// 切换兜底预警展开
toggleBottom(e) {
const has = Number(e?.currentTarget?.dataset?.has || 0)
if (!has) return
const isExpanding = !this.data.showBottom
this.setData({ showBottom: isExpanding })
if (isExpanding) {
this.expandPanel('bottom')
} else {
this.collapsePanel('bottom')
}
},
getDetailData() {
let { merchantId } = this.data
wx.$api.showLoading()
if (!merchantId) {
wx.$sentry.reportParamError(
'门店信息参数异常: merchantId',
merchantId
)
}
CustomerApi.getStoreOverviewData({id: merchantId}).then(res => {
wx.$api.hideLoading()
res?.safetyIndexWarningList?.forEach(val => {
val.cycleIndicator = this.formatPercent(val.cycleIndicator)
val.fallbackIndicator = this.formatPercent(val.fallbackIndicator)
})
res?.displaySituationWarningList?.forEach(val => {
val.cycleIndicator = this.formatPercent(val.cycleIndicator)
val.fallbackIndicator = this.formatPercent(val.fallbackIndicator)
})
// 初始化展开状态:仅在有数据时允许展开
const hasBusiness = (res.displaySituationWarningList || []).length > 0
const hasBottom = (res?.safetyIndexWarningList || []).length > 0
this.setData({
showBusiness: hasBusiness ? false : false,
showBottom: hasBottom ? false : false
})
this.setData({
infoData: res || {},
showPage: true
})
})
},
formatPercent(n) {
const rate = Number(n || 0)
return `${rate.toFixed(2)}%`
},
// 展开面板动画
expandPanel(type) {
const contentSelector = type === 'business' ? '.business-content' : '.bottom-content'
const animationKey = type === 'business' ? 'businessAnimation' : 'bottomAnimation'
// 先临时设置为auto高度以获取实际高度
const tempAnimation = wx.createAnimation({
duration: 0,
timingFunction: 'ease'
})
tempAnimation.height('auto').opacity(1).step()
this.setData({
[animationKey]: tempAnimation.export()
})
// 下一帧获取内容实际高度并应用动画
setTimeout(() => {
wx.createSelectorQuery().in(this).select(contentSelector).boundingClientRect((rect) => {
if (rect && rect.height > 0) {
// 先设置为0,然后动画到实际高度
const startAnimation = wx.createAnimation({
duration: 0,
timingFunction: 'ease'
})
startAnimation.height('0px').opacity(0).step()
const endAnimation = wx.createAnimation({
duration: 300,
timingFunction: 'ease'
})
endAnimation.height(rect.height + 'px').opacity(1).step()
this.setData({
[animationKey]: startAnimation.export()
})
setTimeout(() => {
this.setData({
[animationKey]: endAnimation.export()
})
}, 50)
}
}).exec()
}, 50)
},
// 收起面板动画
collapsePanel(type) {
const animationKey = type === 'business' ? 'businessAnimation' : 'bottomAnimation'
const animation = wx.createAnimation({
duration: 300,
timingFunction: 'ease'
})
animation.height('0px').opacity(0).step()
this.setData({
[animationKey]: animation.export()
})
}
}
})