139 lines
3.2 KiB
JavaScript
139 lines
3.2 KiB
JavaScript
/**
|
||
* 数据解析工具
|
||
* 用于统一处理后端返回的各种数据结构
|
||
*/
|
||
|
||
/**
|
||
* 智能解析列表数据
|
||
* 支持多种返回格式:
|
||
* - 直接数组: [...]
|
||
* - 分页对象: { records: [...], total: 10 }
|
||
* - 列表对象: { list: [...] }
|
||
* - 嵌套对象: { data: [...] } 或 { data: { records: [...] } }
|
||
*
|
||
* @param {*} response - API响应数据
|
||
* @returns {Array} - 解析后的数组
|
||
*/
|
||
export function parseListData(response) {
|
||
// 如果是null或undefined,返回空数组
|
||
if (!response) {
|
||
return []
|
||
}
|
||
|
||
// 直接返回数组
|
||
if (Array.isArray(response)) {
|
||
return response
|
||
}
|
||
|
||
// 分页对象:{ records: [...], total: 10 }
|
||
if (response.records && Array.isArray(response.records)) {
|
||
return response.records
|
||
}
|
||
|
||
// 列表对象:{ list: [...] }
|
||
if (response.list && Array.isArray(response.list)) {
|
||
return response.list
|
||
}
|
||
|
||
// 嵌套在data中
|
||
if (response.data) {
|
||
// { data: [...] }
|
||
if (Array.isArray(response.data)) {
|
||
return response.data
|
||
}
|
||
|
||
// { data: { records: [...] } }
|
||
if (response.data.records && Array.isArray(response.data.records)) {
|
||
return response.data.records
|
||
}
|
||
|
||
// { data: { list: [...] } }
|
||
if (response.data.list && Array.isArray(response.data.list)) {
|
||
return response.data.list
|
||
}
|
||
}
|
||
|
||
// 如果都不匹配,返回空数组
|
||
console.warn('无法解析列表数据,返回空数组:', response)
|
||
return []
|
||
}
|
||
|
||
/**
|
||
* 智能解析分页数据
|
||
* 返回格式: { list: [], total: 0, hasMore: false }
|
||
*
|
||
* @param {*} response - API响应数据
|
||
* @param {number} pageSize - 每页数量
|
||
* @returns {Object} - { list: [], total: 0, hasMore: false }
|
||
*/
|
||
export function parsePageData(response, pageSize = 10) {
|
||
const list = parseListData(response)
|
||
|
||
let total = 0
|
||
if (response && typeof response === 'object') {
|
||
total = response.total || response.totalCount || response.count || list.length
|
||
}
|
||
|
||
return {
|
||
list,
|
||
total,
|
||
hasMore: list.length >= pageSize
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 智能解析详情数据
|
||
*
|
||
* @param {*} response - API响应数据
|
||
* @returns {Object|null} - 详情对象或null
|
||
*/
|
||
export function parseDetailData(response) {
|
||
if (!response) {
|
||
return null
|
||
}
|
||
|
||
// 如果是对象且不是数组,直接返回
|
||
if (typeof response === 'object' && !Array.isArray(response)) {
|
||
// 如果有data字段,返回data
|
||
if (response.data && typeof response.data === 'object' && !Array.isArray(response.data)) {
|
||
return response.data
|
||
}
|
||
return response
|
||
}
|
||
|
||
return null
|
||
}
|
||
|
||
/**
|
||
* 安全获取对象属性
|
||
*
|
||
* @param {Object} obj - 对象
|
||
* @param {string} path - 属性路径,如 'user.name' 或 'data.records[0].name'
|
||
* @param {*} defaultValue - 默认值
|
||
* @returns {*} - 属性值或默认值
|
||
*/
|
||
export function safeGet(obj, path, defaultValue = undefined) {
|
||
if (!obj || !path) {
|
||
return defaultValue
|
||
}
|
||
|
||
const keys = path.replace(/\[(\d+)\]/g, '.$1').split('.')
|
||
let result = obj
|
||
|
||
for (const key of keys) {
|
||
if (result === null || result === undefined) {
|
||
return defaultValue
|
||
}
|
||
result = result[key]
|
||
}
|
||
|
||
return result === undefined ? defaultValue : result
|
||
}
|
||
|
||
export default {
|
||
parseListData,
|
||
parsePageData,
|
||
parseDetailData,
|
||
safeGet
|
||
}
|