Skip to content

Commit 7f96859

Browse files
committed
fix: 登入失败刷新验证码;统一处理接口响应错误
1 parent 17ca835 commit 7f96859

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/router/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ import {
4343
* 如何排除文件请看:https://cn.vitejs.dev/guide/features.html#negative-patterns
4444
*/
4545
const modules: Record<string, any> = import.meta.glob(
46-
["./modules/**/**.ts", "!./modules/**/remaining.ts"],
46+
[
47+
// "./modules/**/**.ts", // 可以看见全部示例页面和组件
48+
"./modules/home.ts",
49+
"!./modules/**/remaining.ts"
50+
],
4751
{
4852
eager: true
4953
}

src/utils/http/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from "./types.d";
1212
import { stringify } from "qs";
1313
import NProgress from "../progress";
14+
import { message } from "@/utils/message";
1415
import { getToken, formatToken } from "@/utils/auth";
1516
import { useUserStoreHook } from "@/store/modules/user";
1617

@@ -29,6 +30,13 @@ const defaultConfig: AxiosRequestConfig = {
2930
}
3031
};
3132

33+
export type AxiosResponseData = {
34+
ret: number;
35+
msg: string;
36+
data?: Object | Array<any>;
37+
success: boolean;
38+
};
39+
3240
class PureHttp {
3341
constructor() {
3442
this.httpInterceptorsRequest();
@@ -121,6 +129,17 @@ class PureHttp {
121129
instance.interceptors.response.use(
122130
(response: PureHttpResponse) => {
123131
const $config = response.config;
132+
const respData: AxiosResponseData = response.data;
133+
// fixMe 处理后端统一http status_code == 200 的情况
134+
if (respData.ret >= 400) {
135+
if (respData.ret < 500) {
136+
// message("身份信息已失效", { type: "error" });
137+
useUserStoreHook().logOut();
138+
}
139+
NProgress.done();
140+
message(`${respData.ret}: ${respData.msg}`, { type: "error" });
141+
return Promise.reject(response);
142+
}
124143
// 关闭进度条动画
125144
NProgress.done();
126145
// 优先判断post/get等方法是否传入回调,否则执行初始化设置等回调
@@ -139,6 +158,7 @@ class PureHttp {
139158
$error.isCancelRequest = Axios.isCancel($error);
140159
// 关闭进度条动画
141160
NProgress.done();
161+
message("后端服务异常", { type: "error" });
142162
// 所有的响应异常 区分来源为取消请求/非取消请求
143163
return Promise.reject($error);
144164
}

src/views/login/index.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ defineOptions({
3939
});
4040
4141
const imgCode = ref("");
42+
const imgVerifyCodeComp = ref(null);
4243
const loginDay = ref(7);
4344
const router = useRouter();
4445
const loading = ref(false);
@@ -63,6 +64,12 @@ const ruleForm = reactive({
6364
verifyCode: ""
6465
});
6566
67+
/** 重置图片验证码 */
68+
function clearVerifyCode() {
69+
ruleForm.verifyCode = "";
70+
imgVerifyCodeComp.value.getImgCode();
71+
}
72+
6673
const onLogin = async (formEl: FormInstance | undefined) => {
6774
if (!formEl) return;
6875
await formEl.validate(valid => {
@@ -89,6 +96,7 @@ const onLogin = async (formEl: FormInstance | undefined) => {
8996
message(t("login.pureLoginFail"), { type: "error" });
9097
}
9198
})
99+
.catch(e => { clearVerifyCode(); })
92100
.finally(() => (loading.value = false));
93101
}
94102
});
@@ -228,7 +236,7 @@ watch(loginDay, value => {
228236
:prefix-icon="useRenderIcon(Keyhole)"
229237
>
230238
<template v-slot:append>
231-
<ReImageVerify v-model:code="imgCode" />
239+
<ReImageVerify ref="imgVerifyCodeComp" v-model:code="imgCode" />
232240
</template>
233241
</el-input>
234242
</el-form-item>

0 commit comments

Comments
 (0)