feat: 接入真实登录鉴权流程
This commit is contained in:
+39
-28
@@ -10,7 +10,7 @@ import { useEventListener } from "@vueuse/core";
|
||||
import type { FormInstance } from "element-plus";
|
||||
import { useLayout } from "@/layout/hooks/useLayout";
|
||||
import { useUserStoreHook } from "@/store/modules/user";
|
||||
import { initRouter, getTopMenu } from "@/router/utils";
|
||||
import { getTopMenu } from "@/router/utils";
|
||||
import { bg, avatar, illustration } from "./utils/static";
|
||||
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
|
||||
import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
|
||||
@@ -38,38 +38,49 @@ const { title } = useNav();
|
||||
|
||||
const ruleForm = reactive({
|
||||
username: "admin",
|
||||
password: "admin123"
|
||||
password: "Admin@123456"
|
||||
});
|
||||
|
||||
function getErrorMessage(error: unknown, fallback: string) {
|
||||
const message = (
|
||||
error as { response?: { data?: { error?: { message?: string } } } }
|
||||
)?.response?.data?.error?.message;
|
||||
|
||||
if (message) return message;
|
||||
if (error instanceof Error && error.message) return error.message;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const onLogin = async (formEl: FormInstance | undefined) => {
|
||||
if (!formEl) return;
|
||||
await formEl.validate(valid => {
|
||||
if (valid) {
|
||||
loading.value = true;
|
||||
useUserStoreHook()
|
||||
.loginByUsername({
|
||||
username: ruleForm.username,
|
||||
password: ruleForm.password
|
||||
})
|
||||
.then(res => {
|
||||
if (res.success) {
|
||||
// 获取后端路由
|
||||
return initRouter().then(() => {
|
||||
disabled.value = true;
|
||||
router
|
||||
.push(getTopMenu(true).path)
|
||||
.then(() => {
|
||||
message("登录成功", { type: "success" });
|
||||
})
|
||||
.finally(() => (disabled.value = false));
|
||||
});
|
||||
} else {
|
||||
message("登录失败", { type: "error" });
|
||||
}
|
||||
})
|
||||
.finally(() => (loading.value = false));
|
||||
|
||||
try {
|
||||
await formEl.validate();
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
loading.value = true;
|
||||
try {
|
||||
const res = await useUserStoreHook().loginByUsername({
|
||||
username: ruleForm.username,
|
||||
password: ruleForm.password
|
||||
});
|
||||
|
||||
if (res.success) {
|
||||
disabled.value = true;
|
||||
const redirect = router.currentRoute.value.query.redirect as
|
||||
| string
|
||||
| undefined;
|
||||
await router.push(redirect || getTopMenu(true)?.path || "/access-denied");
|
||||
message("登录成功", { type: "success" });
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
message(getErrorMessage(error, "登录失败"), { type: "error" });
|
||||
} finally {
|
||||
disabled.value = false;
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const immediateDebounce: any = debounce(
|
||||
|
||||
Reference in New Issue
Block a user