Files
tydata-webview-v2/src/views/Login.vue

355 lines
9.3 KiB
Vue
Raw Normal View History

2025-09-27 17:41:14 +08:00
<script setup>
import { ref, computed, onUnmounted, nextTick } from 'vue'
import { showToast } from 'vant'
import ClickCaptcha from '@/components/ClickCaptcha.vue'
const router = useRouter()
const phoneNumber = ref('')
const verificationCode = ref('')
const password = ref('')
const isPasswordLogin = ref(false)
const isAgreed = ref(false)
const isCountingDown = ref(false)
const countdown = ref(60)
let timer = null
// 验证组件状态
const showCaptcha = ref(false)
const captchaVerified = ref(false)
// 聚焦状态变量
const phoneFocused = ref(false)
const codeFocused = ref(false)
const passwordFocused = ref(false)
const isPhoneNumberValid = computed(() => {
return /^1[3-9]\d{9}$/.test(phoneNumber.value)
})
const canLogin = computed(() => {
if (!isPhoneNumberValid.value) return false
if (isPasswordLogin.value) {
return password.value.length >= 6
} else {
return verificationCode.value.length === 6
}
})
async function sendVerificationCode() {
if (isCountingDown.value || !isPhoneNumberValid.value) return
if (!isPhoneNumberValid.value) {
showToast({ message: "请输入有效的手机号" });
return
}
const { data, error } = await useApiFetch('auth/sendSms')
.post({ mobile: phoneNumber.value, actionType: 'login' })
.json()
if (data.value && !error.value) {
if (data.value.code === 200) {
showToast({ message: "获取成功" });
startCountdown()
// 聚焦到验证码输入框
nextTick(() => {
const verificationCodeInput = document.getElementById('verificationCode');
if (verificationCodeInput) {
verificationCodeInput.focus();
}
});
} else {
showToast(data.value.msg)
}
}
}
function startCountdown() {
isCountingDown.value = true
countdown.value = 60
timer = setInterval(() => {
if (countdown.value > 0) {
countdown.value--
} else {
clearInterval(timer)
isCountingDown.value = false
}
}, 1000)
}
async function handleLogin() {
if (!isPhoneNumberValid.value) {
showToast({ message: "请输入有效的手机号" });
return
}
if (isPasswordLogin.value) {
if (password.value.length < 6) {
showToast({ message: "密码长度不能小于6位" });
return
}
} else {
if (verificationCode.value.length !== 6) {
showToast({ message: "请输入有效的验证码" });
return
}
}
if (!isAgreed.value) {
showToast({ message: "请先同意用户协议" });
return
}
// 显示验证组件
showCaptcha.value = true
}
// 验证成功回调
function handleCaptchaSuccess() {
captchaVerified.value = true
showCaptcha.value = false
// 执行实际的登录逻辑
performLogin()
}
// 验证关闭回调
function handleCaptchaClose() {
showCaptcha.value = false
}
// 执行实际的登录逻辑
async function performLogin() {
const { data, error } = await useApiFetch('/user/mobileCodeLogin')
.post({ mobile: phoneNumber.value, code: verificationCode.value })
.json()
if (data.value && !error.value) {
if (data.value.code === 200) {
localStorage.setItem('token', data.value.data.accessToken)
localStorage.setItem('refreshAfter', data.value.data.refreshAfter)
localStorage.setItem('accessExpire', data.value.data.accessExpire)
window.location.href = '/'
}
}
}
function toUserAgreement() {
router.push(`/userAgreement`)
}
function toPrivacyPolicy() {
router.push(`/privacyPolicy`)
}
onUnmounted(() => {
if (timer) {
clearInterval(timer)
}
})
const onClickLeft = () => {
router.replace('/')
}
</script>
<template>
<div class="login-layout ">
<van-nav-bar fixed placeholder title="用户登录" left-text="" left-arrow @click-left="onClickLeft" />
2025-10-28 12:12:48 +08:00
<div class="login px-4 relative z-10">
<div class="mb-8 pt-20 text-left">
2025-09-27 17:41:14 +08:00
<div class="flex flex-col items-center">
2026-01-03 17:53:26 +08:00
<img class="h-16 w-16 rounded-full shadow" src="/logo.png" alt="Logo" />
2026-02-25 17:49:30 +08:00
<div class="text-3xl mt-4 text-slate-700 font-bold">天远数据</div>
2025-09-27 17:41:14 +08:00
</div>
</div>
2025-10-28 12:12:48 +08:00
<!-- 登录表单 -->
<div class="login-form">
2025-09-27 17:41:14 +08:00
<!-- 手机号输入 -->
2025-10-28 12:12:48 +08:00
<div class="form-item">
<div class="form-label">手机号</div>
<input v-model="phoneNumber" class="form-input" type="tel" placeholder="请输入手机号" maxlength="11" />
2025-09-27 17:41:14 +08:00
</div>
<!-- 验证码输入 -->
2025-10-28 12:12:48 +08:00
<div class="form-item">
<div class="form-label">验证码</div>
<div class="verification-input-wrapper">
<input v-model="verificationCode" id="verificationCode" class="form-input verification-input"
placeholder="请输入验证码" maxlength="6" />
<button class="get-code-btn" :class="{ 'disabled': isCountingDown || !isPhoneNumberValid }"
@click="sendVerificationCode" :disabled="isCountingDown || !isPhoneNumberValid">
{{ isCountingDown ? `${countdown}s` : '获取验证码' }}
2025-09-27 17:41:14 +08:00
</button>
</div>
</div>
2025-10-28 12:12:48 +08:00
<!-- 协议同意框 -->
<div class="agreement-wrapper">
<input type="checkbox" v-model="isAgreed" class="agreement-checkbox accent-primary"
id="agreement" />
<label for="agreement" class="agreement-text">
我已阅读并同意
<a class="agreement-link" @click="toUserAgreement">用户协议</a>
<a class="agreement-link" @click="toPrivacyPolicy">隐私政策</a>
</label>
2025-09-27 17:41:14 +08:00
</div>
2025-10-28 12:12:48 +08:00
<!-- 提示文字 -->
<div class="notice-text">
未注册手机号登录后将自动生成账号并且代表您已阅读并同意
2025-09-27 17:41:14 +08:00
</div>
2025-10-28 12:12:48 +08:00
<!-- 登录按钮 -->
<button class="login-btn" :class="{ 'disabled': !canLogin }" @click="handleLogin" :disabled="!canLogin">
</button>
</div>
2025-09-27 17:41:14 +08:00
</div>
<!-- 点击验证组件 -->
<ClickCaptcha :visible="showCaptcha" @success="handleCaptchaSuccess" @close="handleCaptchaClose" />
</div>
</template>
<style scoped>
.login-layout {
2025-10-28 12:12:48 +08:00
background-image: url('@/assets/images/login_bg.png');
2025-09-27 17:41:14 +08:00
background-position: center;
2025-10-28 12:12:48 +08:00
background-repeat: no-repeat;
2025-09-27 17:41:14 +08:00
background-size: cover;
height: 100vh;
position: relative;
overflow: hidden;
}
2025-10-28 12:12:48 +08:00
.login {}
/* 登录表单 */
.login-form {
background-color: var(--color-bg-primary);
padding: 2rem;
margin-top: 0.5rem;
box-shadow: 0px 0px 24px 0px #3F3F3F0F;
border-radius: 8px;
}
/* 表单项 */
.form-item {
margin-bottom: 1.5rem;
display: flex;
align-items: center;
border: none;
border-bottom: 1px solid var(--color-border-primary);
}
.form-label {
font-size: 0.9375rem;
color: var(--color-text-primary);
margin-bottom: 0;
margin-right: 1rem;
font-weight: 500;
min-width: 4rem;
flex-shrink: 0;
}
.form-input {
width: 100%;
padding: 0.875rem 0;
font-size: 0.9375rem;
color: var(--color-text-primary);
outline: none;
background-color: transparent;
}
.form-input::placeholder {
color: var(--color-text-tertiary);
2025-09-27 17:41:14 +08:00
}
2025-10-28 12:12:48 +08:00
.form-input:focus {
border-bottom-color: var(--color-text-primary);
}
/* 验证码输入 */
.verification-input-wrapper {
position: relative;
display: flex;
align-items: center;
flex: 1;
}
.verification-input {
flex: 1;
padding-right: 6rem;
}
.get-code-btn {
2025-09-27 17:41:14 +08:00
position: absolute;
right: 0;
2025-10-28 12:12:48 +08:00
background: none;
border: none;
color: var(--color-primary);
font-size: 0.875rem;
cursor: pointer;
padding: 0.5rem;
font-weight: 500;
}
.get-code-btn.disabled {
color: var(--color-gray-400);
cursor: not-allowed;
2025-09-27 17:41:14 +08:00
}
2025-10-28 12:12:48 +08:00
/* 协议同意 */
.agreement-wrapper {
display: flex;
align-items: center;
margin-top: 1.5rem;
margin-bottom: 1rem;
2025-09-27 17:41:14 +08:00
}
2025-10-28 12:12:48 +08:00
.agreement-checkbox {
flex-shrink: 0;
margin-right: 0.5rem;
2025-09-27 17:41:14 +08:00
}
2025-10-28 12:12:48 +08:00
.agreement-text {
font-size: 0.75rem;
color: var(--color-text-secondary);
line-height: 1.4;
2025-09-27 17:41:14 +08:00
}
2025-10-28 12:12:48 +08:00
.agreement-link {
color: var(--color-primary);
cursor: pointer;
text-decoration: none;
}
/* 提示文字 */
.notice-text {
font-size: 0.6875rem;
color: var(--color-text-tertiary);
line-height: 1.5;
margin-bottom: 2rem;
}
/* 登录按钮 */
.login-btn {
2025-09-27 17:41:14 +08:00
width: 100%;
2025-10-28 12:12:48 +08:00
padding: 0.875rem;
background-color: var(--color-primary);
color: var(--color-text-white);
2025-09-27 17:41:14 +08:00
border: none;
2025-10-28 12:12:48 +08:00
border-radius: 1.5rem;
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: opacity 0.3s;
letter-spacing: 0.25rem;
}
.login-btn:hover {
opacity: 0.9;
}
.login-btn.disabled {
background-color: var(--color-gray-300);
cursor: not-allowed;
2025-09-27 17:41:14 +08:00
}
</style>