restructure/src/components/DataTable/tools/useDataSource.js

136 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-05-19 17:37:16 +08:00
import { ref, unref, computed, onMounted } from 'vue'
import { isBoolean } from '@/utils/is'
2022-05-23 10:52:21 +08:00
import { toTreeData } from './toTree'
2022-05-19 17:37:16 +08:00
export function useDataSource(propsRef, { getPaginationInfo, setPagination, setLoading, tableData }, emit) {
const dataSourceRef = ref([])
async function fetch(opt) {
try {
/* 设置loading */
setLoading(true)
2022-05-20 08:49:37 +08:00
const { request, pagination, paginationSetting, dataType } = unref(propsRef)
2022-05-19 17:37:16 +08:00
/* 无接口请求中断 */
if (!request) return
/* 获取分页信息 */
const pageField = paginationSetting.pageField
const sizeField = paginationSetting.sizeField
const totalField = paginationSetting.totalField
const listField = paginationSetting.listField
let pageParams = {}
const { page = 1, pageSize = 10 } = unref(getPaginationInfo)
/* 判断是否需要分页信息 */
2022-07-05 14:05:00 +08:00
const noPagination = (isBoolean(pagination) && !pagination) || isBoolean(getPaginationInfo)
if (noPagination) {
2022-05-19 17:37:16 +08:00
pageParams = {}
} else {
pageParams[pageField] = (opt && opt[pageField]) || page
pageParams[sizeField] = pageSize
}
const params = {
...pageParams
}
const response = await request(params)
2022-07-05 14:05:00 +08:00
const res = noPagination ? response : response.data
2022-05-19 17:37:16 +08:00
const resultTotal = res[totalField] || 0
const currentPage = res[pageField]
// 如果数据异常,需获取正确的页码再次执行
if (resultTotal) {
if (page > Math.ceil(resultTotal / pageSize)) {
setPagination({
[pageField]: Math.ceil(resultTotal / pageSize)
})
fetch(opt)
}
}
2022-05-23 10:52:21 +08:00
// 处理数据结构
2022-05-27 15:05:48 +08:00
const resultInfo = res[listField] ? res[listField] : res
2022-05-20 08:49:37 +08:00
dataSourceRef.value = dataType === 'tree' ? dealTree(resultInfo) : resultInfo
2022-05-19 17:37:16 +08:00
setPagination({
[pageField]: currentPage,
[totalField]: Math.ceil(resultTotal / pageSize)
})
/* 更新页码数据 */
if (opt && opt[pageField]) {
setPagination({
[pageField]: opt[pageField] || 1
})
}
emit('fetch-success', {
items: unref(resultInfo),
resultTotal
})
} catch (error) {
console.error(error)
// emit('fetch-error', error)
dataSourceRef.value = []
} finally {
setLoading(false)
}
}
2022-05-23 10:52:21 +08:00
/**
* 递归遍历数据处理成树形结构
* @returns 返回树形结构数据
*/
function dealTree(info) {
2022-05-27 15:05:48 +08:00
const tree = toTreeData(info, 'id', 'pid', 'children')
2022-05-20 08:49:37 +08:00
return tree
}
2022-05-19 17:37:16 +08:00
const getDataSourceRef = computed(() => {
const dataSource = unref(dataSourceRef)
if (!dataSource || dataSource.length === 0) {
return unref(dataSourceRef)
}
return unref(dataSourceRef)
})
function getDataSource() {
return getDataSourceRef.value
}
function setTableData(values) {
dataSourceRef.value = values
}
const getRowKey = computed(() => {
const { rowKey } = unref(propsRef)
return rowKey || (() => {
return 'key'
})
})
async function reload(opt) {
await fetch(opt)
}
2022-05-24 09:09:30 +08:00
async function reFetch(opt) {
const { paginationSetting } = unref(propsRef)
const pageField = paginationSetting.pageField
const sizeField = paginationSetting.sizeField
const pageSize = paginationSetting.pageSize
setPagination({
[pageField]: 1,
[sizeField]: pageSize
})
await fetch(opt)
}
2022-05-19 17:37:16 +08:00
onMounted(() => {
setTimeout(() => {
fetch()
}, 15)
})
return {
fetch,
getDataSourceRef,
getDataSource,
setTableData,
getRowKey,
2022-05-24 09:09:30 +08:00
reload,
reFetch
2022-05-19 17:37:16 +08:00
}
}