parent
e3a99a619f
commit
761e5144f7
6 changed files with 144 additions and 29 deletions
@ -0,0 +1,65 @@ |
|||||||
|
import { ElMessage, MessageOptions } from "element-plus"; |
||||||
|
|
||||||
|
enum indexs { |
||||||
|
fulfilled, |
||||||
|
Rejected |
||||||
|
} |
||||||
|
|
||||||
|
interface Options { |
||||||
|
onFulfilled?: Function; |
||||||
|
onRejected?: Function; |
||||||
|
onFinish?: Function; |
||||||
|
// 是否需要提示:[ 成功时的 , 失败时的]。
|
||||||
|
// 默认:[true, true]
|
||||||
|
isNeedPrompts?: boolean[]; |
||||||
|
// 提示配置:[成功时的 , 失败时的]
|
||||||
|
msgObjs?: MessageOptions[]; |
||||||
|
// 提示配置的快捷message配置:[ 成功时的 , 失败时的]。
|
||||||
|
// 默认:['成功', '失败']
|
||||||
|
msgs?: string[]; |
||||||
|
[key: string]: any; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
export function getHint(pro: Promise<any>, options: Options = {}) { |
||||||
|
const ful = indexs.fulfilled; |
||||||
|
const rej = indexs.Rejected; |
||||||
|
const { isNeedPrompts, msgs } = options; |
||||||
|
|
||||||
|
const opt: Options = { |
||||||
|
...options, |
||||||
|
isNeedPrompts: Object.assign([true, true], isNeedPrompts), |
||||||
|
msgs: Object.assign(['成功', '失败'], msgs), |
||||||
|
} |
||||||
|
|
||||||
|
const onFulfilled = (res: any) => { |
||||||
|
if (opt.isNeedPrompts?.[ful]) { |
||||||
|
ElMessage({ |
||||||
|
message: opt.msgs?.[ful], |
||||||
|
type: 'success', |
||||||
|
...opt.msgObjs?.[ful] |
||||||
|
}); |
||||||
|
} |
||||||
|
if (opt.onFulfilled) opt.onFulfilled(res); |
||||||
|
} |
||||||
|
const onRejected = (err: Error) => { |
||||||
|
if (opt.isNeedPrompts?.[rej]) { |
||||||
|
ElMessage({ |
||||||
|
message: opt.msgs?.[rej], |
||||||
|
type: 'error', |
||||||
|
...opt.msgObjs?.[rej] |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
if (opt.onRejected) opt.onRejected(err); |
||||||
|
} |
||||||
|
const onFinish = () => { |
||||||
|
console.log(opt, opt.onFinish); |
||||||
|
|
||||||
|
if (opt.onFinish) opt.onFinish(); |
||||||
|
} |
||||||
|
|
||||||
|
pro.then(onFulfilled).catch(onRejected).finally(onFinish); |
||||||
|
|
||||||
|
return pro; |
||||||
|
} |
Loading…
Reference in new issue