cmc-web/packages/common/hooks/useTable.ts

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
import { ref, toRefs, reactive } from 'vue'
import { MessageBox, Message } from 'element-ui'
import { handleSearchParam } from '@cmp/cmp-element'
interface IRemoveService {
(id: number, params?: any): Promise<Base.IResponseData>
}
2024-08-21 01:17:14 +00:00
interface IConfigs {
2024-08-20 12:11:31 +00:00
getService: {
(params: Base.IListParams): Promise<Base.IResponseList>
2024-08-21 01:17:14 +00:00
}
removeService?: IRemoveService
2024-08-20 12:11:31 +00:00
listFormat?: {
(data: any): any[]
2024-08-21 01:17:14 +00:00
}
2024-08-20 12:11:31 +00:00
afterGetList?: {
(): void
2024-08-21 01:17:14 +00:00
}
rows?: number
params?: any
initParams?: any
2024-08-20 12:11:31 +00:00
deleteTipKey?: string
}
2024-08-21 01:17:14 +00:00
interface IRecord {
id: number
2024-08-20 12:11:31 +00:00
name: string
}
2024-08-21 01:17:14 +00:00
export function useDelete(removeService: IRemoveService, getData: { (): void }, deleteTipKey = 'name') {
2024-08-20 12:11:31 +00:00
function handleDelete(record: any, params: any) {
MessageBox.confirm(`您确定要删除【${record[deleteTipKey]}】吗?`, '提示', {
confirmButtonClass: 'el-button--danger',
type: 'warning'
2024-08-21 01:17:14 +00:00
})
.then(async () => {
const res = await removeService(record.id, params)
if (res.success) {
Message.success(res.message)
getData()
}
})
.catch(e => {})
2024-08-20 12:11:31 +00:00
}
return {
handleDelete
}
}
2024-08-21 01:17:14 +00:00
export default function <T = any>(configs: IConfigs) {
const { getService, removeService, listFormat, rows = 10, params = {}, afterGetList, initParams, deleteTipKey = 'name' } = configs
const loading = ref(false)
2024-08-20 12:11:31 +00:00
const state: Base.IListState<T> = reactive({
list: [],
total: 0,
params: {
page: 1,
rows,
...params
}
2024-08-21 01:17:14 +00:00
})
2024-08-20 12:11:31 +00:00
// if (initParams) {
// state.params.params = handleSearchParam(initParams)
// }
2024-08-21 01:17:14 +00:00
async function getList() {
loading.value = true
2024-08-20 12:11:31 +00:00
try {
2024-08-21 01:17:14 +00:00
const data = await getService(state.params)
loading.value = false
2024-08-20 12:11:31 +00:00
if (data.success) {
if (listFormat) {
state.list = listFormat(data.data.rows)
} else {
state.list = data.data.rows
}
2024-08-21 01:17:14 +00:00
state.total = data.data.total
2024-08-20 12:11:31 +00:00
afterGetList && afterGetList()
}
} catch (e) {
2024-08-21 01:17:14 +00:00
loading.value = false
2024-08-20 12:11:31 +00:00
}
}
const { handleDelete } = useDelete(removeService as IRemoveService, getList, deleteTipKey)
return {
loading,
...toRefs(state),
getList,
handleDelete
}
}
export function useSelect() {
const selectList = ref<object[]>([])
function handleSelectItem(selection: object[], row: object) {
selectList.value = selection
}
function handleSelectAll(selection: object[]) {
selectList.value = selection
}
return {
2024-08-21 01:17:14 +00:00
selectList,
handleSelectItem,
handleSelectAll
2024-08-20 12:11:31 +00:00
}
}