|
1 |
| -import { isFunction, isPlainObject } from '../../../utils/is/type'; |
2 |
| -import { map, mixin } from '../../../utils/collection'; |
| 1 | +import { isFunction, isPlainObject, map, mixin } from '@ntks/toolbox'; |
3 | 2 | import { invokeDingTalkApi } from './helper';
|
4 | 3 |
|
5 | 4 | /**
|
6 | 5 | * 调用通知类 API
|
7 |
| - * |
| 6 | + * |
8 | 7 | * @param {*} shortcut API 简写
|
9 | 8 | * @param {*} opts 配置项
|
10 | 9 | */
|
11 |
| -function invokeNotificationApi( shortcut: string, opts: any ): void { |
| 10 | +function invokeNotificationApi(shortcut: string, opts: any): void { |
12 | 11 | return invokeDingTalkApi(`device.notification.${shortcut}`, opts);
|
13 | 12 | }
|
14 | 13 |
|
15 | 14 | export default {
|
16 |
| - alert( message: any, callback: Function = function() {} ): void { |
17 |
| - invokeNotificationApi('alert', mixin({ |
18 |
| - title: '', |
19 |
| - buttonName: '知道了', |
20 |
| - onSuccess: callback |
21 |
| - }, isPlainObject(message) ? message : {message})); |
| 15 | + alert(message: any, callback: Function = function () {}): void { |
| 16 | + invokeNotificationApi( |
| 17 | + 'alert', |
| 18 | + mixin( |
| 19 | + { |
| 20 | + title: '', |
| 21 | + buttonName: '知道了', |
| 22 | + onSuccess: callback, |
| 23 | + }, |
| 24 | + isPlainObject(message) ? message : { message }, |
| 25 | + ), |
| 26 | + ); |
22 | 27 | },
|
23 |
| - confirm( message: any, agreed: Function = function() {}, cancelled: Function = function() {} ): void { |
24 |
| - invokeNotificationApi('confirm', mixin({ |
25 |
| - title: '', |
26 |
| - buttonLabels: ['确定', '取消'], |
27 |
| - onSuccess: ( result: any ) => result.buttonIndex === 0 ? agreed() : cancelled() |
28 |
| - }, isPlainObject(message) ? message : {message})); |
| 28 | + confirm(message: any, agreed: Function = function () {}, cancelled: Function = function () {}): void { |
| 29 | + invokeNotificationApi( |
| 30 | + 'confirm', |
| 31 | + mixin( |
| 32 | + { |
| 33 | + title: '', |
| 34 | + buttonLabels: ['确定', '取消'], |
| 35 | + onSuccess: (result: any) => (result.buttonIndex === 0 ? agreed() : cancelled()), |
| 36 | + }, |
| 37 | + isPlainObject(message) ? message : { message }, |
| 38 | + ), |
| 39 | + ); |
29 | 40 | },
|
30 | 41 | // prompt() {},
|
31 |
| - toast( opts: any ): void { |
| 42 | + toast(opts: any): void { |
32 | 43 | const { text, icon, duration, callback: onSuccess } = opts;
|
33 | 44 |
|
34 | 45 | invokeNotificationApi('toast', {
|
35 | 46 | text,
|
36 | 47 | icon: icon === 'fail' ? 'error' : icon,
|
37 | 48 | duration,
|
38 | 49 | delay: 0,
|
39 |
| - onSuccess |
| 50 | + onSuccess, |
40 | 51 | });
|
41 | 52 | },
|
42 | 53 | loading: {
|
43 |
| - show( opts: any ): void { |
| 54 | + show(opts: any): void { |
44 | 55 | const { text, callback: onSuccess } = opts;
|
45 | 56 |
|
46 | 57 | invokeNotificationApi('showPreloader', {
|
47 | 58 | text,
|
48 | 59 | showIcon: true,
|
49 |
| - onSuccess |
| 60 | + onSuccess, |
50 | 61 | });
|
51 | 62 | },
|
52 |
| - hide( callback: Function ): void { |
53 |
| - invokeNotificationApi('hidePreloader', {onSuccess: callback}); |
54 |
| - } |
| 63 | + hide(callback: Function): void { |
| 64 | + invokeNotificationApi('hidePreloader', { onSuccess: callback }); |
| 65 | + }, |
55 | 66 | },
|
56 |
| - actionSheet( opts: any ): void { |
| 67 | + actionSheet(opts: any): void { |
57 | 68 | const { title, cancel, actions } = opts;
|
58 | 69 |
|
59 | 70 | invokeNotificationApi('actionSheet', {
|
60 | 71 | title,
|
61 | 72 | cancelButton: cancel.text,
|
62 |
| - otherButtons: map(actions, ( action: any ) => action.text), |
63 |
| - onSuccess: ( result: any ) => { |
| 73 | + otherButtons: map(actions, (action: any) => action.text), |
| 74 | + onSuccess: (result: any) => { |
64 | 75 | const { buttonIndex: idx } = result;
|
65 |
| - |
| 76 | + |
66 | 77 | let action, handler;
|
67 |
| - |
68 |
| - if ( idx === -1 ) { |
| 78 | + |
| 79 | + if (idx === -1) { |
69 | 80 | handler = cancel.handler;
|
70 |
| - } |
71 |
| - else { |
| 81 | + } else { |
72 | 82 | action = actions[idx];
|
73 | 83 | handler = action.handler;
|
74 | 84 | }
|
75 |
| - |
76 |
| - if ( !isFunction(handler) ) { |
| 85 | + |
| 86 | + if (!isFunction(handler)) { |
77 | 87 | handler = opts.handler;
|
78 | 88 | }
|
79 |
| - |
| 89 | + |
80 | 90 | return action ? handler(action) : handler();
|
81 |
| - } |
| 91 | + }, |
82 | 92 | });
|
83 |
| - } |
84 |
| -} |
| 93 | + }, |
| 94 | +}; |
0 commit comments