Files
AllinSSL/frontend/packages/vue/hooks/src/axios/model/response-middle.ts
chudong f1a75afaba 【同步】前端项目源码
【修复】工作流兼容问题
2025-05-10 11:53:11 +08:00

41 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { AxiosResponse } from 'axios'
import { responseMiddleware } from './other'
import { isObject, isString } from '@baota/utils/type'
import { hasRequiredKeys } from '@baota/utils/data'
/*
* 预处理响应数据中间件,该组件运行在
*/
export const processPanelDataMiddle = responseMiddleware((response: AxiosResponse) => {
const defaultOption = {
data: {}, // 请求数据
code: 0, // 状态码200为成功其他为失败
msg: 'success', // 提示信息
status: true, // 接口状态
default: true, // 默认状态,用于判断当前数据是否为默认数据,没有经过处理
cache: false, // 是否缓存,基于前端缓存
oldData: null, // 旧数据用于保存原始shuj
timestamp: 0, // 时间戳
}
const { data } = response
const { custom } = response.config
const result = { ...defaultOption } // 拷贝一份数据
// 监测字段是否存在
if (isObject(data)) {
const hasRequiredKeysCurry = hasRequiredKeys(data)
const hasStatus = hasRequiredKeysCurry(['status']) // 是否存在status字段
const hasMsg = hasRequiredKeysCurry(['msg']) // 是否存在msg字段
const hasData = hasRequiredKeysCurry(['data']) // 是否存在data字段
if (hasStatus) result.status = (data as { status: boolean }).status
if (hasMsg) result.msg = (data as { msg: string }).msg
if (hasData) result.data = (data as { data: any }).data
result.default = false
} else {
result.data = data
result.default = true // 原数据仅移动至data
}
result.oldData = data
if (isString(data)) return response
return response
})