2024-07-27 05:33:36 +00:00
|
|
|
|
import { cloneDeep } from 'lodash-es'
|
|
|
|
|
import { hasPerm } from '@/utils/permission'
|
|
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
import useTabs from '@/utils/useTabs'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表页面表格信息 hook 封装(数据操作)
|
|
|
|
|
* @param apiModule 调用接口
|
|
|
|
|
* @param tableColumns 表格头部信息
|
2024-07-30 09:47:29 +00:00
|
|
|
|
* @param hasPermData
|
2024-07-27 05:33:36 +00:00
|
|
|
|
* @returns {{searchFormRef: Ref<UnwrapRef<{}>>, toolConfig: {columnSetting: boolean, striped: boolean, refresh: boolean, height: boolean}, searchFormState: Ref<UnwrapRef<{}>>, tableRef: Ref<UnwrapRef<null>>, selectedRowKeys: Ref<UnwrapRef<*[]>>, columns: Ref<UnwrapRef<unknown>>, options: {rowSelection: {onChange: options.rowSelection.onChange}, alert: {show: boolean, clear: options.alert.clear}}, reset: reset, loadData: (function(*): *), deleteBatchRecords: deleteBatchRecords, deleteRecord: deleteRecord}}
|
|
|
|
|
*/
|
|
|
|
|
export function useTableManagement(apiModule = {}, tableColumns, hasPermData) {
|
|
|
|
|
const searchFormState = ref({})
|
2024-07-28 06:11:55 +00:00
|
|
|
|
const searchFormRef = ref(null)
|
2024-07-27 05:33:36 +00:00
|
|
|
|
const selectedRowKeys = ref([])
|
2024-07-28 06:11:55 +00:00
|
|
|
|
let advanced = ref(false)
|
2024-07-27 05:33:36 +00:00
|
|
|
|
|
2024-07-30 09:47:29 +00:00
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
|
|
const tableRef = ref(null)
|
|
|
|
|
|
2024-07-27 05:33:36 +00:00
|
|
|
|
// 动态列配置
|
|
|
|
|
const columns = ref(tableColumns)
|
|
|
|
|
|
2024-07-30 09:47:29 +00:00
|
|
|
|
// 选择配置
|
|
|
|
|
const options = {
|
|
|
|
|
alert: {
|
|
|
|
|
show: true,
|
|
|
|
|
clear: () => {
|
|
|
|
|
selectedRowKeys.value = []
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
rowSelection: {
|
|
|
|
|
onChange: (selectedRowKey, selectedRows) => {
|
|
|
|
|
selectedRowKeys.value = selectedRowKey
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
toolConfig: { refresh: true, height: true, columnSetting: true, striped: false }
|
|
|
|
|
}
|
2024-07-27 05:33:36 +00:00
|
|
|
|
|
|
|
|
|
// 根据权限添加操作列
|
|
|
|
|
if (hasPerm(hasPermData)) {
|
|
|
|
|
// 判断columns 是否有操作
|
|
|
|
|
const columnsFilter = columns.value.filter((item) => item.dataIndex === 'action')
|
|
|
|
|
if (columnsFilter.length === 0)
|
2024-07-30 09:47:29 +00:00
|
|
|
|
columns.value.unshift({
|
2024-07-27 05:33:36 +00:00
|
|
|
|
title: '操作',
|
|
|
|
|
dataIndex: 'action',
|
|
|
|
|
align: 'center',
|
2024-07-30 09:47:29 +00:00
|
|
|
|
width: 150,
|
|
|
|
|
fixed: 'left'
|
2024-07-27 05:33:36 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加载数据
|
|
|
|
|
const loadData = (parameter) => {
|
|
|
|
|
const searchFormParam = cloneDeep(searchFormState.value)
|
|
|
|
|
return apiModule.page(Object.assign(parameter, searchFormParam)).then((data) => {
|
|
|
|
|
return data
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置
|
|
|
|
|
const reset = () => {
|
|
|
|
|
if (tableRef.value) {
|
2024-07-28 06:11:55 +00:00
|
|
|
|
searchFormRef.value.resetFields()
|
2024-07-27 05:33:36 +00:00
|
|
|
|
tableRef.value.refresh(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除
|
|
|
|
|
const deleteRecord = (record) => {
|
|
|
|
|
let params = [{ id: record.id }]
|
2024-07-28 06:11:55 +00:00
|
|
|
|
return apiModule.delete(params).then(() => {
|
2024-07-27 05:33:36 +00:00
|
|
|
|
if (tableRef.value) {
|
|
|
|
|
tableRef.value.refresh(true)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量删除
|
|
|
|
|
const deleteBatchRecords = (params) => {
|
|
|
|
|
apiModule.delete(params).then(() => {
|
|
|
|
|
if (tableRef.value) {
|
|
|
|
|
tableRef.value.clearRefreshSelected()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 页面跳转
|
|
|
|
|
const navigateTo = (to, params) => {
|
|
|
|
|
router.push({
|
|
|
|
|
path: to,
|
|
|
|
|
query: params
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 06:11:55 +00:00
|
|
|
|
const toggleAdvanced = () => {
|
|
|
|
|
advanced.value = !advanced.value
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 05:33:36 +00:00
|
|
|
|
// 返回Hook的值
|
|
|
|
|
return {
|
|
|
|
|
searchFormState,
|
|
|
|
|
searchFormRef,
|
|
|
|
|
tableRef,
|
|
|
|
|
selectedRowKeys,
|
|
|
|
|
columns,
|
2024-07-30 09:47:29 +00:00
|
|
|
|
options,
|
|
|
|
|
advanced,
|
2024-07-27 05:33:36 +00:00
|
|
|
|
loadData,
|
|
|
|
|
reset,
|
|
|
|
|
deleteRecord,
|
|
|
|
|
deleteBatchRecords,
|
2024-07-28 06:11:55 +00:00
|
|
|
|
navigateTo,
|
2024-07-30 09:47:29 +00:00
|
|
|
|
toggleAdvanced
|
2024-07-27 05:33:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|