avue中增删改功能hook提取

再avue使用中,我们会进场用到表格的增删改功能,我们写一个公共的hooks,然后只需要对请求的方法,参数的前后处理,就可以统一生成

javascript 复制代码
import type { AxiosPromise } from "axios";
import type { Ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { cloneDeep } from "lodash-es";
import { error } from "console";
export interface CudOptions {
  cRequest: (...args) => AxiosPromise; //新增
  uRequest: (...args) => AxiosPromise; //更新
  dRequest: (...args) => AxiosPromise; //删除
  tableRequest: (...args) => AxiosPromise; // avue table onload 方法
  page: Ref<any>;
  // 前丶后置处理
  beforCReqFunc?: (row: any) => void;

  afterCReqFunc?: (row: any) => void;
  beforUReqFunc?: (row: any) => void;
  afterUReqFunc?: (row: any) => void;
  beforDReqFunc?: (row: any) => void;
  afterDReqFunc?: (row: any) => void;
  // 额外的请求参数
  cQuery?: object | Ref<object>;
  uQuery?: object | Ref<object>;
  // id - key
  rowIdKey?: string;
  // mock 配置
  isMock?: boolean;
  mockDelay?: number;
  developType?: string;
}

export default function (options: CudOptions) {
  /**增,删,改 */
  const _ = options;
  const pageRow = unref(_.page);
  const defaultPage = {
    pageSize: 1,
    currentPage: 10,
    total: 0,
  };
  //删除
  const rowDel = (row: object) => {
    ElMessageBox.confirm("确定将选择数据删除?", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const rowCopy = cloneDeep(row);
      _?.beforDReqFunc?.(rowCopy); //请求前自定义处理
      try {
        const res = await _?.dRequest(rowCopy[_?.rowIdKey ?? "id"]);
        if ((res as any).code === 200) {
          ElMessage.success({
            type: "success",
            message: "操作成功!",
          });
          _?.afterDReqFunc?.(rowCopy); //请求自定义后处置
          //更新tabel数据
          await _?.tableRequest?.(_?.page);
        }
      } catch (e) {
        ElMessage.error(e);
      }
    });
  };

  //新增
  const rowSave = async (row, done, loading) => {
    const rowCopy = cloneDeep(row);
    _?.beforCReqFunc?.(rowCopy); //请求前自定义处理
    try {
      loading();
      const res = await _?.cRequest?.({
        ...rowCopy,
        ...unref(_?.cQuery ?? {}),
      });
      if ((res as unknown as any).code === 200) {
        ElMessage({
          type: "success",
          message: "操作成功",
        });
        done();
      }
      await _?.tableRequest?.(_?.page);
      _?.afterCReqFunc?.(rowCopy);
    } catch (e) {
      console.log("create error", error);
    }
  };
  // 更新
  const rowUpdate = async (row, index, done, loading) => {
    if (_.isMock) {
      setTimeout(() => {
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
      }, _.mockDelay ?? 1000);
    } else {
      const rowCopy = cloneDeep(row);
      _?.beforUReqFunc?.(rowCopy); // 请求自定义前处理
      try {
        loading();
        const res = await _?.uRequest?.({
          ...rowCopy,
          ...unref(_?.uQuery ?? {}),
        });
        if ((res as unknown as ReplaceTargetType<any>).code === 200) {
          ElMessage({
            type: "success",
            message: "操作成功!",
          });
          done();
        }
        // 更新table数据
        await _?.tableRequest?.(_.page);
        _?.afterUReqFunc?.(rowCopy); // 请求自定义后处理
      } catch (error) {
        // done();
        console.log("create error:", error);
      }
    }
  };
  return {
    rowDel,
    rowSave,
    rowUpdate,
  };
}
相关推荐
kyriewen12 小时前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒13 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
山河木马13 小时前
矩阵专题2-怎么创建视图矩阵(uViewMatrix)
javascript·webgl·计算机图形学
小林攻城狮13 小时前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦13 小时前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer14 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队14 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY14 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_14 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏14 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端