2023-12-22 16:19:33 +08:00
|
|
|
import { reactive } from 'vue'
|
2023-12-26 16:17:30 +08:00
|
|
|
import createAxios from '@/utils/request'
|
|
|
|
|
import { requestPayload } from '@/utils/request'
|
|
|
|
|
import { Method } from 'axios'
|
2024-01-04 10:38:41 +08:00
|
|
|
import { mainHeight } from '@/utils/layout'
|
2024-12-26 15:56:32 +08:00
|
|
|
import { filtration } from './tableMethod'
|
2024-12-30 10:07:26 +08:00
|
|
|
import { ElMessage } from 'element-plus'
|
2023-12-22 16:19:33 +08:00
|
|
|
interface TableStoreParams {
|
2024-09-24 10:19:54 +08:00
|
|
|
url: string // 请求地址
|
2024-12-26 15:56:32 +08:00
|
|
|
pk?: string
|
2023-12-22 16:19:33 +08:00
|
|
|
column: TableColumn[]
|
2024-12-26 15:56:32 +08:00
|
|
|
params?: anyObj
|
|
|
|
|
method?: Method // 请求方式
|
2023-12-29 13:23:11 +08:00
|
|
|
isWebPaging?: boolean // 是否前端分页
|
2024-09-24 10:19:54 +08:00
|
|
|
showPage?: boolean //是否需要分页
|
|
|
|
|
paramsPOST?: boolean // post请求 params传参
|
|
|
|
|
publicHeight?: number //计算高度
|
|
|
|
|
resetCallback?: () => void // 重置
|
2024-12-26 15:56:32 +08:00
|
|
|
loadCallback?: () => void // 接口调用后的回调
|
2024-09-24 10:19:54 +08:00
|
|
|
beforeSearchFun?: () => void // 接口调用前的回调
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
export default class TableStore {
|
|
|
|
|
public url
|
|
|
|
|
public pk
|
2023-12-26 16:17:30 +08:00
|
|
|
public method: Method
|
2023-12-27 10:39:40 +08:00
|
|
|
public initData: any = null
|
2023-12-28 09:59:28 +08:00
|
|
|
public isWebPaging = false
|
2024-09-24 10:19:54 +08:00
|
|
|
public paramsPOST = true
|
2024-01-04 10:38:41 +08:00
|
|
|
public showPage = true
|
2023-12-22 16:19:33 +08:00
|
|
|
public table: CnTable = reactive({
|
|
|
|
|
ref: null,
|
|
|
|
|
selection: [],
|
|
|
|
|
data: [],
|
2024-12-26 15:56:32 +08:00
|
|
|
allData: [],
|
|
|
|
|
allFlag: false,
|
2023-12-28 09:59:28 +08:00
|
|
|
webPagingData: [],
|
2023-12-22 16:19:33 +08:00
|
|
|
total: 0,
|
|
|
|
|
params: {
|
|
|
|
|
pageNum: 1,
|
2023-12-28 09:59:28 +08:00
|
|
|
pageSize: 20
|
2023-12-22 16:19:33 +08:00
|
|
|
},
|
|
|
|
|
loading: true,
|
2023-12-29 13:23:11 +08:00
|
|
|
column: [],
|
|
|
|
|
loadCallback: null,
|
2024-01-04 10:38:41 +08:00
|
|
|
resetCallback: null,
|
2024-01-17 09:53:00 +08:00
|
|
|
beforeSearchFun: null,
|
2024-04-01 18:29:32 +08:00
|
|
|
height: '',
|
2024-01-04 10:38:41 +08:00
|
|
|
publicHeight: 0
|
2023-12-22 16:19:33 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
constructor(public options: TableStoreParams) {
|
|
|
|
|
this.url = options.url
|
|
|
|
|
this.pk = options.pk || 'id'
|
2024-09-24 10:19:54 +08:00
|
|
|
this.paramsPOST = options.paramsPOST || false
|
2023-12-28 09:59:28 +08:00
|
|
|
this.isWebPaging = options.isWebPaging || false
|
2023-12-26 16:17:30 +08:00
|
|
|
this.method = options.method || 'GET'
|
2023-12-22 16:19:33 +08:00
|
|
|
this.table.column = options.column
|
2024-01-18 18:19:59 +08:00
|
|
|
this.showPage = options.showPage !== false
|
2024-01-04 10:38:41 +08:00
|
|
|
this.table.publicHeight = options.publicHeight || 0
|
2023-12-29 13:23:11 +08:00
|
|
|
this.table.resetCallback = options.resetCallback || null
|
2023-12-29 14:46:20 +08:00
|
|
|
this.table.loadCallback = options.loadCallback || null
|
2024-01-17 09:53:00 +08:00
|
|
|
this.table.beforeSearchFun = options.beforeSearchFun || null
|
2023-12-22 16:19:33 +08:00
|
|
|
Object.assign(this.table.params, options.params)
|
2024-04-01 18:29:32 +08:00
|
|
|
this.table.height = mainHeight(20 + (this.showPage ? 58 : 0) + this.table.publicHeight).height as string
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
index() {
|
2024-04-01 18:29:32 +08:00
|
|
|
this.table.beforeSearchFun && this.table.beforeSearchFun()
|
2024-01-03 14:23:49 +08:00
|
|
|
this.table.data = []
|
2023-12-29 16:30:26 +08:00
|
|
|
this.table.loading = true
|
2023-12-27 10:39:40 +08:00
|
|
|
// 重置用的数据数据
|
|
|
|
|
if (!this.initData) {
|
|
|
|
|
this.initData = JSON.parse(JSON.stringify(this.table.params))
|
|
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
createAxios(
|
|
|
|
|
Object.assign(
|
|
|
|
|
{
|
|
|
|
|
url: this.url,
|
|
|
|
|
method: this.method
|
|
|
|
|
},
|
2024-09-24 10:19:54 +08:00
|
|
|
requestPayload(this.method, this.table.params, this.paramsPOST)
|
2023-12-26 16:17:30 +08:00
|
|
|
)
|
2024-12-26 15:56:32 +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
|
|
|
|
|
})
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
/**
|
|
|
|
|
* 表格内的事件统一响应
|
|
|
|
|
* @param event 事件:selection-change=选中项改变,page-size-change=每页数量改变,current-page-change=翻页
|
|
|
|
|
* @param data 携带数据
|
|
|
|
|
*/
|
|
|
|
|
onTableAction = (event: string, data: anyObj) => {
|
|
|
|
|
const actionFun = new Map([
|
2023-12-27 10:39:40 +08:00
|
|
|
[
|
|
|
|
|
'search',
|
|
|
|
|
() => {
|
|
|
|
|
this.table.params.pageNum = 1
|
|
|
|
|
this.index()
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'reset',
|
|
|
|
|
() => {
|
|
|
|
|
delete this.initData.pageSize
|
2024-04-01 18:29:32 +08:00
|
|
|
// console.log(this.table.params)
|
|
|
|
|
// console.log(this.initData)
|
2023-12-27 10:39:40 +08:00
|
|
|
Object.assign(this.table.params, this.initData)
|
|
|
|
|
this.index()
|
2023-12-29 13:23:11 +08:00
|
|
|
this.table.resetCallback && this.table.resetCallback()
|
2023-12-27 10:39:40 +08:00
|
|
|
}
|
|
|
|
|
],
|
2023-12-22 16:19:33 +08:00
|
|
|
[
|
|
|
|
|
'selection-change',
|
|
|
|
|
() => {
|
|
|
|
|
this.table.selection = data as TableRow[]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'page-size-change',
|
|
|
|
|
() => {
|
2023-12-28 09:59:28 +08:00
|
|
|
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()
|
|
|
|
|
}
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'current-page-change',
|
|
|
|
|
() => {
|
2023-12-28 09:59:28 +08:00
|
|
|
this.table.params.pageNum = data.page
|
|
|
|
|
if (this.isWebPaging) {
|
2024-01-03 14:23:49 +08:00
|
|
|
this.table.data = []
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
this.table.data = this.table.webPagingData[data.page - 1]
|
|
|
|
|
})
|
2023-12-28 09:59:28 +08:00
|
|
|
} else {
|
|
|
|
|
this.index()
|
|
|
|
|
}
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'field-change',
|
|
|
|
|
() => {
|
|
|
|
|
console.warn('field-change')
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'default',
|
|
|
|
|
() => {
|
|
|
|
|
console.warn('No action defined')
|
|
|
|
|
}
|
2024-12-26 15:56:32 +08:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'export',
|
|
|
|
|
() => {
|
|
|
|
|
// this.index()
|
2024-12-30 10:07:26 +08:00
|
|
|
ElMessage({
|
|
|
|
|
message: '正在导出,请稍等...',
|
|
|
|
|
type: 'info',
|
|
|
|
|
duration: 1000
|
|
|
|
|
})
|
2024-12-26 15:56:32 +08:00
|
|
|
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.allFlag = data.showAllFlag || true
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-12-22 16:19:33 +08:00
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
const action = actionFun.get(event) || actionFun.get('default')
|
|
|
|
|
action!.call(this)
|
|
|
|
|
}
|
|
|
|
|
}
|