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,
  };
}
相关推荐
WZl几秒前
在传统的HTML、CSS与JavaScript项目中加入vue
javascript·css·vue.js·html
xcLeigh2 分钟前
AI 绘制图表专栏:用豆包轻松实现 HTML 柱状图、折线图与饼图
前端·人工智能·html·折线图·柱状图·图表·豆包
码代码的霖3 分钟前
HTML———标签元素
前端·html
BillKu4 分钟前
html2pdf.js使用与配置详解
开发语言·javascript·ecmascript
无·糖4 分钟前
大学生HTML期末大作业——HTML+CSS+JavaScript人物明星(周杰伦)
javascript·css·html·课程设计·大学生·大作业·web网页设计作业
鲸落✗6 分钟前
深入解析单HTML实现的网页版《我的世界》(附代码下载链接)
前端·python·html
Aerelin9 分钟前
豆瓣数据采集案例
前端·爬虫·python·js·playwright
扑棱蛾子10 分钟前
unipush推送入门:10分钟搞定UniPush在线消息集成
前端
b***653219 分钟前
Go-Gin Web 框架完整教程
前端·golang·gin
济南壹软网络科技有限公司22 分钟前
沉浸式体验革命:壹软科技2025新版盲盒源码前端3D渲染与个性化运营技术解析
前端·科技·uni-app·开源·php·盲盒源码