zhitong.yu
8 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
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
import { ElMessageBox, ElMessage } from "element-plus";
import { HandleData } from "./interface";
 
/**
 * @description 操作单条数据信息 (二次确认【删除、禁用、启用、重置密码】)
 * @param {Function} api 操作数据接口的api方法 (必传)
 * @param {Object} params 携带的操作数据参数 {id,params} (必传)
 * @param {String} message 提示信息 (必传)
 * @param {Function} t 翻译函数 (必传)
 * @param {String} confirmType icon类型 (不必传,默认为 warning)
 * @returns {Promise}
 */
export const useHandleData = (
  api: (params: any) => Promise<any>,
  params: any = {},
  message: string,
  t: (key: string, params?: any) => string,
  confirmType: HandleData.MessageType = "warning"
) => {
  return new Promise((resolve, reject) => {
    ElMessageBox.confirm(t("HandleData.confirmMessage", { message }), t("HandleData.confirmTitle"), {
      confirmButtonText: t("HandleData.confirmButton"),
      cancelButtonText: t("HandleData.cancelButton"),
      type: confirmType,
      draggable: true
    })
      .then(async () => {
        const res = await api(params);
        if (!res) return reject(false);
 
        ElMessage({
          type: "success",
          message: t("HandleData.successMessage", {
            action: message.replace(/^是否/, "") // 移除前面的"是否"前缀
          })
        });
        resolve(true);
      })
      .catch(() => {
        // cancel operation
      });
  });
};