buddhism/pages/institutionalStructure/mixins/data-manager.js

235 lines
5.8 KiB
JavaScript
Raw Normal View History

/**
* 数据管理 Mixin
* 提供通用的数据获取分页加载功能
* 支持多种数据格式和灵活的配置
*/
export const dataManagerMixin = {
data() {
return {
// 数据数组
2025-08-14 11:22:53 +08:00
dataList: [],
// 加载状态
loading: false,
// 分页参数
pageNum: 1,
pageSize: 10,
hasMore: true,
// 总数据量
total: 0,
// 当前查询参数
2025-08-14 11:22:53 +08:00
currentParams: {},
}
},
methods: {
/**
* 获取数据
* @param {Object} options 配置选项
* @param {boolean} options.isLoadMore 是否为加载更多
* @param {Function} options.apiCall API调用函数
* @param {Function} options.dataTransformer 数据转换函数
* @param {Object} options.params 查询参数
* @param {string} options.dataPath 数据路径 'data.list.rows'
* @param {string} options.totalPath 总数路径 'data.total'
* @param {Function} options.onSuccess 成功回调
* @param {Function} options.onError 错误回调
* @param {boolean} options.showLoading 是否显示loading
* @param {boolean} options.showError 是否显示错误提示
*/
async fetchData(options = {}) {
const {
isLoadMore = false,
apiCall,
dataTransformer,
params = {},
dataPath = 'rows',
totalPath = 'total',
onSuccess,
onError,
showLoading = true,
2025-08-14 11:22:53 +08:00
showError = true,
} = options
if (!apiCall) {
console.error('fetchData: apiCall 是必需的')
return
}
// 设置loading状态
if (showLoading) {
this.loading = true
}
2025-08-14 11:22:53 +08:00
try {
// 更新页码
if (isLoadMore) {
this.pageNum++
} else {
this.pageNum = 1
}
2025-08-14 11:22:53 +08:00
// 构建请求参数
const requestParams = {
pageNum: this.pageNum,
pageSize: this.pageSize,
...this.currentParams,
2025-08-14 11:22:53 +08:00
...params,
}
2025-08-14 11:22:53 +08:00
// 调用API
const response = await apiCall(requestParams)
2025-08-14 11:22:53 +08:00
console.log('API响应:', response)
console.log('数据路径:', dataPath)
console.log('总数路径:', totalPath)
2025-08-14 11:22:53 +08:00
if (response.code === 200) {
// 根据路径获取数据
const dataArray = this.getNestedValue(response, dataPath) || []
const total = this.getNestedValue(response, totalPath) || 0
2025-08-14 11:22:53 +08:00
console.log('提取的数据数组:', dataArray)
console.log('总数:', total)
2025-08-14 11:22:53 +08:00
// 转换数据
const newData = dataTransformer ? dataTransformer(dataArray) : dataArray
2025-08-14 11:22:53 +08:00
console.log('转换后的数据:', newData)
2025-08-14 11:22:53 +08:00
// 更新数据
if (isLoadMore) {
this.dataList = [...this.dataList, ...newData]
} else {
this.dataList = newData
}
2025-08-14 11:22:53 +08:00
console.log('更新后的 dataList:', this.dataList)
2025-08-14 11:22:53 +08:00
// 更新状态
this.total = total
this.hasMore = newData.length === this.pageSize
this.currentParams = { ...requestParams }
2025-08-14 11:22:53 +08:00
// 成功回调
if (onSuccess) {
onSuccess(newData, response)
}
} else {
const errorMsg = response.msg || '获取数据失败'
if (showError) {
uni.showToast({
title: errorMsg,
2025-08-14 11:22:53 +08:00
icon: 'none',
})
}
if (onError) {
onError(errorMsg, response)
}
}
} catch (error) {
console.error('获取数据失败:', error)
const errorMsg = '网络错误'
if (showError) {
uni.showToast({
title: errorMsg,
2025-08-14 11:22:53 +08:00
icon: 'none',
})
}
if (onError) {
onError(errorMsg, error)
}
} finally {
if (showLoading) {
this.loading = false
}
}
},
/**
* 刷新数据
* @param {Object} options 配置选项
*/
refreshData(options = {}) {
return this.fetchData({
isLoadMore: false,
2025-08-14 11:22:53 +08:00
...options,
})
},
/**
* 加载更多数据
* @param {Object} options 配置选项
*/
loadMoreData(options = {}) {
if (this.hasMore && !this.loading) {
return this.fetchData({
isLoadMore: true,
2025-08-14 11:22:53 +08:00
...options,
})
}
},
/**
* 搜索数据
* @param {Object} searchParams 搜索参数
* @param {Object} options 配置选项
*/
searchData(searchParams = {}, options = {}) {
return this.fetchData({
isLoadMore: false,
params: searchParams,
2025-08-14 11:22:53 +08:00
...options,
})
},
/**
* 重置分页状态
*/
resetPagination() {
this.pageNum = 1
2025-08-14 11:22:53 +08:00
this.hasMore = true //可能就单页
this.dataList = []
this.total = 0
this.currentParams = {}
},
/**
* 获取嵌套对象的值
* @param {Object} obj 对象
* @param {string} path 路径 'data.list.rows'
* @returns {*}
*/
getNestedValue(obj, path) {
if (!path) return obj
return path.split('.').reduce((current, key) => {
return current && current[key] !== undefined ? current[key] : null
}, obj)
},
/**
* 设置分页大小
* @param {number} pageSize 分页大小
*/
setPageSize(pageSize) {
this.pageSize = pageSize
this.resetPagination()
},
/**
* 获取当前数据状态
* @returns {Object} 数据状态
*/
getDataState() {
return {
dataList: this.dataList,
loading: this.loading,
pageNum: this.pageNum,
pageSize: this.pageSize,
hasMore: this.hasMore,
total: this.total,
2025-08-14 11:22:53 +08:00
currentParams: this.currentParams,
}
2025-08-14 11:22:53 +08:00
},
},
}