diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index d6b310c4..515cb70d 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -135,14 +135,14 @@ function createRequestClient(baseURL: string) { }); // 通用的错误处理, 如果没有进入上面的错误处理逻辑,就会进入这里 - // 主要处理http状态码不为200的情况 必须放在在下面的响应拦截器之前 + // 主要处理http状态码不为200(如网络异常/离线)的情况 必须放在在下面的响应拦截器之前 client.addResponseInterceptor( errorMessageResponseInterceptor((msg: string) => message.error(msg)), ); client.addResponseInterceptor({ fulfilled: async (response) => { - const encryptKey = (response.headers || {})['encrypt-key']; + const encryptKey = (response.headers ?? {})['encrypt-key']; if (encryptKey) { /** RSA私钥解密 拿到解密秘钥的base64 */ const base64Str = encryptUtil.decrypt(encryptKey); @@ -168,7 +168,6 @@ function createRequestClient(baseURL: string) { /** * 需要判断下载二进制的情况 正常是返回二进制 报错会返回json * 当type为blob且content-type为application/json时 则判断已经下载出错 - * 不能直接去判断blob的类型 因为下载json类型和报错的类型是一致的 需要从响应头判断 */ if ( response.config.responseType === 'blob' && @@ -180,7 +179,7 @@ function createRequestClient(baseURL: string) { response.data = JSON.parse(await blob.text()); // 然后按正常逻辑执行下面的代码(判断业务状态码) } else { - // 不为blob 直接返回 + // 其他情况 直接返回 return response.data; } } @@ -190,10 +189,10 @@ function createRequestClient(baseURL: string) { throw new Error($t('http.apiRequestFailed')); } - // ruoyi-plus没有采用严格的{code, msg, data}模式 + // 后端并没有采用严格的{code, msg, data}模式 const { code, data, msg, ...other } = axiosResponseData; - // 这里逻辑可以根据项目进行修改 + // 业务状态码为200则请求成功 const hasSuccess = Reflect.has(axiosResponseData, 'code') && code === 200; if (hasSuccess) { let successMsg = msg; @@ -210,11 +209,13 @@ function createRequestClient(baseURL: string) { } else if (response.config.successMessageMode === 'message') { message.success(successMsg); } + // 分页情况下为code msg rows total 并没有data字段 // 如果有data 直接返回data 没有data将剩余参数(...other)封装为data返回 - // 需要考虑data为null的情况(比如查询为空) + // 需要考虑data为null的情况(比如查询为空) 所以这里直接判断undefined if (data !== undefined) { return data; } + // 没有data 将其他参数包装为data return other; } // 在此处根据自己项目的实际情况对不同的code执行不同的操作