Files
admin-sjzx/src/utils/tableStore.ts

226 lines
7.8 KiB
TypeScript
Raw Normal View History

2024-02-19 13:44:32 +08:00
import { reactive } from 'vue'
import createAxios from '@/utils/request'
import { requestPayload } from '@/utils/request'
import { Method } from 'axios'
import { mainHeight } from '@/utils/layout'
2024-12-27 08:47:47 +08:00
import { filtration } from './tableMethod'
2024-02-26 20:36:18 +08:00
2024-02-19 13:44:32 +08:00
interface TableStoreParams {
2024-08-01 14:36:54 +08:00
url: string // 请求地址
2024-10-16 10:49:48 +08:00
pk?: string
filename?: any // 导出文件名
2024-02-19 13:44:32 +08:00
column: TableColumn[]
2024-10-16 10:49:48 +08:00
params?: anyObj
method?: Method // 请求方式
2024-02-19 13:44:32 +08:00
isWebPaging?: boolean // 是否前端分页
2024-08-01 14:36:54 +08:00
showPage?: boolean //是否需要分页
2025-01-07 16:32:42 +08:00
timeAll?: boolean //是否需要时间全部显示
2024-08-01 14:36:54 +08:00
paramsPOST?: boolean // post请求 params传参
publicHeight?: number //计算高度
resetCallback?: () => void // 重置
2024-10-16 10:49:48 +08:00
loadCallback?: () => void // 接口调用后的回调
exportProcessingData?:() => void //导出处理数据
2024-08-01 14:36:54 +08:00
beforeSearchFun?: () => void // 接口调用前的回调
2024-02-19 13:44:32 +08:00
}
export default class TableStore {
public url
public pk
public filename: any = null
2024-02-19 13:44:32 +08:00
public method: Method
public initData: any = null
public isWebPaging = false
2024-08-01 13:58:32 +08:00
public paramsPOST = true
2024-02-19 13:44:32 +08:00
public showPage = true
2025-01-07 16:32:42 +08:00
public timeAll = true
2024-02-19 13:44:32 +08:00
public table: CnTable = reactive({
ref: null,
selection: [],
data: [],
2024-12-27 08:47:47 +08:00
allData: [],
allFlag: false,
2024-02-19 13:44:32 +08:00
webPagingData: [],
total: 0,
params: {
pageNum: 1,
pageSize: 20
},
filename:null,
2024-02-19 13:44:32 +08:00
loading: true,
column: [],
loadCallback: null,
exportProcessingData: null,
2024-02-19 13:44:32 +08:00
resetCallback: null,
beforeSearchFun: null,
2024-03-13 11:42:19 +08:00
height: '',
2024-02-19 13:44:32 +08:00
publicHeight: 0
})
constructor(public options: TableStoreParams) {
this.url = options.url
this.pk = options.pk || 'id'
2024-08-01 13:58:32 +08:00
this.paramsPOST = options.paramsPOST || false
2024-02-19 13:44:32 +08:00
this.isWebPaging = options.isWebPaging || false
this.method = options.method || 'GET'
this.table.filename = options.filename || null
2024-02-19 13:44:32 +08:00
this.table.column = options.column
this.showPage = options.showPage !== false
2025-01-07 16:32:42 +08:00
2024-02-19 13:44:32 +08:00
this.table.publicHeight = options.publicHeight || 0
this.table.resetCallback = options.resetCallback || null
this.table.loadCallback = options.loadCallback || null
this.table.exportProcessingData = options.exportProcessingData || null
2024-02-19 13:44:32 +08:00
this.table.beforeSearchFun = options.beforeSearchFun || null
Object.assign(this.table.params, options.params)
2024-02-26 20:36:18 +08:00
this.table.height = mainHeight(20 + (this.showPage ? 58 : 0) + this.table.publicHeight).height as string
2024-02-19 13:44:32 +08:00
}
index() {
2024-02-28 11:16:40 +08:00
this.table.beforeSearchFun && this.table.beforeSearchFun()
2024-02-19 13:44:32 +08:00
this.table.data = []
this.table.loading = true
// 重置用的数据数据
if (!this.initData) {
this.initData = JSON.parse(JSON.stringify(this.table.params))
2025-01-07 16:32:42 +08:00
}
if (!this.timeAll) {
delete this.table.params.startTime;
delete this.table.params.endTime;
delete this.table.params.searchBeginTime;
delete this.table.params.searchEndTime;
delete this.table.params.timeFlag;
2024-02-19 13:44:32 +08:00
}
createAxios(
Object.assign(
{
url: this.url,
method: this.method
},
2024-08-01 13:58:32 +08:00
requestPayload(this.method, this.table.params, this.paramsPOST)
2024-02-19 13:44:32 +08:00
)
2024-10-16 10:49:48 +08:00
)
.then((res: any) => {
if (res.data) {
this.table.data = res.data.records || res.data
this.table.total = res.data?.total || res.data.length || 0
} else {
this.table.data = []
this.table.total = 0
}
if (Array.isArray(res)) {
this.table.data = res
}
if (this.isWebPaging) {
this.table.webPagingData = window.XEUtils.chunk(this.table.data, this.table.params.pageSize)
this.table.data = this.table.webPagingData[this.table.params.pageNum - 1]
}
this.table.loadCallback && this.table.loadCallback()
this.table.loading = false
})
.catch(() => {
this.table.loading = false
})
2024-02-19 13:44:32 +08:00
}
/**
*
* @param event 事件:selection-change=,page-size-change=,current-page-change=
* @param data
*/
onTableAction = (event: string, data: anyObj) => {
const actionFun = new Map([
[
'search',
() => {
this.table.params.pageNum = 1
this.index()
}
],
[
'reset',
() => {
delete this.initData.pageSize
// console.log(this.table.params)
// console.log(this.initData)
2024-02-19 13:44:32 +08:00
Object.assign(this.table.params, this.initData)
this.index()
this.table.resetCallback && this.table.resetCallback()
}
],
[
'selection-change',
() => {
this.table.selection = data as TableRow[]
}
],
[
'page-size-change',
() => {
this.table.params.pageSize = data.size
this.table.params.pageNum = 1
if (this.isWebPaging) {
this.table.webPagingData = window.XEUtils.chunk(
window.XEUtils.flatten(this.table.webPagingData),
this.table.params.pageSize
)
this.table.data = this.table.webPagingData[this.table.params.pageNum - 1]
} else {
this.index()
}
}
],
[
'current-page-change',
() => {
this.table.params.pageNum = data.page
if (this.isWebPaging) {
this.table.data = []
requestAnimationFrame(() => {
this.table.data = this.table.webPagingData[data.page - 1]
})
} else {
this.index()
}
}
],
[
'field-change',
() => {
console.warn('field-change')
}
],
[
'default',
() => {
console.warn('No action defined')
}
2024-12-27 08:47:47 +08:00
],
[
'export',
() => {
// this.index()
let params = { ...this.table.params, pageNum: 1, pageSize: this.table.total }
createAxios(
Object.assign(
{
url: this.url,
method: this.method
},
requestPayload(this.method, params, this.paramsPOST)
)
).then(res => {
this.table.allData = filtration(res.data.records || res.data)
this.table.exportProcessingData && this.table.exportProcessingData()
2024-12-27 08:47:47 +08:00
this.table.allFlag = data.showAllFlag || true
})
}
2024-02-19 13:44:32 +08:00
]
])
const action = actionFun.get(event) || actionFun.get('default')
action!.call(this)
}
}