|
@@ -1,38 +1,104 @@
|
|
|
<template>
|
|
|
<div class="login">
|
|
|
- <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
|
|
|
+ <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" v-if="!isPhoneLogin">
|
|
|
<h3 class="title">{{ title }}</h3>
|
|
|
<el-form-item prop="username">
|
|
|
<el-input
|
|
|
- v-model="loginForm.username"
|
|
|
- type="text"
|
|
|
- size="large"
|
|
|
- auto-complete="off"
|
|
|
- placeholder="账号"
|
|
|
+ v-model="loginForm.username"
|
|
|
+ type="text"
|
|
|
+ size="large"
|
|
|
+ auto-complete="off"
|
|
|
+ placeholder="账号"
|
|
|
>
|
|
|
- <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
|
|
|
+ <template #prefix>
|
|
|
+ <svg-icon icon-class="user" class="el-input__icon input-icon"/>
|
|
|
+ </template>
|
|
|
</el-input>
|
|
|
</el-form-item>
|
|
|
<el-form-item prop="password">
|
|
|
<el-input
|
|
|
- v-model="loginForm.password"
|
|
|
- type="password"
|
|
|
- size="large"
|
|
|
- auto-complete="off"
|
|
|
- placeholder="密码"
|
|
|
- @keyup.enter="handleLogin"
|
|
|
+ v-model="loginForm.password"
|
|
|
+ type="password"
|
|
|
+ size="large"
|
|
|
+ auto-complete="off"
|
|
|
+ placeholder="密码"
|
|
|
+ @keyup.enter="handleLogin"
|
|
|
>
|
|
|
- <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
|
|
|
+ <template #prefix>
|
|
|
+ <svg-icon icon-class="password" class="el-input__icon input-icon"/>
|
|
|
+ </template>
|
|
|
</el-input>
|
|
|
</el-form-item>
|
|
|
- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
|
|
|
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
|
|
|
+ <el-checkbox v-model="loginForm.rememberMe" style="margin: 0;">记住密码</el-checkbox>
|
|
|
+ <el-button text="text" plain @click="changeLoginModel">验证码登录</el-button>
|
|
|
+ </div>
|
|
|
<el-form-item style="width:100%;">
|
|
|
<el-button
|
|
|
- :loading="loading"
|
|
|
- size="large"
|
|
|
- type="primary"
|
|
|
- style="width:100%;"
|
|
|
- @click.prevent="handleLogin"
|
|
|
+ :loading="loading"
|
|
|
+ size="large"
|
|
|
+ type="primary"
|
|
|
+ style="width:100%;"
|
|
|
+ @click.prevent="handleLogin"
|
|
|
+ >
|
|
|
+ <span v-if="!loading">登 录</span>
|
|
|
+ <span v-else>登 录 中...</span>
|
|
|
+ </el-button>
|
|
|
+ <div style="float: right;" v-if="register">
|
|
|
+ <router-link class="link-type" :to="'/register'">立即注册</router-link>
|
|
|
+ </div>
|
|
|
+ </el-form-item>
|
|
|
+ </el-form>
|
|
|
+ <el-form ref="smsLoginRef" :model="loginForm" :rules="loginRules" class="login-form" v-if="isPhoneLogin">
|
|
|
+ <h3 class="title">{{ title }}</h3>
|
|
|
+ <el-form-item prop="phonenumber">
|
|
|
+ <el-input
|
|
|
+ v-model="loginForm.phonenumber"
|
|
|
+ type="text"
|
|
|
+ clearable
|
|
|
+ size="large"
|
|
|
+ auto-complete="off"
|
|
|
+ placeholder="手机号"
|
|
|
+ >
|
|
|
+ <template #prefix>
|
|
|
+ <svg-icon icon-class="phone" class="el-input__icon input-icon"/>
|
|
|
+ </template>
|
|
|
+ <template #append>
|
|
|
+ <el-button
|
|
|
+ :disabled="isCountingDown"
|
|
|
+ @click="handleSendSms"
|
|
|
+ >
|
|
|
+ {{ buttonText }}
|
|
|
+ </el-button>
|
|
|
+ </template>
|
|
|
+ </el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item prop="code">
|
|
|
+ <el-input
|
|
|
+ v-model="loginForm.code"
|
|
|
+ type="text"
|
|
|
+ size="large"
|
|
|
+ clearable
|
|
|
+ auto-complete="off"
|
|
|
+ placeholder="验证码"
|
|
|
+ @keyup.enter="handleLoginWithSms"
|
|
|
+ >
|
|
|
+ <template #prefix>
|
|
|
+ <svg-icon icon-class="message" class="el-input__icon input-icon"/>
|
|
|
+ </template>
|
|
|
+ </el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
|
|
|
+ <div style="text-align: right;margin-bottom: 20px">
|
|
|
+ <el-button text="text" plain @click="changeLoginModel">用户名登录</el-button>
|
|
|
+ </div>
|
|
|
+ <el-form-item style="width:100%;">
|
|
|
+ <el-button
|
|
|
+ :loading="loading"
|
|
|
+ size="large"
|
|
|
+ type="primary"
|
|
|
+ style="width:100%;"
|
|
|
+ @click.prevent="handleLoginWithSms"
|
|
|
>
|
|
|
<span v-if="!loading">登 录</span>
|
|
|
<span v-else>登 录 中...</span>
|
|
@@ -51,37 +117,98 @@
|
|
|
|
|
|
<script setup>
|
|
|
import Cookies from "js-cookie";
|
|
|
-import { encrypt, decrypt } from "@/utils/jsencrypt";
|
|
|
+import {decrypt, encrypt} from "@/utils/jsencrypt";
|
|
|
import useUserStore from '@/store/modules/user'
|
|
|
+import {loginWithSms, sendSms} from "@/api/login.js";
|
|
|
+import {computed, onUnmounted, ref} from 'vue'
|
|
|
+
|
|
|
+const isCountingDown = ref(false)
|
|
|
+const countDown = ref(0)
|
|
|
+let timer = null
|
|
|
|
|
|
const title = import.meta.env.VITE_APP_TITLE;
|
|
|
const userStore = useUserStore();
|
|
|
const route = useRoute();
|
|
|
const router = useRouter();
|
|
|
-const { proxy } = getCurrentInstance();
|
|
|
+const {proxy} = getCurrentInstance();
|
|
|
|
|
|
const loginForm = ref({
|
|
|
username: "",
|
|
|
password: "",
|
|
|
rememberMe: false,
|
|
|
code: "",
|
|
|
+ phonenumber: "",
|
|
|
uuid: ""
|
|
|
});
|
|
|
|
|
|
+const isPhoneLogin = ref(true);
|
|
|
+const canLogin = ref(false);
|
|
|
+
|
|
|
const loginRules = {
|
|
|
- username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
|
|
|
- password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
|
|
|
- code: [{ required: true, trigger: "change", message: "请输入验证码" }]
|
|
|
+ username: [{required: true, trigger: "blur", message: "请输入您的账号"}],
|
|
|
+ password: [{required: true, trigger: "blur", message: "请输入您的密码"}],
|
|
|
+ phonenumber: [{required: true, trigger: "blur", message: "请输入手机号"}, {
|
|
|
+ pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
|
|
|
+ message: '手机号格式不正确',
|
|
|
+ trigger: 'blur'
|
|
|
+ }],
|
|
|
+ code: [{required: true, trigger: "change", message: "请输入验证码"}]
|
|
|
};
|
|
|
|
|
|
const loading = ref(false);
|
|
|
// 注册开关
|
|
|
const register = ref(false);
|
|
|
const redirect = ref(undefined);
|
|
|
+// 计算属性优化按钮文本
|
|
|
+const buttonText = computed(() =>
|
|
|
+ isCountingDown.value ? `${countDown.value}秒后重试` : '发送验证码'
|
|
|
+)// 倒计时逻辑封装
|
|
|
+const startCountDown = () => {
|
|
|
+ isCountingDown.value = true
|
|
|
+ countDown.value = 60
|
|
|
+
|
|
|
+ timer = setInterval(() => {
|
|
|
+ if (--countDown.value <= 0) {
|
|
|
+ resetCountDown()
|
|
|
+ }
|
|
|
+ }, 1000)
|
|
|
+}
|
|
|
+
|
|
|
+// 重置倒计时
|
|
|
+const resetCountDown = () => {
|
|
|
+ clearInterval(timer)
|
|
|
+ isCountingDown.value = false
|
|
|
+}
|
|
|
|
|
|
watch(route, (newRoute) => {
|
|
|
- redirect.value = newRoute.query && newRoute.query.redirect;
|
|
|
-}, { immediate: true });
|
|
|
+ redirect.value = newRoute.query && newRoute.query.redirect;
|
|
|
+}, {immediate: true});
|
|
|
+
|
|
|
+function changeLoginModel() {
|
|
|
+ isPhoneLogin.value = !isPhoneLogin.value;
|
|
|
+}
|
|
|
+
|
|
|
+function handleLoginWithSms() {
|
|
|
+ proxy.$refs.smsLoginRef.validate(valid => {
|
|
|
+ if (valid) {
|
|
|
+ loading.value = true;
|
|
|
+ // 调用action的登录方法
|
|
|
+ // 调用action的登录方法
|
|
|
+ userStore.loginWithSms(loginForm.value).then(() => {
|
|
|
+ const query = route.query;
|
|
|
+ const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
|
|
|
+ if (cur !== "redirect") {
|
|
|
+ acc[cur] = query[cur];
|
|
|
+ }
|
|
|
+ return acc;
|
|
|
+ }, {});
|
|
|
+ router.push({path: redirect.value || "/", query: otherQueryParams});
|
|
|
+ }).catch(() => {
|
|
|
+ loading.value = false;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
function handleLogin() {
|
|
|
proxy.$refs.loginRef.validate(valid => {
|
|
@@ -89,9 +216,9 @@ function handleLogin() {
|
|
|
loading.value = true;
|
|
|
// 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
|
|
|
if (loginForm.value.rememberMe) {
|
|
|
- Cookies.set("username", loginForm.value.username, { expires: 30 });
|
|
|
- Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
|
|
|
- Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
|
|
|
+ Cookies.set("username", loginForm.value.username, {expires: 30});
|
|
|
+ Cookies.set("password", encrypt(loginForm.value.password), {expires: 30});
|
|
|
+ Cookies.set("rememberMe", loginForm.value.rememberMe, {expires: 30});
|
|
|
} else {
|
|
|
// 否则移除
|
|
|
Cookies.remove("username");
|
|
@@ -107,7 +234,7 @@ function handleLogin() {
|
|
|
}
|
|
|
return acc;
|
|
|
}, {});
|
|
|
- router.push({ path: redirect.value || "/", query: otherQueryParams });
|
|
|
+ router.push({path: redirect.value || "/", query: otherQueryParams});
|
|
|
}).catch(() => {
|
|
|
loading.value = false;
|
|
|
});
|
|
@@ -115,6 +242,21 @@ function handleLogin() {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+function handleSendSms() {
|
|
|
+ if (isPhoneLogin.value) {
|
|
|
+ // 调用action的登录方法
|
|
|
+ sendSms(loginForm.value.phonenumber).then(() => {
|
|
|
+ proxy.$modal.msgSuccess("验证码发送成功");
|
|
|
+ startCountDown()
|
|
|
+ canLogin.value = true;
|
|
|
+ }).catch(() => {
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
function getCookie() {
|
|
|
const username = Cookies.get("username");
|
|
|
const password = Cookies.get("password");
|
|
@@ -127,6 +269,8 @@ function getCookie() {
|
|
|
}
|
|
|
|
|
|
getCookie();
|
|
|
+// 组件卸载时清理
|
|
|
+onUnmounted(resetCountDown)
|
|
|
</script>
|
|
|
|
|
|
<style lang='scss' scoped>
|
|
@@ -138,6 +282,7 @@ getCookie();
|
|
|
background-image: url("../assets/images/login-background.jpg");
|
|
|
background-size: cover;
|
|
|
}
|
|
|
+
|
|
|
.title {
|
|
|
margin: 0px auto 30px auto;
|
|
|
text-align: center;
|
|
@@ -149,32 +294,39 @@ getCookie();
|
|
|
background: #ffffff;
|
|
|
width: 400px;
|
|
|
padding: 25px 25px 5px 25px;
|
|
|
+
|
|
|
.el-input {
|
|
|
height: 40px;
|
|
|
+
|
|
|
input {
|
|
|
height: 40px;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
.input-icon {
|
|
|
height: 39px;
|
|
|
width: 14px;
|
|
|
margin-left: 0px;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
.login-tip {
|
|
|
font-size: 13px;
|
|
|
text-align: center;
|
|
|
color: #bfbfbf;
|
|
|
}
|
|
|
+
|
|
|
.login-code {
|
|
|
width: 33%;
|
|
|
height: 40px;
|
|
|
float: right;
|
|
|
+
|
|
|
img {
|
|
|
cursor: pointer;
|
|
|
vertical-align: middle;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
.el-login-footer {
|
|
|
height: 40px;
|
|
|
line-height: 40px;
|
|
@@ -187,6 +339,7 @@ getCookie();
|
|
|
font-size: 12px;
|
|
|
letter-spacing: 1px;
|
|
|
}
|
|
|
+
|
|
|
.login-code-img {
|
|
|
height: 40px;
|
|
|
padding-left: 12px;
|