fixWxShowToast.js
1.98 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
// 注意此代码应该在调用原生api之前执行
export default function fixWxShowToast() {
let isShowLoading = false
let isShowToast = false
const { showLoading, hideLoading, showToast, hideToast } = wx
Object.defineProperty(wx, 'showLoading', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowToast) {
// Toast优先级更高
return
}
isShowLoading = true
// console.log('--------showLoading--------')
return showLoading.apply(this, param) // 原样移交函数参数和this
}
})
Object.defineProperty(wx, 'hideLoading', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowToast) {
// Toast优先级更高
return
}
isShowLoading = false
// console.log('--------hideLoading--------')
return hideLoading.apply(this, param) // 原样移交函数参数和this
}
})
Object.defineProperty(wx, 'showToast', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowLoading) {
// Toast优先级更高
wx.hideLoading()
}
isShowToast = true
setTimeout(() => {
isShowToast = false
}, 2000)
console.error('--------showToast--------')
return showToast.apply(this, param) // 原样移交函数参数和this
}
})
Object.defineProperty(wx, 'hideToast', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
isShowToast = false
console.error('--------hideToast--------')
return hideToast.apply(this, param) // 原样移交函数参数和this
}
})
}