请求时提示配置
Thu Sep 26 2024 03:53:30 GMT+0000 (Coordinated Universal Time)
Saved by
@vasttininess
#javascript
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;
}
content_copyCOPY
Comments