fix:修复验证码报错刷新问题
This commit is contained in:
@@ -86,7 +86,7 @@ service.interceptors.response.use(
|
||||
}
|
||||
}
|
||||
}
|
||||
return Promise.reject(new Error(error));
|
||||
return Promise.reject(error.response);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -89,13 +89,20 @@ export default function useAuths(opt) {
|
||||
|
||||
// 用户名密码登录
|
||||
const loginFun = async (params) => {
|
||||
const res = await userLogin(params);
|
||||
ElMessage({
|
||||
message: `您好${params.userName},登录成功!`,
|
||||
type: "success",
|
||||
});
|
||||
loginSuccess(res);
|
||||
return res;
|
||||
try {
|
||||
const res = await userLogin(params);
|
||||
ElMessage({
|
||||
message: `您好${params.userName},登录成功!`,
|
||||
type: "success",
|
||||
});
|
||||
loginSuccess(res);
|
||||
return res;
|
||||
} catch (error) {
|
||||
const { data } = error;
|
||||
if (error.status === 403 && data.error?.message === "验证码错误") {
|
||||
useUserStore().updateCodeImage();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 获取用户基本信息、角色、菜单权限
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { login, logout, register } from "@/apis/accountApi";
|
||||
import { getUserDetailInfo } from "@/apis/auth";
|
||||
import { getUserDetailInfo, getLoginCode } from "@/apis/auth";
|
||||
import useAuths from "@/hooks/useAuths";
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
@@ -14,6 +14,8 @@ const useUserStore = defineStore("user", {
|
||||
icon: null,
|
||||
roles: [],
|
||||
permissions: [],
|
||||
codeImageURL: "",
|
||||
codeUUid: "",
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
@@ -112,6 +114,11 @@ const useUserStore = defineStore("user", {
|
||||
this.userName = "";
|
||||
this.id = "";
|
||||
},
|
||||
async updateCodeImage() {
|
||||
const { data } = await getLoginCode();
|
||||
this.codeImageURL = "data:image/jpg;base64," + data.img;
|
||||
this.codeUUid = data.uuid;
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
key: "userInfo",
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-image
|
||||
@click="handleGetCode"
|
||||
@click="handleGetCodeImage"
|
||||
style="width: 120px; height: 40px; cursor: pointer"
|
||||
:src="codeImageURL"
|
||||
:fit="fit"
|
||||
@@ -160,11 +160,11 @@
|
||||
</div>
|
||||
</template>
|
||||
<script setup>
|
||||
import { ref, reactive, onMounted } from "vue";
|
||||
import { ref, reactive, onMounted, computed } from "vue";
|
||||
import { useRouter, useRoute } from "vue-router";
|
||||
import useAuths from "@/hooks/useAuths";
|
||||
import { getCodePhone } from "@/apis/accountApi";
|
||||
import { getLoginCode } from "@/apis/auth";
|
||||
import useUserStore from "@/stores/user";
|
||||
|
||||
const { loginFun, registerFun } = useAuths();
|
||||
const router = useRouter();
|
||||
@@ -184,13 +184,16 @@ const guestlogin = async () => {
|
||||
const redirect = route.query?.redirect ?? "/index";
|
||||
router.push(redirect);
|
||||
};
|
||||
const codeUUid = computed(() => useUserStore().codeUUid);
|
||||
const login = async (formEl) => {
|
||||
if (!formEl) return;
|
||||
await formEl.validate((valid) => {
|
||||
if (valid) {
|
||||
try {
|
||||
loginForm.uuid = codeUUid.value;
|
||||
loginFun(loginForm);
|
||||
} catch (error) {
|
||||
console.log(error.message, "error.message");
|
||||
ElMessage({
|
||||
message: error.message,
|
||||
type: "error",
|
||||
@@ -200,11 +203,6 @@ const login = async (formEl) => {
|
||||
}
|
||||
});
|
||||
};
|
||||
// 获取图片验证码
|
||||
const codeImageURL = ref("");
|
||||
const handleGetCode = () => {
|
||||
getImageCode();
|
||||
};
|
||||
|
||||
// 注册逻辑
|
||||
const isRegister = ref(true);
|
||||
@@ -284,14 +282,13 @@ const captcha = async () => {
|
||||
const handleSignInNow = () => {
|
||||
isRegister.value = !isRegister.value;
|
||||
};
|
||||
|
||||
const getImageCode = async () => {
|
||||
const { data } = await getLoginCode();
|
||||
codeImageURL.value = "data:image/jpg;base64," + data.img;
|
||||
loginForm.uuid = data.uuid;
|
||||
// 获取图片验证码
|
||||
const codeImageURL = computed(() => useUserStore().codeImageURL);
|
||||
const handleGetCodeImage = () => {
|
||||
useUserStore().updateCodeImage();
|
||||
};
|
||||
onMounted(async () => {
|
||||
await getImageCode();
|
||||
await useUserStore().updateCodeImage();
|
||||
});
|
||||
</script>
|
||||
<style scoped lang="scss">
|
||||
|
||||
Reference in New Issue
Block a user