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 loginFun = async (params) => {
|
||||||
const res = await userLogin(params);
|
try {
|
||||||
ElMessage({
|
const res = await userLogin(params);
|
||||||
message: `您好${params.userName},登录成功!`,
|
ElMessage({
|
||||||
type: "success",
|
message: `您好${params.userName},登录成功!`,
|
||||||
});
|
type: "success",
|
||||||
loginSuccess(res);
|
});
|
||||||
return res;
|
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 { login, logout, register } from "@/apis/accountApi";
|
||||||
import { getUserDetailInfo } from "@/apis/auth";
|
import { getUserDetailInfo, getLoginCode } from "@/apis/auth";
|
||||||
import useAuths from "@/hooks/useAuths";
|
import useAuths from "@/hooks/useAuths";
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
|
||||||
@@ -14,6 +14,8 @@ const useUserStore = defineStore("user", {
|
|||||||
icon: null,
|
icon: null,
|
||||||
roles: [],
|
roles: [],
|
||||||
permissions: [],
|
permissions: [],
|
||||||
|
codeImageURL: "",
|
||||||
|
codeUUid: "",
|
||||||
}),
|
}),
|
||||||
getters: {},
|
getters: {},
|
||||||
actions: {
|
actions: {
|
||||||
@@ -112,6 +114,11 @@ const useUserStore = defineStore("user", {
|
|||||||
this.userName = "";
|
this.userName = "";
|
||||||
this.id = "";
|
this.id = "";
|
||||||
},
|
},
|
||||||
|
async updateCodeImage() {
|
||||||
|
const { data } = await getLoginCode();
|
||||||
|
this.codeImageURL = "data:image/jpg;base64," + data.img;
|
||||||
|
this.codeUUid = data.uuid;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
persist: {
|
persist: {
|
||||||
key: "userInfo",
|
key: "userInfo",
|
||||||
|
|||||||
@@ -53,7 +53,7 @@
|
|||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-image
|
<el-image
|
||||||
@click="handleGetCode"
|
@click="handleGetCodeImage"
|
||||||
style="width: 120px; height: 40px; cursor: pointer"
|
style="width: 120px; height: 40px; cursor: pointer"
|
||||||
:src="codeImageURL"
|
:src="codeImageURL"
|
||||||
:fit="fit"
|
:fit="fit"
|
||||||
@@ -160,11 +160,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, reactive, onMounted } from "vue";
|
import { ref, reactive, onMounted, computed } from "vue";
|
||||||
import { useRouter, useRoute } from "vue-router";
|
import { useRouter, useRoute } from "vue-router";
|
||||||
import useAuths from "@/hooks/useAuths";
|
import useAuths from "@/hooks/useAuths";
|
||||||
import { getCodePhone } from "@/apis/accountApi";
|
import { getCodePhone } from "@/apis/accountApi";
|
||||||
import { getLoginCode } from "@/apis/auth";
|
import useUserStore from "@/stores/user";
|
||||||
|
|
||||||
const { loginFun, registerFun } = useAuths();
|
const { loginFun, registerFun } = useAuths();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -184,13 +184,16 @@ const guestlogin = async () => {
|
|||||||
const redirect = route.query?.redirect ?? "/index";
|
const redirect = route.query?.redirect ?? "/index";
|
||||||
router.push(redirect);
|
router.push(redirect);
|
||||||
};
|
};
|
||||||
|
const codeUUid = computed(() => useUserStore().codeUUid);
|
||||||
const login = async (formEl) => {
|
const login = async (formEl) => {
|
||||||
if (!formEl) return;
|
if (!formEl) return;
|
||||||
await formEl.validate((valid) => {
|
await formEl.validate((valid) => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
try {
|
try {
|
||||||
|
loginForm.uuid = codeUUid.value;
|
||||||
loginFun(loginForm);
|
loginFun(loginForm);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error.message, "error.message");
|
||||||
ElMessage({
|
ElMessage({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
type: "error",
|
type: "error",
|
||||||
@@ -200,11 +203,6 @@ const login = async (formEl) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
// 获取图片验证码
|
|
||||||
const codeImageURL = ref("");
|
|
||||||
const handleGetCode = () => {
|
|
||||||
getImageCode();
|
|
||||||
};
|
|
||||||
|
|
||||||
// 注册逻辑
|
// 注册逻辑
|
||||||
const isRegister = ref(true);
|
const isRegister = ref(true);
|
||||||
@@ -284,14 +282,13 @@ const captcha = async () => {
|
|||||||
const handleSignInNow = () => {
|
const handleSignInNow = () => {
|
||||||
isRegister.value = !isRegister.value;
|
isRegister.value = !isRegister.value;
|
||||||
};
|
};
|
||||||
|
// 获取图片验证码
|
||||||
const getImageCode = async () => {
|
const codeImageURL = computed(() => useUserStore().codeImageURL);
|
||||||
const { data } = await getLoginCode();
|
const handleGetCodeImage = () => {
|
||||||
codeImageURL.value = "data:image/jpg;base64," + data.img;
|
useUserStore().updateCodeImage();
|
||||||
loginForm.uuid = data.uuid;
|
|
||||||
};
|
};
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await getImageCode();
|
await useUserStore().updateCodeImage();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
|
|||||||
Reference in New Issue
Block a user