avue中tabel请求封装

1.封装请求hooks

返回reuqestdata,loading三个参数

import type { MaybeRef } from "@vueuse/core";
import type { Ref } from "vue";

export interface requestOptionTy {
  otherFunc: (...args) => void;
  otherQuery: any | Ref<any>;
  disableRequest: MaybeRef<boolean>;
  isMock: boolean;
  mockData: any;
  mockDelay: number;
  beforeRequest: (...args) => void;
}

/**
 * @param fetch onload时调用的方法
 * @param options 除开请求接口外额外的配置项
 *  beforeRequest 请求前执行的函数
 *  otherQuery 额外的请求参数
 *  otherFunc 请求方法之外执行的函数
 *  disableRequest 是否可以执行请求
 *  isMock 是否采用模拟数据
 *  mockData 模拟数据
 *  mockDelay 模拟数据延时
 *
 */

export default function <T extends Promise<BaseResType>>(
  fetch: ((current: number, size: number, params: object) => T) | null,
  options: Partial<requestOptionTy>
) {
  const data: Ref<PickOne<Awaited<T>, "data">> = ref();
  const loading: Ref<boolean> = ref(true);
  const _ = options;
  let queryCache = reactive({
    wheres: [],
  });
  const onLoad = async (page, params) => {
    if (params) {
      queryCache = params;
    }
    loading.value = true;
    const pageRow = unref(page);
    if (_?.isMock || fetch === null) {
      setTimeout(() => {
        data.value = _?.mockData;
        pageRow.total = _?.mockData.length ?? 0;
        loading.value = false;
      }, _?.mockDelay || 1000);
      return;
    }

    if (!unref(_?.disableRequest)) {
      if (_?.otherQuery && _?.otherQuery.parmasSearchWheres) {
        queryCache.wheres =
          queryCache.wheres && queryCache.wheres.length
            ? queryCache.wheres
            : [];
        //将搜索条件赋值
        _?.otherQuery.parmasSearchWheres.array.forEach((item) => {
          queryCache.wheres?.push(item);
        });
      }
      const _resParams = Object.assign(
        {},
        queryCache,
        unref(_?.otherQuery ?? {})
      );
      //请求执行之前方法
      _?.beforeRequest &&
        typeof _?.beforeRequest === "function" &&
        _?.beforeRequest(_resParams);

      const res = await fetch(pageRow.current, pageRow.size, _resParams);

      if (Object.keys(res?.data).length === 0) {
        data.value = [];
      } else {
        data.value = res?.data?.records ?? res?.data ?? [];
      }
      pageRow.total = res?.data?.total ?? 0;
      loading.value = false;
      _?.otherFunc && typeof _.otherFunc === "function" && _.otherFunc();
    }
  };

  return {
    onLoad,
    data,
    loading,
  };
}

2.对selectionList列表封装

import type { Ref } from "vue";
/**
 *
 * @param idKey id key值
 * @param selectionList 选择的checkbox
 * @param ids 选择对象的id集合 用,分割
 * @param selectionChange checkbox改变事件 数据源
 */

export default function (idKey = "id") {
  const selectionList: Ref<any[]> = ref([]);
  const selectionChange = (list: any[]) => {
    selectionList.value = list;
  };
  const ids = computed(() => {
    return selectionList.value.map((item) => item[idKey]).join(",");
  });
  const idsArr = computed(() => {
    return selectionList.value.map((item) => item[idKey]);
  });
  return {
    selectionList,
    selectionChange,
    ids,
    idsArr,
  };
}

3.搜索条件的封装

import type { Ref } from "vue";
import { cloneDeep } from "lodash-unified";

/**
 *
 * @param page 分页
 * @param request onload请求
 * @param paramsPreFixFn 请求参数前置处理
 * query 缓存的请求参数
 * searchChange 搜索条件变化重新请求
 * searchReset 重置搜索条件
 */

export default function (
  page,
  request: (...args) => void,
  paramsPreFixFn?: (...args) => void
) {
  const query = ref({});
  const searchChange = (params, done: () => void) => {
    const paramsClone = cloneDeep(params);
    paramsPreFixFn?.(paramsClone);
    query.value = paramsClone;
    page.value.currentPage = 1;
    request(page, paramsClone);
    done();
  };

  const searchReset = (params: any) => {
    const paramsClone = cloneDeep(params);
    paramsPreFixFn?.(paramsClone);
    query.value = {};
    page.value.currentPage = 1;
    request(page, paramsClone);
  };

  const refreshChange = () => {
    request(page, query.value);
  };
  return {
    query,
    searchChange,
    searchReset,
    refreshChange,
  };
}

针对于翻页的封装

export default function (request?: (...args: any) => void) {
  const page = ref({
    currentPage: 1,
    pageSize: 10,
    total: 0,
  });
  //改变页数
  const currentChange = (current: number, params: any) => {
    page.value.currentPage = current;
    if (request) request(page, params);
  };
  const sizeChange = (size: number, params: any) => {
    page.value.currentPage = 1;
    page.value.pageSize = size;
    if (request) request(page, params);
  };
  return {
    page,
    currentChange,
    sizeChange,
  };
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值