-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.14 KB
/
index.js
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
const CURRENT_OPTIONS = {
icon: 'none',
duration: 2000,
mask: true,
};
const parseOptions = (message) => {
const isObject = Object.prototype.toString.call(message) === '[object Object]';
return isObject ? message : { message };
};
const Toast = (toastOptions) => {
const options = parseOptions(toastOptions);
const { message = '', icon = 'none' } = options;
if (!message) {
console.error('Error: 缺少必要参数 "message"!');
return undefined;
}
const params = { title: message };
if (icon === 'loading') {
Object.assign(params, CURRENT_OPTIONS, { duration: 3600000 });
} else {
Object.assign(params, CURRENT_OPTIONS);
}
delete options.message;
wx.showToast({ ...params, ...options });
};
const createMethod = (type) => (toastOptions) => {
const options = parseOptions(toastOptions);
Toast({ ...options, icon: type });
};
const clear = () => wx.hideToast();
const setDefaultOptions = (options) => Object.assign(CURRENT_OPTIONS, options);
Toast.loading = createMethod('loading');
Toast.success = createMethod('success');
Toast.clear = clear;
Toast.setDefaultOptions = setDefaultOptions;
module.exports = Toast;