useTable.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-20 17:15:49
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-09-09 14:32:30
  6. * @FilePath: \Game-Backstage-Management-System\src\hooks\useTable.ts
  7. * @Description:
  8. *
  9. */
  10. import type { ResponseInfo } from '@/types/res'
  11. import axiosInstance from '../utils/axios/axiosInstance'
  12. // import { useRequest } from './useRequest'
  13. import type { TablePaginationSetting, DialogSetting } from '@/types/table'
  14. import { type FormInstance } from 'element-plus'
  15. export function useTable(tableData: Array<any>, paginationSetting: TablePaginationSetting) {
  16. // const { analysisResCode } = useRequest()
  17. const getTableData = (url: string, option: any, isPagination: boolean = false) => {
  18. return new Promise(async (reslove, reject) => {
  19. try {
  20. await axiosInstance.post(url, option).then((result) => {
  21. let info = JSON.parse(JSON.stringify(result)) as ResponseInfo
  22. let data = info.data
  23. // 如果开启了分页,那么默认这个tabledata是一个二维数组,每个位置对应当页的一个数据数组
  24. // 没开启则是一个一维数组,直接赋值
  25. if (isPagination) {
  26. tableData[paginationSetting.currentPage] = data
  27. } else {
  28. tableData.splice(0, tableData.length, ...data)
  29. }
  30. // 如果有的接口没有返回count属性,就需要自己写
  31. // 这个length,如果数组长为0,则需要自己赋值,不然会报错
  32. if (info.count) paginationSetting.total = info.count
  33. else if (info.data) {
  34. paginationSetting.total = info.data.length
  35. } else {
  36. paginationSetting.total = 0
  37. }
  38. reslove(true)
  39. // analysisResCode(data)
  40. // .then((info) => {
  41. // let data = info.data
  42. // // 如果开启了分页,那么默认这个tabledata是一个二维数组,每个位置对应当页的一个数据数组
  43. // // 没开启则是一个一维数组,直接赋值
  44. // if (isPagination) {
  45. // tableData[paginationSetting.currentPage] = data
  46. // } else {
  47. // tableData.splice(0, tableData.length, ...data)
  48. // }
  49. // // 如果有的接口没有返回count属性,就需要自己写
  50. // // 这个length,如果数组长为0,则需要自己赋值,不然会报错
  51. // if (info.count) paginationSetting.total = info.count
  52. // else if (info.data) {
  53. // paginationSetting.total = info.data.length
  54. // } else {
  55. // paginationSetting.total = 0
  56. // }
  57. // reslove(true)
  58. // })
  59. // .catch((err) => {
  60. // console.log(err)
  61. // throw new Error('请求失败')
  62. // })
  63. })
  64. } catch (err) {
  65. console.log(err)
  66. reject(err)
  67. throw new Error('网络请求错误')
  68. }
  69. })
  70. }
  71. // 对话框关闭
  72. const dialogClose = (formEl: FormInstance | undefined, dialogParam: DialogSetting) => {
  73. if (!formEl) return
  74. dialogParam.dialogVisible = false
  75. formEl.resetFields()
  76. }
  77. return {
  78. getTableData,
  79. dialogClose
  80. }
  81. }