Commit b218f10e25a9364c399a5fe42eedb549f57c01ea

Authored by Netfan
Committed by GitHub
1 parent 834fa7eb

fix(login): login page modal style fixed: #662 (#666)

* fix: catch axios error data on request

* fix(login): login page modal style fixed: #662
src/locales/lang/en/sys.ts
@@ -5,6 +5,7 @@ export default { @@ -5,6 +5,7 @@ export default {
5 errorMessage: 'The operation failed, the system is abnormal!', 5 errorMessage: 'The operation failed, the system is abnormal!',
6 timeoutMessage: 'Login timed out, please log in again!', 6 timeoutMessage: 'Login timed out, please log in again!',
7 apiTimeoutMessage: 'The interface request timed out, please refresh the page and try again!', 7 apiTimeoutMessage: 'The interface request timed out, please refresh the page and try again!',
  8 + apiRequestFailed: 'The interface request failed, please try again later!',
8 networkException: 'network anomaly', 9 networkException: 'network anomaly',
9 networkExceptionMsg: 10 networkExceptionMsg:
10 'Please check if your network connection is normal! The network is abnormal', 11 'Please check if your network connection is normal! The network is abnormal',
src/locales/lang/zh_CN/sys.ts
@@ -5,6 +5,7 @@ export default { @@ -5,6 +5,7 @@ export default {
5 errorMessage: '操作失败,系统异常!', 5 errorMessage: '操作失败,系统异常!',
6 timeoutMessage: '登录超时,请重新登录!', 6 timeoutMessage: '登录超时,请重新登录!',
7 apiTimeoutMessage: '接口请求超时,请刷新页面重试!', 7 apiTimeoutMessage: '接口请求超时,请刷新页面重试!',
  8 + apiRequestFailed: '请求出错,请稍候重试',
8 networkException: '网络异常', 9 networkException: '网络异常',
9 networkExceptionMsg: '请检查您的网络连接是否正常!', 10 networkExceptionMsg: '请检查您的网络连接是否正常!',
10 11
src/store/modules/user.ts
@@ -100,7 +100,7 @@ export const useUserStore = defineStore({ @@ -100,7 +100,7 @@ export const useUserStore = defineStore({
100 !sessionTimeout && goHome && (await router.replace(PageEnum.BASE_HOME)); 100 !sessionTimeout && goHome && (await router.replace(PageEnum.BASE_HOME));
101 return userInfo; 101 return userInfo;
102 } catch (error) { 102 } catch (error) {
103 - return null; 103 + return Promise.reject(error);
104 } 104 }
105 }, 105 },
106 async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) { 106 async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) {
src/utils/http/axios/Axios.ts
@@ -8,7 +8,7 @@ import { AxiosCanceler } from './axiosCancel'; @@ -8,7 +8,7 @@ import { AxiosCanceler } from './axiosCancel';
8 import { isFunction } from '/@/utils/is'; 8 import { isFunction } from '/@/utils/is';
9 import { cloneDeep } from 'lodash-es'; 9 import { cloneDeep } from 'lodash-es';
10 10
11 -import { errorResult } from './const'; 11 +//import { errorResult } from './const';
12 import { ContentTypeEnum } from '/@/enums/httpEnum'; 12 import { ContentTypeEnum } from '/@/enums/httpEnum';
13 import { RequestEnum } from '../../../enums/httpEnum'; 13 import { RequestEnum } from '../../../enums/httpEnum';
14 14
@@ -208,11 +208,15 @@ export class VAxios { @@ -208,11 +208,15 @@ export class VAxios {
208 .request<any, AxiosResponse<Result>>(conf) 208 .request<any, AxiosResponse<Result>>(conf)
209 .then((res: AxiosResponse<Result>) => { 209 .then((res: AxiosResponse<Result>) => {
210 if (transformRequestHook && isFunction(transformRequestHook)) { 210 if (transformRequestHook && isFunction(transformRequestHook)) {
211 - const ret = transformRequestHook(res, opt);  
212 - ret !== errorResult ? resolve(ret) : reject(new Error('request error!')); 211 + try {
  212 + const ret = transformRequestHook(res, opt);
  213 + resolve(ret);
  214 + } catch (err) {
  215 + reject(err || new Error('request error!'));
  216 + }
213 return; 217 return;
214 } 218 }
215 - resolve((res as unknown) as Promise<T>); 219 + resolve(res as unknown as Promise<T>);
216 }) 220 })
217 .catch((e: Error) => { 221 .catch((e: Error) => {
218 if (requestCatchHook && isFunction(requestCatchHook)) { 222 if (requestCatchHook && isFunction(requestCatchHook)) {
src/utils/http/axios/index.ts
@@ -18,7 +18,7 @@ import { getToken } from &#39;/@/utils/auth&#39;; @@ -18,7 +18,7 @@ import { getToken } from &#39;/@/utils/auth&#39;;
18 import { setObjToUrlParams, deepMerge } from '/@/utils'; 18 import { setObjToUrlParams, deepMerge } from '/@/utils';
19 import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; 19 import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
20 20
21 -import { errorResult } from './const'; 21 +//import { errorResult } from './const';
22 import { useI18n } from '/@/hooks/web/useI18n'; 22 import { useI18n } from '/@/hooks/web/useI18n';
23 import { createNow, formatRequestDate } from './helper'; 23 import { createNow, formatRequestDate } from './helper';
24 24
@@ -31,7 +31,7 @@ const { createMessage, createErrorModal } = useMessage(); @@ -31,7 +31,7 @@ const { createMessage, createErrorModal } = useMessage();
31 */ 31 */
32 const transform: AxiosTransform = { 32 const transform: AxiosTransform = {
33 /** 33 /**
34 - * @description: 处理请求数据 34 + * @description: 处理请求数据。如果数据不是预期格式,可直接抛出错误
35 */ 35 */
36 transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { 36 transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => {
37 const { t } = useI18n(); 37 const { t } = useI18n();
@@ -50,7 +50,8 @@ const transform: AxiosTransform = { @@ -50,7 +50,8 @@ const transform: AxiosTransform = {
50 const { data } = res; 50 const { data } = res;
51 if (!data) { 51 if (!data) {
52 // return '[HTTP] Request has no return value'; 52 // return '[HTTP] Request has no return value';
53 - return errorResult; 53 + throw new Error(t('sys.api.apiRequestFailed'));
  54 + //return errorResult;
54 } 55 }
55 // 这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式 56 // 这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式
56 const { code, result, message } = data; 57 const { code, result, message } = data;
@@ -66,8 +67,8 @@ const transform: AxiosTransform = { @@ -66,8 +67,8 @@ const transform: AxiosTransform = {
66 createMessage.error(message); 67 createMessage.error(message);
67 } 68 }
68 } 69 }
69 - Promise.reject(new Error(message));  
70 - return errorResult; 70 + throw new Error(message);
  71 + //return errorResult;
71 } 72 }
72 73
73 // 接口请求成功,直接返回结果 74 // 接口请求成功,直接返回结果
@@ -78,13 +79,13 @@ const transform: AxiosTransform = { @@ -78,13 +79,13 @@ const transform: AxiosTransform = {
78 if (code === ResultEnum.ERROR) { 79 if (code === ResultEnum.ERROR) {
79 if (message) { 80 if (message) {
80 createMessage.error(data.message); 81 createMessage.error(data.message);
81 - Promise.reject(new Error(message)); 82 + throw new Error(message);
82 } else { 83 } else {
83 const msg = t('sys.api.errorMessage'); 84 const msg = t('sys.api.errorMessage');
84 createMessage.error(msg); 85 createMessage.error(msg);
85 - Promise.reject(new Error(msg)); 86 + throw new Error(msg);
86 } 87 }
87 - return errorResult; 88 + //return errorResult;
88 } 89 }
89 // 登录超时 90 // 登录超时
90 if (code === ResultEnum.TIMEOUT) { 91 if (code === ResultEnum.TIMEOUT) {
@@ -93,10 +94,11 @@ const transform: AxiosTransform = { @@ -93,10 +94,11 @@ const transform: AxiosTransform = {
93 title: t('sys.api.operationFailed'), 94 title: t('sys.api.operationFailed'),
94 content: timeoutMsg, 95 content: timeoutMsg,
95 }); 96 });
96 - Promise.reject(new Error(timeoutMsg));  
97 - return errorResult; 97 + throw new Error(timeoutMsg);
  98 + //return errorResult;
98 } 99 }
99 - return errorResult; 100 + throw new Error(t('sys.api.apiRequestFailed'));
  101 + //return errorResult;
100 }, 102 },
101 103
102 // 请求之前处理config 104 // 请求之前处理config
src/views/sys/login/LoginForm.vue
@@ -118,7 +118,7 @@ @@ -118,7 +118,7 @@
118 }, 118 },
119 setup() { 119 setup() {
120 const { t } = useI18n(); 120 const { t } = useI18n();
121 - const { notification } = useMessage(); 121 + const { notification, createErrorModal } = useMessage();
122 const { prefixCls } = useDesign('login'); 122 const { prefixCls } = useDesign('login');
123 const userStore = useUserStore(); 123 const userStore = useUserStore();
124 124
@@ -149,6 +149,7 @@ @@ -149,6 +149,7 @@
149 toRaw({ 149 toRaw({
150 password: data.password, 150 password: data.password,
151 username: data.account, 151 username: data.account,
  152 + mode: 'none', //不要默认的错误提示
152 }) 153 })
153 ); 154 );
154 if (userInfo) { 155 if (userInfo) {
@@ -158,6 +159,12 @@ @@ -158,6 +159,12 @@
158 duration: 3, 159 duration: 3,
159 }); 160 });
160 } 161 }
  162 + } catch (error) {
  163 + createErrorModal({
  164 + title: t('sys.api.errorTip'),
  165 + content: error.message || t('sys.api.networkExceptionMsg'),
  166 + getContainer: () => document.body.querySelector(`.${prefixCls}`) || document.body,
  167 + });
161 } finally { 168 } finally {
162 loading.value = false; 169 loading.value = false;
163 } 170 }