mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-07 00:47:19 +08:00
perf: 优化登录操作,防止在网络较慢情况下多次触发登录接口 (#880)
* fix(function): 修复点击登录后再连续敲击回车会不断触发onLogin函数的问题
This commit is contained in:
parent
c5e280307e
commit
c1eaeeb309
@ -1,13 +1,4 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {
|
|
||||||
ref,
|
|
||||||
toRaw,
|
|
||||||
reactive,
|
|
||||||
watch,
|
|
||||||
computed,
|
|
||||||
onMounted,
|
|
||||||
onBeforeUnmount
|
|
||||||
} from "vue";
|
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
import Motion from "./utils/motion";
|
import Motion from "./utils/motion";
|
||||||
import { useRouter } from "vue-router";
|
import { useRouter } from "vue-router";
|
||||||
@ -15,10 +6,12 @@ import { message } from "@/utils/message";
|
|||||||
import { loginRules } from "./utils/rule";
|
import { loginRules } from "./utils/rule";
|
||||||
import phone from "./components/phone.vue";
|
import phone from "./components/phone.vue";
|
||||||
import TypeIt from "@/components/ReTypeit";
|
import TypeIt from "@/components/ReTypeit";
|
||||||
|
import { debounce } from "@pureadmin/utils";
|
||||||
import qrCode from "./components/qrCode.vue";
|
import qrCode from "./components/qrCode.vue";
|
||||||
import regist from "./components/regist.vue";
|
import regist from "./components/regist.vue";
|
||||||
import update from "./components/update.vue";
|
import update from "./components/update.vue";
|
||||||
import { useNav } from "@/layout/hooks/useNav";
|
import { useNav } from "@/layout/hooks/useNav";
|
||||||
|
import { useEventListener } from "@vueuse/core";
|
||||||
import type { FormInstance } from "element-plus";
|
import type { FormInstance } from "element-plus";
|
||||||
import { $t, transformI18n } from "@/plugins/i18n";
|
import { $t, transformI18n } from "@/plugins/i18n";
|
||||||
import { operates, thirdParty } from "./utils/enums";
|
import { operates, thirdParty } from "./utils/enums";
|
||||||
@ -27,6 +20,7 @@ import { useUserStoreHook } from "@/store/modules/user";
|
|||||||
import { initRouter, getTopMenu } from "@/router/utils";
|
import { initRouter, getTopMenu } from "@/router/utils";
|
||||||
import { bg, avatar, illustration } from "./utils/static";
|
import { bg, avatar, illustration } from "./utils/static";
|
||||||
import { ReImageVerify } from "@/components/ReImageVerify";
|
import { ReImageVerify } from "@/components/ReImageVerify";
|
||||||
|
import { ref, toRaw, reactive, watch, computed } from "vue";
|
||||||
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
|
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
|
||||||
import { useTranslationLang } from "@/layout/hooks/useTranslationLang";
|
import { useTranslationLang } from "@/layout/hooks/useTranslationLang";
|
||||||
import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
|
import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
|
||||||
@ -48,6 +42,7 @@ const loginDay = ref(7);
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const checked = ref(false);
|
const checked = ref(false);
|
||||||
|
const disabled = ref(false);
|
||||||
const ruleFormRef = ref<FormInstance>();
|
const ruleFormRef = ref<FormInstance>();
|
||||||
const currentPage = computed(() => {
|
const currentPage = computed(() => {
|
||||||
return useUserStoreHook().currentPage;
|
return useUserStoreHook().currentPage;
|
||||||
@ -68,42 +63,41 @@ const ruleForm = reactive({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const onLogin = async (formEl: FormInstance | undefined) => {
|
const onLogin = async (formEl: FormInstance | undefined) => {
|
||||||
loading.value = true;
|
|
||||||
if (!formEl) return;
|
if (!formEl) return;
|
||||||
await formEl.validate((valid, fields) => {
|
await formEl.validate((valid, fields) => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
|
loading.value = true;
|
||||||
useUserStoreHook()
|
useUserStoreHook()
|
||||||
.loginByUsername({ username: ruleForm.username, password: "admin123" })
|
.loginByUsername({ username: ruleForm.username, password: "admin123" })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
// 获取后端路由
|
// 获取后端路由
|
||||||
return initRouter().then(() => {
|
return initRouter().then(() => {
|
||||||
router.push(getTopMenu(true).path);
|
disabled.value = true;
|
||||||
|
router
|
||||||
|
.push(getTopMenu(true).path)
|
||||||
|
.then(() => {
|
||||||
message("登录成功", { type: "success" });
|
message("登录成功", { type: "success" });
|
||||||
|
})
|
||||||
|
.finally(() => (disabled.value = false));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.finally(() => (loading.value = false));
|
.finally(() => (loading.value = false));
|
||||||
} else {
|
} else {
|
||||||
loading.value = false;
|
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 使用公共函数,避免`removeEventListener`失效 */
|
const immediateDebounce: any = debounce(
|
||||||
function onkeypress({ code }: KeyboardEvent) {
|
formRef => onLogin(formRef),
|
||||||
if (code === "Enter") {
|
1000,
|
||||||
onLogin(ruleFormRef.value);
|
true
|
||||||
}
|
);
|
||||||
}
|
|
||||||
|
|
||||||
onMounted(() => {
|
useEventListener(document, "keypress", ({ code }) => {
|
||||||
window.document.addEventListener("keypress", onkeypress);
|
if (code === "Enter" && !disabled.value) immediateDebounce(ruleFormRef.value);
|
||||||
});
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
window.document.removeEventListener("keypress", onkeypress);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(imgCode, value => {
|
watch(imgCode, value => {
|
||||||
@ -270,6 +264,7 @@ watch(loginDay, value => {
|
|||||||
size="default"
|
size="default"
|
||||||
type="primary"
|
type="primary"
|
||||||
:loading="loading"
|
:loading="loading"
|
||||||
|
:disabled="disabled"
|
||||||
@click="onLogin(ruleFormRef)"
|
@click="onLogin(ruleFormRef)"
|
||||||
>
|
>
|
||||||
{{ t("login.login") }}
|
{{ t("login.login") }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user