From be75b2b118f1cc14d2d791316dce77bc9cbdb535 Mon Sep 17 00:00:00 2001 From: pany <939630029@qq.com> Date: Fri, 16 Jun 2023 18:13:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?views/login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/login/index.vue | 40 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 2b40140..f078a47 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -2,13 +2,15 @@ import { reactive, ref } from "vue" import { useRouter } from "vue-router" import { useUserStore } from "@/store/modules/user" -import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue" -import ThemeSwitch from "@/components/ThemeSwitch/index.vue" import { type FormInstance, FormRules } from "element-plus" +import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue" import { getLoginCodeApi } from "@/api/login" import { type LoginRequestData } from "@/api/login/types/login" +import ThemeSwitch from "@/components/ThemeSwitch/index.vue" const router = useRouter() + +/** 登录表单元素的引用 */ const loginFormRef = ref(null) /** 登录按钮 Loading */ @@ -16,7 +18,7 @@ const loading = ref(false) /** 验证码图片 URL */ const codeUrl = ref("") /** 登录表单数据 */ -const loginForm: LoginRequestData = reactive({ +const loginFormData: LoginRequestData = reactive({ username: "admin", password: "12345678", code: "" @@ -32,34 +34,30 @@ const loginFormRules: FormRules = { } /** 登录逻辑 */ const handleLogin = () => { - loginFormRef.value?.validate((valid: boolean) => { + loginFormRef.value?.validate((valid: boolean, fields) => { if (valid) { loading.value = true useUserStore() - .login({ - username: loginForm.username, - password: loginForm.password, - code: loginForm.code - }) + .login(loginFormData) .then(() => { router.push({ path: "/" }) }) .catch(() => { createCode() - loginForm.password = "" + loginFormData.password = "" }) .finally(() => { loading.value = false }) } else { - return false + console.error("表单校验不通过", fields) } }) } /** 创建验证码 */ const createCode = () => { // 先清空验证码的输入 - loginForm.code = "" + loginFormData.code = "" // 获取验证码 codeUrl.value = "" getLoginCodeApi().then((res) => { @@ -79,10 +77,10 @@ createCode()
- + - 登 录 + 登 录