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,
  };
}
相关推荐
Pu_Nine_98 小时前
IntersectionObserver 详解:封装 Vue 指令实现图片懒加载
前端·javascript·vue.js·性能优化
清灵xmf8 小时前
Web 和 Native 是怎么“对话“的?JSBridge 解答
前端·webview·native·jsbridge·hybrid
jiayong238 小时前
前端面试题库 - ES6+新特性篇
前端·面试·es6
海兰8 小时前
【实用应用】React+TypeScript+Next.js博客项目
开发语言·javascript·elasticsearch
前端那点事8 小时前
Vue nextTick 超全解析|作用、使用场景、底层原理、Vue2/Vue3区别
前端·vue.js
前端那点事8 小时前
Vue面试高频:子组件能直接修改父组件数据吗?单向数据流原理+正确写法全覆盖
前端·vue.js
前端那点事8 小时前
为什么 Vue 的 template 标签不能用 v-show?底层机制+踩坑复盘+生产级解决方案
前端·vue.js
weelinking9 小时前
【claude】14_Claude作为技术文档助手
前端·人工智能·react.js·数据挖掘·前端框架
天问一9 小时前
router路由类型和使用方法
开发语言·javascript·ecmascript
jiayong239 小时前
前端面试题库 - JavaScript核心基础篇
前端·javascript·面试