mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-30 23:26:41 +08:00
feat(utils): deep fields in the response can be specified in the backendConfig
This commit is contained in:
parent
506ffb8adf
commit
69092a4529
@ -7,7 +7,8 @@ import {
|
|||||||
handleBackendError,
|
handleBackendError,
|
||||||
handleResponseError,
|
handleResponseError,
|
||||||
handleServiceResult,
|
handleServiceResult,
|
||||||
transformRequestData
|
transformRequestData,
|
||||||
|
getDeepResponseWithKey
|
||||||
} from '@/utils';
|
} from '@/utils';
|
||||||
import { handleRefreshToken } from './helpers';
|
import { handleRefreshToken } from './helpers';
|
||||||
|
|
||||||
@ -64,13 +65,16 @@ export default class CustomAxiosInstance {
|
|||||||
if (status === 200 || status < 300 || status === 304) {
|
if (status === 200 || status < 300 || status === 304) {
|
||||||
const backend = response.data;
|
const backend = response.data;
|
||||||
const { codeKey, dataKey, successCode } = this.backendConfig;
|
const { codeKey, dataKey, successCode } = this.backendConfig;
|
||||||
|
|
||||||
|
const code = getDeepResponseWithKey(backend, codeKey);
|
||||||
// 请求成功
|
// 请求成功
|
||||||
if (backend[codeKey] === successCode) {
|
if (code === successCode) {
|
||||||
return handleServiceResult(null, backend[dataKey]);
|
const data = getDeepResponseWithKey(backend, dataKey);
|
||||||
|
return handleServiceResult(null, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// token失效, 刷新token
|
// token失效, 刷新token
|
||||||
if (REFRESH_TOKEN_CODE.includes(backend[codeKey])) {
|
if (REFRESH_TOKEN_CODE.includes(code)) {
|
||||||
const config = await handleRefreshToken(response.config);
|
const config = await handleRefreshToken(response.config);
|
||||||
if (config) {
|
if (config) {
|
||||||
return this.instance.request(config);
|
return this.instance.request(config);
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
REQUEST_TIMEOUT_CODE,
|
REQUEST_TIMEOUT_CODE,
|
||||||
REQUEST_TIMEOUT_MSG
|
REQUEST_TIMEOUT_MSG
|
||||||
} from '@/config';
|
} from '@/config';
|
||||||
|
import { getDeepResponseWithKey } from '@/utils';
|
||||||
import { exeStrategyActions } from '../common';
|
import { exeStrategyActions } from '../common';
|
||||||
import { showErrorMsg } from './msg';
|
import { showErrorMsg } from './msg';
|
||||||
|
|
||||||
@ -91,8 +92,8 @@ export function handleBackendError(backendResult: Record<string, any>, config: S
|
|||||||
const { codeKey, msgKey } = config;
|
const { codeKey, msgKey } = config;
|
||||||
const error: Service.RequestError = {
|
const error: Service.RequestError = {
|
||||||
type: 'backend',
|
type: 'backend',
|
||||||
code: backendResult[codeKey],
|
code: getDeepResponseWithKey(backendResult, codeKey),
|
||||||
msg: backendResult[msgKey]
|
msg: getDeepResponseWithKey(backendResult, msgKey)
|
||||||
};
|
};
|
||||||
|
|
||||||
showErrorMsg(error);
|
showErrorMsg(error);
|
||||||
|
@ -59,3 +59,11 @@ async function transformFile(formData: FormData, key: string, file: File[] | Fil
|
|||||||
formData.append(key, file);
|
formData.append(key, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getDeepResponseWithKey(res: any, key: string) {
|
||||||
|
let result = res;
|
||||||
|
key.split('.').forEach(item => {
|
||||||
|
result = result[item];
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user