index.js
1.39 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
module.exports = {
showZanToast(title, timeout) {
const options = formatParameter(title, timeout);
// 清除上一轮的计时器
const { timer = 0 } = this.data.zanToast || {};
clearTimeout(timer);
// 弹层设置~
const zanToast = {
show: true,
icon: options.icon,
image: options.image,
title: options.title,
success:options.success
};
// console.log('zantoast',zanToast);
this.setData({
zanToast
});
// 传入的显示时长小于0,就认为需要一直显示
if (timeout < 0) {
return;
}
// 下一轮计时器
const nextTimer = setTimeout(() => {
zanToast.success();
this.clearZanToast();
}, timeout || 3000);
this.setData({
'zanToast.timer': nextTimer
});
},
// 清除所有 toast
clearZanToast() {
const { timer = 0 } = this.data.zanToast || {};
clearTimeout(timer);
this.setData({
'zanToast.show': false
});
},
// 快捷方法,显示 loading
showZanLoading(title) {
const options = formatParameter(title);
this.showZanToast({
...options,
icon: 'loading'
});
}
};
function formatParameter(title, timeout = 0) {
// 如果传入的 title 是对象,那么认为所有的配置属性都在这个对象中了
if (typeof title === 'object') {
return title;
}
return {
title,
timeout
};
}