2022-05-19 17:37:16 +08:00
|
|
|
import { ref, unref, computed, onMounted } from 'vue'
|
|
|
|
|
import { isBoolean } from '@/utils/is'
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
/* 判断是否需要分页信息 */
|
|
|
|
|
if ((isBoolean(pagination) && !pagination) || isBoolean(getPaginationInfo)) {
|
|
|
|
|
pageParams = {}
|
|
|
|
|
} else {
|
|
|
|
|
pageParams[pageField] = (opt && opt[pageField]) || page
|
|
|
|
|
pageParams[sizeField] = pageSize
|
|
|
|
|
}
|
|
|
|
|
const params = {
|
|
|
|
|
...pageParams
|
|
|
|
|
}
|
|
|
|
|
const response = await request(params)
|
|
|
|
|
const res = response.data
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const resultInfo = res[listField] ? res[listField] : []
|
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-20 08:49:37 +08:00
|
|
|
function dealTree() {
|
|
|
|
|
const tree = []
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
fetch()
|
|
|
|
|
}, 15)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fetch,
|
|
|
|
|
getDataSourceRef,
|
|
|
|
|
getDataSource,
|
|
|
|
|
setTableData,
|
|
|
|
|
getRowKey,
|
|
|
|
|
reload
|
|
|
|
|
}
|
|
|
|
|
}
|