/* * @Author: fxs bjnsfxs@163.com * @Date: 2024-08-20 17:15:49 * @LastEditors: fxs bjnsfxs@163.com * @LastEditTime: 2024-09-09 14:32:30 * @FilePath: \Game-Backstage-Management-System\src\hooks\useTable.ts * @Description: * */ import type { ResponseInfo } from '@/types/res' import axiosInstance from '../utils/axios/axiosInstance' // import { useRequest } from './useRequest' import type { TablePaginationSetting, DialogSetting } from '@/types/table' import { type FormInstance } from 'element-plus' export function useTable(tableData: Array, paginationSetting: TablePaginationSetting) { // const { analysisResCode } = useRequest() const getTableData = (url: string, option: any, isPagination: boolean = false) => { return new Promise(async (reslove, reject) => { try { await axiosInstance.post(url, option).then((result) => { let info = JSON.parse(JSON.stringify(result)) as ResponseInfo let data = info.data // 如果开启了分页,那么默认这个tabledata是一个二维数组,每个位置对应当页的一个数据数组 // 没开启则是一个一维数组,直接赋值 if (isPagination) { tableData[paginationSetting.currentPage] = data } else { tableData.splice(0, tableData.length, ...data) } // 如果有的接口没有返回count属性,就需要自己写 // 这个length,如果数组长为0,则需要自己赋值,不然会报错 if (info.count) paginationSetting.total = info.count else if (info.data) { paginationSetting.total = info.data.length } else { paginationSetting.total = 0 } reslove(true) // analysisResCode(data) // .then((info) => { // let data = info.data // // 如果开启了分页,那么默认这个tabledata是一个二维数组,每个位置对应当页的一个数据数组 // // 没开启则是一个一维数组,直接赋值 // if (isPagination) { // tableData[paginationSetting.currentPage] = data // } else { // tableData.splice(0, tableData.length, ...data) // } // // 如果有的接口没有返回count属性,就需要自己写 // // 这个length,如果数组长为0,则需要自己赋值,不然会报错 // if (info.count) paginationSetting.total = info.count // else if (info.data) { // paginationSetting.total = info.data.length // } else { // paginationSetting.total = 0 // } // reslove(true) // }) // .catch((err) => { // console.log(err) // throw new Error('请求失败') // }) }) } catch (err) { console.log(err) reject(err) throw new Error('网络请求错误') } }) } // 对话框关闭 const dialogClose = (formEl: FormInstance | undefined, dialogParam: DialogSetting) => { if (!formEl) return dialogParam.dialogVisible = false formEl.resetFields() } return { getTableData, dialogClose } }