integrated_traffic_vue/src/assets/js/useListData.js

104 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { nextTick, ref } from "vue";
import { getDataType } from "@/assets/js/utils.js";
/**
* @param api {Function} 接口函数
* @param options {Object?: {callbackFn, otherParams, defaultSearchForm, immediate, usePagination, key}} 配置项
* @param options.callbackFn {Function?} 回调函数(返回值【第一个参数表格数据,第二个参数后台返回的所有数据】)
* @param options.otherParams {Object?} 其它接口参数
* @param options.defaultSearchForm {Object?} searchForm默认值
* @param options.immediate {Boolean?} 是否立即执行接口函数(默认是)
* @param options.usePagination {Boolean?} 是否使用分页(默认是)
* @param options.clearSelection {Boolean?} 调用fnResetPagination是是否清空表格选择数据默认是
* @param options.key {String?} 返回的存放数组的key默认varList
* @return {Object} 返回对象包含以下属性list 表格数据pagination 分页数据searchForm 搜索表单数据tableRef 表格实例fnGetData 获取数据函数fnResetPagination 重置分页函数
*/
export default function useListData(api, options = {}) {
if (getDataType(api) !== "Function") throw new Error("api必须是一个函数");
if (getDataType(options) !== "Object")
throw new Error("options必须是一个对象");
if (options.immediate && getDataType(options.immediate) !== "Boolean")
throw new Error("options.immediate必须是一个布尔值");
if (options.usePagination && getDataType(options.usePagination) !== "Boolean")
throw new Error("options.usePagination必须是一个布尔值");
if (options.key && getDataType(options.key) !== "String")
throw new Error("options.key必须是一个字符串");
if (
options.callbackFn &&
getDataType(options.callbackFn) !== "Function" &&
getDataType(options.callbackFn) !== "AsyncFunction"
)
throw new Error("options.callbackFn必须是一个函数");
if (
options.defaultSearchForm &&
getDataType(options.defaultSearchForm) !== "Object"
)
throw new Error("options.defaultSearchForm必须是一个对象");
if (
options.clearSelection &&
getDataType(options.clearSelection) !== "Boolean"
)
throw new Error("options.clearSelection必须是一个布尔值");
const immediate = options.immediate ?? true;
const usePagination = options.usePagination ?? true;
const key = options.key ?? "varList";
const defaultSearchForm = options.defaultSearchForm ?? {};
const clearSelection = options.clearSelection ?? true;
if (
immediate &&
options.otherParams &&
getDataType(options.otherParams) !== "Object"
)
throw new Error("options.otherParams必须是一个对象");
const list = ref([]);
const pagination = ref({
currentPage: 1,
pageSize: 10,
total: 0,
});
const searchForm = ref(defaultSearchForm);
const tableRef = ref(null);
const fnGetData = async (otherParams = {}) => {
const resData = await api({
...(usePagination
? {
currentPage: pagination.value.currentPage,
showCount: pagination.value.pageSize,
}
: {}),
...searchForm.value,
...(options.otherParams || {}),
...(getDataType(otherParams) === "Object" ? otherParams : {}),
});
list.value = resData[key];
if (usePagination) pagination.value.total = resData.page.totalResult;
options.callbackFn && options.callbackFn(list.value, resData);
!usePagination &&
clearSelection &&
tableRef.value &&
tableRef.value.clearSelection();
};
immediate && fnGetData().then();
const fnResetPagination = async (otherParams) => {
list.value = [];
pagination.value = {
currentPage: 1,
pageSize: 10,
total: 0,
};
await nextTick();
await fnGetData(otherParams);
clearSelection && tableRef.value && tableRef.value.clearSelection();
};
return {
list,
pagination,
searchForm,
tableRef,
fnGetData: async (otherParams) => await fnGetData(otherParams),
fnResetPagination: async (otherParams) =>
await fnResetPagination(otherParams),
};
}