Files
JayuCloud/Frontend/src/views/AuthView.vue
T

1248 lines
34 KiB
Vue
Raw Normal View History

2026-02-24 09:59:22 +08:00
<template>
<div class="mobile-container">
<div class="auth-container">
<div class="logo-section">
<div class="logo">Yumi</div>
<div class="subtitle">美甲服务平台</div>
</div>
<div class="tab-container">
<div
class="tab-item"
2026-02-24 09:59:22 +08:00
:class="{ active: activeTab === 'login' }"
@click="activeTab = 'login'"
>
登录
</div>
<div
class="tab-item"
2026-02-24 09:59:22 +08:00
:class="{ active: activeTab === 'register' }"
@click="activeTab = 'register'"
>
注册
</div>
</div>
<div v-if="activeTab === 'login'" class="login-type-toggle">
<a-radio-group v-model:value="loginType" button-style="solid">
<a-radio-button value="password">账号密码登录</a-radio-button>
<a-radio-button value="phone">手机号验证码登录</a-radio-button>
</a-radio-group>
</div>
2026-02-24 09:59:22 +08:00
<a-form
:model="formData"
:rules="currentRules"
2026-02-24 09:59:22 +08:00
@finish="handleSubmit"
layout="vertical"
class="auth-form"
>
<a-form-item v-if="activeTab === 'register'" name="nickname" label="昵称">
2026-02-24 09:59:22 +08:00
<a-input
v-model:value="formData.nickname"
placeholder="请输入昵称"
2026-02-24 09:59:22 +08:00
size="large"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
</a-form-item>
<a-form-item v-if="activeTab === 'register'" name="account" label="账号">
<a-input
v-model:value="formData.account"
placeholder="请输入账号"
size="large"
@input="handleAccountInput"
2026-03-19 20:51:07 +08:00
@blur="handleRegisterAccountBlur"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
2026-03-19 20:51:07 +08:00
<div
v-if="registerAccountStatus"
class="field-status"
:class="registerAccountStatus.available ? 'success' : 'error'"
>
{{ registerAccountStatus.message }}
</div>
</a-form-item>
<a-form-item v-if="activeTab === 'register'" name="phone" label="手机号">
<a-input
v-model:value="formData.phone"
placeholder="请输入手机号"
size="large"
2026-03-19 20:51:07 +08:00
@blur="handleRegisterPhoneBlur"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
2026-03-19 20:51:07 +08:00
<div
v-if="registerPhoneStatus"
class="field-status"
:class="registerPhoneStatus.available ? 'success' : 'error'"
>
{{ registerPhoneStatus.message }}
</div>
</a-form-item>
<a-form-item v-if="activeTab === 'register'" name="verificationCode" label="验证码">
<a-input
v-model:value="formData.verificationCode"
placeholder="请输入验证码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0 || !formData.phone"
@click="handleSendVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
<a-form-item
v-if="activeTab === 'login' && loginType === 'password'"
name="account"
label="账号"
>
<a-input
v-model:value="formData.account"
placeholder="请输入账号"
size="large"
@input="handleAccountInput"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
<div
v-if="loginAccountStatus"
class="field-status"
:class="loginAccountStatus.exists ? 'success' : 'error'"
>
{{ loginAccountStatus.message }}
</div>
</a-form-item>
<a-form-item
v-if="activeTab === 'login' && loginType === 'phone'"
name="phone"
label="手机号"
>
<a-input
v-model:value="formData.phone"
placeholder="请输入手机号"
size="large"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
<div class="phone-login-tip">
<InfoCircleOutlined style="color: #1890ff; margin-right: 4px;" />
<span>新用户将自动注册</span>
</div>
</a-form-item>
<a-form-item
v-if="activeTab === 'login' && loginType === 'phone'"
name="verificationCode"
label="验证码"
>
<a-input
v-model:value="formData.verificationCode"
placeholder="请输入验证码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0 || !formData.phone"
@click="handleSendVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
2026-03-19 20:51:07 +08:00
<a-form-item
v-if="activeTab === 'login'"
name="shopId"
label="选择登录店铺"
>
<a-select
v-model:value="formData.shopId"
:placeholder="loginShopPlaceholder"
size="large"
:loading="loadingShops"
:disabled="!allowLoginShopless && !loginShops.length"
2026-03-19 20:51:07 +08:00
allow-clear
>
2026-03-19 20:51:07 +08:00
<a-select-option
v-if="loginShops.length === 0"
disabled
value=""
>
{{ allowLoginShopless ? '当前无需选择店铺' : '暂无可用店铺' }}
</a-select-option>
2026-03-19 20:51:07 +08:00
<a-select-option
v-for="shop in loginShops"
2026-03-19 20:51:07 +08:00
:key="shop.id"
:value="normalizeShopOptionValue(shop.id)"
>
{{ shop.shopName }}
</a-select-option>
</a-select>
<div
v-if="loginShopMessage"
class="field-status"
:class="loginShops.length || allowLoginShopless ? 'success' : 'error'"
>
{{ loginShopMessage }}
</div>
</a-form-item>
<a-form-item
v-if="(activeTab === 'login' && loginType === 'password') || activeTab === 'register'"
name="password"
label="密码"
>
2026-02-24 09:59:22 +08:00
<a-input-password
v-model:value="formData.password"
placeholder="请输入密码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
</a-input-password>
</a-form-item>
<a-form-item v-if="activeTab === 'register'" name="confirmPassword" label="确认密码">
<a-input-password
v-model:value="formData.confirmPassword"
placeholder="请再次输入密码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
</a-input-password>
</a-form-item>
<a-form-item v-if="activeTab === 'register'" name="shopId" label="选择店铺">
<a-select
v-model:value="formData.shopId"
placeholder="请选择店铺"
size="large"
:loading="loadingShops"
>
<a-select-option
2026-02-24 09:59:22 +08:00
v-if="shops.length === 0"
disabled
value=""
>
暂无店铺数据
</a-select-option>
<a-select-option
v-for="shop in shops"
:key="shop.id"
:value="normalizeShopOptionValue(shop.id)"
2026-02-24 09:59:22 +08:00
>
{{ shop.shopName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item v-if="activeTab === 'login'" style="margin-bottom: 8px">
<div class="form-actions">
<a-checkbox v-model:checked="formData.rememberMe">记住我</a-checkbox>
<a class="forgot-password" @click="handleForgotPassword">忘记密码?</a>
</div>
</a-form-item>
<a-form-item>
<a-button
type="primary"
html-type="submit"
size="large"
2026-02-24 09:59:22 +08:00
block
:loading="loading"
>
{{ activeTab === 'login' ? '登录' : '注册' }}
</a-button>
</a-form-item>
<div class="social-login">
<div class="divider">
<span>其他登录方式</span>
</div>
<div class="social-buttons">
<a-button shape="circle" size="large" @click="handleWeChatLogin">
<WechatOutlined />
</a-button>
<a-button shape="circle" size="large" @click="handleQQLogin">
<QqOutlined />
</a-button>
</div>
</div>
<div class="admin-login">
<span class="admin-login-link" @click="handleAdminLogin">管理后台登录</span>
</div>
2026-02-24 09:59:22 +08:00
</a-form>
</div>
2026-03-19 14:10:59 +08:00
<a-modal
v-model:open="forgotPasswordVisible"
2026-03-19 14:10:59 +08:00
title="忘记密码"
:footer="null"
width="90%"
:destroyOnClose="true"
>
<a-form
:model="forgotPasswordForm"
:rules="forgotPasswordRules"
@finish="handleResetPassword"
layout="vertical"
>
<a-form-item name="phone" label="手机号">
<a-input
v-model:value="forgotPasswordForm.phone"
placeholder="请输入绑定的手机号"
size="large"
>
<template #prefix>
<UserOutlined style="color: #999" />
</template>
</a-input>
</a-form-item>
<a-form-item name="verificationCode" label="验证码">
<a-input
v-model:value="forgotPasswordForm.verificationCode"
placeholder="请输入验证码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
<template #suffix>
<a-button
type="link"
2026-03-19 14:10:59 +08:00
:disabled="forgotCountdown > 0 || !forgotPasswordForm.phone"
@click="handleSendForgotCode"
>
{{ forgotCountdown > 0 ? `${forgotCountdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
<a-form-item name="newPassword" label="新密码">
<a-input-password
v-model:value="forgotPasswordForm.newPassword"
placeholder="请输入新密码"
size="large"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
</a-input-password>
</a-form-item>
<a-form-item>
<a-button
type="primary"
html-type="submit"
size="large"
2026-03-19 14:10:59 +08:00
block
:loading="resettingPassword"
>
重置密码
</a-button>
</a-form-item>
<a-form-item>
<a-button size="large" block @click="forgotPasswordVisible = false">
取消
</a-button>
</a-form-item>
</a-form>
</a-modal>
2026-02-24 09:59:22 +08:00
</div>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
2026-02-24 09:59:22 +08:00
import { useRouter } from 'vue-router'
import { InfoCircleOutlined, LockOutlined, QqOutlined, UserOutlined, WechatOutlined } from '@ant-design/icons-vue'
2026-02-24 09:59:22 +08:00
import { message } from 'ant-design-vue'
2026-03-10 23:23:15 +08:00
import type { Rule } from 'ant-design-vue/es/form'
2026-02-24 09:59:22 +08:00
import { authApi } from '@/api/auth'
import { shopApi } from '@/api/shop'
import { ACCOUNT_REGEX, ACCOUNT_RULE_MESSAGE, sanitizeAccountInput } from '@/utils/account'
import {
userApi,
type AvailabilityCheckResponse,
type LoginAccountCheckResponse,
type LoginShopSelectionResponse
} from '@/api/user'
2026-02-24 09:59:22 +08:00
const router = useRouter()
const activeTab = ref<'login' | 'register'>('login')
const loginType = ref<'password' | 'phone'>('password')
2026-02-24 09:59:22 +08:00
const loading = ref(false)
const loadingShops = ref(false)
const shops = ref<any[]>([])
const loginShops = ref<Array<{ id: number | string; shopName: string }>>([])
const countdown = ref(0)
const loginAccountStatus = ref<LoginAccountCheckResponse | null>(null)
2026-03-19 20:51:07 +08:00
const registerAccountStatus = ref<AvailabilityCheckResponse | null>(null)
const registerPhoneStatus = ref<AvailabilityCheckResponse | null>(null)
const loginShopMessage = ref('')
const allowLoginShopless = ref(false)
2026-02-24 09:59:22 +08:00
2026-03-19 14:10:59 +08:00
const forgotPasswordVisible = ref(false)
const forgotCountdown = ref(0)
const resettingPassword = ref(false)
const forgotPasswordForm = reactive({
phone: '',
verificationCode: '',
newPassword: ''
})
2026-02-24 09:59:22 +08:00
const formData = reactive({
account: '',
password: '',
confirmPassword: '',
nickname: '',
shopId: undefined as string | undefined,
phone: '',
verificationCode: '',
2026-02-24 09:59:22 +08:00
rememberMe: false
})
let loginAccountCheckTimer: number | undefined
let loginPhoneShopCheckTimer: number | undefined
const phoneRegex = /^1[3-9]\d{9}$/
const verificationCodeRegex = /^\d{6}$/
const loginShopPlaceholder = computed(() => {
if (allowLoginShopless.value) {
return '当前暂无启用店铺,可直接登录'
}
if (loadingShops.value) {
return '正在加载可登录店铺'
}
return loginShops.value.length ? '请选择登录店铺' : '请先输入账号或手机号'
})
const validateLoginShopRequired = async () => {
if (allowLoginShopless.value || !loginShops.value.length || formData.shopId) {
return Promise.resolve()
}
return Promise.reject('请选择登录店铺')
}
const validatePassword = async (_rule: Rule, value: string) => {
if (!value || value.length < 6) {
return Promise.reject('密码长度不能少于 6 位')
2026-02-24 09:59:22 +08:00
}
2026-03-19 14:10:59 +08:00
let count = 0
if (/\d/.test(value)) count++
if (/[a-z]/.test(value)) count++
if (/[A-Z]/.test(value)) count++
if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>/?]/.test(value)) count++
2026-03-19 14:10:59 +08:00
if (count < 3) {
return Promise.reject('密码需至少包含数字、小写字母、大写字母、符号中的 3 种')
2026-03-19 14:10:59 +08:00
}
2026-02-24 09:59:22 +08:00
return Promise.resolve()
}
const validateConfirmPassword = async (_rule: Rule, value: string) => {
2026-02-24 09:59:22 +08:00
if (!value) {
return Promise.reject('请确认密码')
}
if (value !== formData.password) {
return Promise.reject('两次输入的密码不一致')
}
return Promise.resolve()
}
const handleAccountInput = () => {
const normalized = sanitizeAccountInput(formData.account)
if (normalized !== formData.account) {
formData.account = normalized
}
}
const registerRules: Record<string, Rule[]> = {
2026-02-24 09:59:22 +08:00
account: [
{ required: true, message: '请输入账号', trigger: 'blur' },
{ pattern: ACCOUNT_REGEX, message: ACCOUNT_RULE_MESSAGE, trigger: 'blur' }
2026-02-24 09:59:22 +08:00
],
phone: [
{ required: true, message: '请输入手机号', trigger: 'blur' },
{ pattern: phoneRegex, message: '手机号格式不正确', trigger: 'blur' }
],
verificationCode: [
{ required: true, message: '请输入验证码', trigger: 'blur' },
{ pattern: verificationCodeRegex, message: '验证码必须为 6 位数字', trigger: 'blur' }
],
2026-02-24 09:59:22 +08:00
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ validator: validatePassword, trigger: 'blur' }
],
confirmPassword: [
{ required: true, message: '请确认密码', trigger: 'blur' },
{ validator: validateConfirmPassword, trigger: 'blur' }
],
nickname: [
{ required: true, message: '请输入昵称', trigger: 'blur' },
{ min: 2, max: 20, message: '昵称长度需为 2 到 20 个字符', trigger: 'blur' },
2026-03-19 14:10:59 +08:00
{ pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_]+$/, message: '昵称不能包含特殊符号', trigger: 'blur' }
2026-02-24 09:59:22 +08:00
],
shopId: [
{ required: true, message: '请选择店铺', trigger: 'change' }
]
}
const loginPasswordRules: Record<string, Rule[]> = {
account: [
{ required: true, message: '请输入账号', trigger: 'blur' },
{ pattern: ACCOUNT_REGEX, message: ACCOUNT_RULE_MESSAGE, trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
],
shopId: [
{ validator: validateLoginShopRequired, trigger: 'change' }
]
}
const loginPhoneRules: Record<string, Rule[]> = {
2026-03-19 14:10:59 +08:00
phone: [
{ required: true, message: '请输入手机号', trigger: 'blur' },
{ pattern: phoneRegex, message: '手机号格式不正确', trigger: 'blur' }
2026-03-19 14:10:59 +08:00
],
verificationCode: [
{ required: true, message: '请输入验证码', trigger: 'blur' },
{ pattern: verificationCodeRegex, message: '验证码必须为 6 位数字', trigger: 'blur' }
],
shopId: [
{ validator: validateLoginShopRequired, trigger: 'change' }
]
}
const forgotPasswordRules: Record<string, Rule[]> = {
phone: [
{ required: true, message: '请输入手机号', trigger: 'blur' },
{ pattern: phoneRegex, message: '手机号格式不正确', trigger: 'blur' }
],
verificationCode: [
{ required: true, message: '请输入验证码', trigger: 'blur' },
{ pattern: verificationCodeRegex, message: '验证码必须为 6 位数字', trigger: 'blur' }
2026-03-19 14:10:59 +08:00
],
newPassword: [
{ required: true, message: '请输入新密码', trigger: 'blur' }
]
}
const currentRules = computed<Record<string, Rule[]>>(() => {
if (activeTab.value === 'register') {
return registerRules
}
return loginType.value === 'phone' ? loginPhoneRules : loginPasswordRules
})
2026-02-24 09:59:22 +08:00
const fetchShops = async () => {
loadingShops.value = true
try {
const response = await shopApi.getShopsSimple()
2026-02-24 11:17:15 +08:00
shops.value = (response as unknown as any[]) || []
2026-02-24 09:59:22 +08:00
} catch (error) {
console.error('获取店铺列表失败:', error)
2026-03-19 20:51:07 +08:00
message.warning('店铺列表加载失败,普通登录不受影响')
2026-02-24 09:59:22 +08:00
} finally {
loadingShops.value = false
}
}
const resetLoginAccountStatus = () => {
if (loginAccountCheckTimer !== undefined) {
window.clearTimeout(loginAccountCheckTimer)
loginAccountCheckTimer = undefined
}
loginAccountStatus.value = null
}
const resetPhoneLoginShopCheck = () => {
if (loginPhoneShopCheckTimer !== undefined) {
window.clearTimeout(loginPhoneShopCheckTimer)
loginPhoneShopCheckTimer = undefined
}
}
const resetLoginShopState = (clearSelectedShop = activeTab.value === 'login') => {
resetPhoneLoginShopCheck()
loginShops.value = []
loginShopMessage.value = ''
allowLoginShopless.value = false
if (clearSelectedShop) {
formData.shopId = undefined
}
}
const normalizeShopOptionValue = (shopId?: string | number | null) => {
if (shopId === undefined || shopId === null || shopId === '') {
return undefined
}
return String(shopId)
}
const isLoginShopPrefetchUnsupported = (error: unknown) => {
const status = (error as any)?.response?.status
return status === 404
}
const applyLoginShopSelection = (selection?: LoginShopSelectionResponse | null) => {
loginShops.value = selection?.shops || []
allowLoginShopless.value = Boolean(selection?.allowShoplessLogin)
loginShopMessage.value = selection?.message || ''
if (allowLoginShopless.value) {
formData.shopId = undefined
return
}
if (!loginShops.value.length) {
formData.shopId = undefined
return
}
const currentShopId = normalizeShopOptionValue(formData.shopId)
const matchedShop = loginShops.value.find(shop => normalizeShopOptionValue(shop.id) === currentShopId)
if (matchedShop) {
formData.shopId = normalizeShopOptionValue(matchedShop.id)
return
}
if (loginShops.value.length === 1) {
const [onlyShop] = loginShops.value
if (onlyShop) {
formData.shopId = normalizeShopOptionValue(onlyShop.id)
return
}
}
formData.shopId = undefined
}
const loadLoginShopsByAccount = async (account = formData.account) => {
const trimmedAccount = account.trim()
if (activeTab.value !== 'login' || loginType.value !== 'password' || !trimmedAccount) {
resetLoginShopState()
return null
}
try {
const selection = await userApi.getLoginShopsByAccount(trimmedAccount) as unknown as LoginShopSelectionResponse
if (
activeTab.value === 'login'
&& loginType.value === 'password'
&& formData.account.trim() === trimmedAccount
) {
applyLoginShopSelection(selection)
}
return selection
} catch (error) {
if (formData.account.trim() === trimmedAccount) {
if (isLoginShopPrefetchUnsupported(error)) {
applyLoginShopSelection({
allowShoplessLogin: true,
message: '当前环境暂未开放登录前店铺预加载,可直接登录。',
shops: []
})
} else {
resetLoginShopState()
}
}
if (!isLoginShopPrefetchUnsupported(error)) {
console.error('登录店铺查询失败:', error)
}
return null
}
}
const loadLoginShopsByPhone = async (phone = formData.phone) => {
const trimmedPhone = phone.trim()
if (activeTab.value !== 'login' || loginType.value !== 'phone' || !phoneRegex.test(trimmedPhone)) {
resetLoginShopState()
return null
}
try {
const selection = await userApi.getLoginShopsByPhone(trimmedPhone) as unknown as LoginShopSelectionResponse
if (
activeTab.value === 'login'
&& loginType.value === 'phone'
&& formData.phone.trim() === trimmedPhone
) {
applyLoginShopSelection(selection)
}
return selection
} catch (error) {
if (formData.phone.trim() === trimmedPhone) {
if (isLoginShopPrefetchUnsupported(error)) {
applyLoginShopSelection({
allowShoplessLogin: true,
message: '当前环境暂未开放登录前店铺预加载,可直接登录。',
shops: []
})
} else {
resetLoginShopState()
}
}
if (!isLoginShopPrefetchUnsupported(error)) {
console.error('手机号登录店铺查询失败:', error)
}
return null
}
}
const checkLoginAccountAvailability = async (account = formData.account) => {
const trimmedAccount = account.trim()
if (activeTab.value !== 'login' || loginType.value !== 'password' || !trimmedAccount) {
loginAccountStatus.value = null
return null
}
try {
const response = await userApi.checkLoginAccountAvailability(trimmedAccount) as unknown as LoginAccountCheckResponse
if (
activeTab.value === 'login'
&& loginType.value === 'password'
&& formData.account.trim() === trimmedAccount
) {
loginAccountStatus.value = response
}
return response
} catch (error) {
if (formData.account.trim() === trimmedAccount) {
loginAccountStatus.value = null
}
console.error('登录账号检测失败:', error)
return null
}
}
const checkRegisterAccountAvailability = async (showWarning = true) => {
const trimmedAccount = formData.account.trim()
if (activeTab.value !== 'register' || !trimmedAccount) {
registerAccountStatus.value = null
return false
}
const response = await userApi.checkAccountAvailability(trimmedAccount) as unknown as AvailabilityCheckResponse
registerAccountStatus.value = response
if (showWarning && !response.available) {
message.warning(response.message)
}
return response.available
}
const checkRegisterPhoneAvailability = async (showWarning = true) => {
const trimmedPhone = formData.phone.trim()
if (activeTab.value !== 'register' || !trimmedPhone) {
registerPhoneStatus.value = null
return false
}
const response = await userApi.checkPhoneAvailability(trimmedPhone) as unknown as AvailabilityCheckResponse
registerPhoneStatus.value = response
if (showWarning && !response.available) {
message.warning(response.message)
}
return response.available
}
2026-02-24 09:59:22 +08:00
const handleSubmit = async () => {
loading.value = true
try {
if (activeTab.value === 'login') {
let response
if (loginType.value === 'phone') {
response = await authApi.phoneLogin({
phone: formData.phone,
verificationCode: formData.verificationCode,
shopId: formData.shopId
})
} else {
response = await authApi.login({
account: formData.account,
password: formData.password,
rememberMe: formData.rememberMe,
shopId: formData.shopId
})
}
const loginData = response as any
if (!loginData.accessToken || !loginData.refreshToken) {
console.error('登录响应缺少 token:', loginData)
message.error('登录失败:响应数据异常')
2026-03-19 20:51:07 +08:00
return
}
localStorage.setItem('accessToken', loginData.accessToken)
localStorage.setItem('refreshToken', loginData.refreshToken)
localStorage.setItem('userInfo', JSON.stringify(loginData.userInfo))
sessionStorage.setItem('accessToken', loginData.accessToken)
if (loginData.needSetPassword) {
localStorage.setItem('needSetPassword', 'true')
} else {
localStorage.removeItem('needSetPassword')
}
message.success('登录成功')
router.push('/home').catch(err => {
console.error('跳转首页失败:', err)
window.location.href = '/home'
2026-02-24 09:59:22 +08:00
})
return
2026-02-24 09:59:22 +08:00
}
const accountAvailable = await checkRegisterAccountAvailability(false)
const phoneAvailable = await checkRegisterPhoneAvailability(false)
if (!accountAvailable || !phoneAvailable) {
message.error('请先处理注册信息中的占用问题')
return
}
await authApi.register({
account: formData.account,
password: formData.password,
nickname: formData.nickname,
phone: formData.phone,
verificationCode: formData.verificationCode,
shopId: formData.shopId!
})
message.success('注册成功,请登录')
activeTab.value = 'login'
loginType.value = 'password'
formData.password = ''
formData.confirmPassword = ''
formData.verificationCode = ''
resetLoginAccountStatus()
2026-02-24 09:59:22 +08:00
} catch (error: any) {
console.error(activeTab.value === 'login' ? '登录失败:' : '注册失败:', error)
2026-02-24 09:59:22 +08:00
message.error(error.message || (activeTab.value === 'login' ? '登录失败' : '注册失败'))
} finally {
loading.value = false
}
}
const handleForgotPassword = () => {
2026-03-19 14:10:59 +08:00
forgotPasswordVisible.value = true
}
const handleSendForgotCode = async () => {
if (!forgotPasswordForm.phone) {
message.error('请输入手机号')
return
}
if (!phoneRegex.test(forgotPasswordForm.phone)) {
message.error('手机号格式不正确')
return
}
try {
await authApi.sendVerificationCode({ phone: forgotPasswordForm.phone })
message.success('验证码已发送')
forgotCountdown.value = 60
const timer = window.setInterval(() => {
2026-03-19 14:10:59 +08:00
forgotCountdown.value--
if (forgotCountdown.value <= 0) {
window.clearInterval(timer)
2026-03-19 14:10:59 +08:00
}
}, 1000)
} catch (error: any) {
message.error(error.response?.data?.message || '发送验证码失败')
}
}
const handleResetPassword = async () => {
resettingPassword.value = true
try {
await authApi.resetPasswordByPhone({
phone: forgotPasswordForm.phone,
verificationCode: forgotPasswordForm.verificationCode,
newPassword: forgotPasswordForm.newPassword
})
message.success('密码重置成功,请使用新密码登录')
forgotPasswordVisible.value = false
forgotPasswordForm.phone = ''
forgotPasswordForm.verificationCode = ''
forgotPasswordForm.newPassword = ''
} catch (error: any) {
message.error(error.response?.data?.message || '重置密码失败')
} finally {
resettingPassword.value = false
}
2026-02-24 09:59:22 +08:00
}
const handleWeChatLogin = () => {
message.info('微信登录功能开发中')
}
const handleQQLogin = () => {
message.info('QQ 登录功能开发中')
2026-03-19 20:51:07 +08:00
}
const handleRegisterAccountBlur = async () => {
if (activeTab.value !== 'register' || !formData.account.trim()) {
registerAccountStatus.value = null
return
}
try {
await checkRegisterAccountAvailability(false)
} catch (error: any) {
registerAccountStatus.value = null
message.error(error.message || '账号校验失败')
}
}
2026-03-19 20:51:07 +08:00
const handleRegisterPhoneBlur = async () => {
if (activeTab.value !== 'register' || !formData.phone.trim()) {
registerPhoneStatus.value = null
return
}
if (!phoneRegex.test(formData.phone)) {
registerPhoneStatus.value = null
return
}
try {
await checkRegisterPhoneAvailability(false)
} catch (error: any) {
registerPhoneStatus.value = null
message.error(error.message || '手机号校验失败')
}
}
const handleSendVerificationCode = async () => {
if (!formData.phone) {
message.error('请输入手机号')
return
}
2026-03-19 14:10:59 +08:00
if (!phoneRegex.test(formData.phone)) {
message.error('手机号格式不正确')
return
}
2026-03-19 20:51:07 +08:00
try {
2026-03-19 20:51:07 +08:00
if (activeTab.value === 'register') {
const phoneAvailable = await checkRegisterPhoneAvailability(false)
if (!phoneAvailable) {
message.warning(registerPhoneStatus.value?.message || '该手机号已被注册')
return
}
}
await userApi.sendVerificationCode({ phone: formData.phone })
message.success('验证码已发送')
countdown.value = 60
const timer = window.setInterval(() => {
countdown.value--
if (countdown.value <= 0) {
window.clearInterval(timer)
}
}, 1000)
} catch (error: any) {
message.error(error.message || '发送验证码失败')
}
}
const handleAdminLogin = () => {
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')
localStorage.removeItem('adminUserInfo')
sessionStorage.removeItem('accessToken')
sessionStorage.removeItem('adminAccessToken')
window.setTimeout(() => {
window.location.href = '/admin/login'
}, 100)
}
watch([activeTab, loginType], () => {
resetLoginAccountStatus()
resetLoginShopState(activeTab.value === 'login')
if (activeTab.value !== 'login') {
return
}
if (loginType.value === 'password' && formData.account.trim()) {
void Promise.all([
checkLoginAccountAvailability(formData.account.trim()),
loadLoginShopsByAccount(formData.account.trim())
])
return
}
if (loginType.value === 'phone' && phoneRegex.test(formData.phone.trim())) {
void loadLoginShopsByPhone(formData.phone.trim())
}
})
watch(
() => formData.account,
value => {
if (activeTab.value !== 'login' || loginType.value !== 'password') {
return
}
resetLoginAccountStatus()
const trimmedAccount = value.trim()
if (!trimmedAccount) {
return
}
loginAccountCheckTimer = window.setTimeout(() => {
loginAccountCheckTimer = undefined
void Promise.all([
checkLoginAccountAvailability(trimmedAccount),
loadLoginShopsByAccount(trimmedAccount)
])
}, 400)
}
)
watch(
() => formData.phone,
value => {
if (activeTab.value !== 'login' || loginType.value !== 'phone') {
return
}
resetPhoneLoginShopCheck()
const trimmedPhone = value.trim()
if (!phoneRegex.test(trimmedPhone)) {
resetLoginShopState()
return
}
loginPhoneShopCheckTimer = window.setTimeout(() => {
loginPhoneShopCheckTimer = undefined
void loadLoginShopsByPhone(trimmedPhone)
}, 400)
}
)
2026-02-24 09:59:22 +08:00
onMounted(() => {
fetchShops()
})
onBeforeUnmount(() => {
resetLoginAccountStatus()
resetPhoneLoginShopCheck()
})
2026-02-24 09:59:22 +08:00
</script>
<style scoped>
.mobile-container {
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.auth-container {
width: 100%;
max-width: 400px;
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
}
.logo-section {
text-align: center;
margin-bottom: 30px;
}
.logo {
font-size: 48px;
font-weight: bold;
color: #667eea;
margin-bottom: 10px;
}
.subtitle {
font-size: 16px;
color: #999;
}
.tab-container {
display: flex;
margin-bottom: 30px;
border-bottom: 2px solid #f0f0f0;
}
.tab-item {
flex: 1;
text-align: center;
padding: 15px;
font-size: 16px;
color: #999;
cursor: pointer;
transition: all 0.3s;
position: relative;
}
.tab-item.active {
color: #667eea;
font-weight: bold;
}
.tab-item.active::after {
content: '';
position: absolute;
bottom: -2px;
left: 0;
right: 0;
height: 2px;
background: #667eea;
}
.login-type-toggle {
margin-bottom: 20px;
text-align: center;
}
2026-02-24 09:59:22 +08:00
.auth-form {
margin-top: 20px;
}
.form-actions {
display: flex;
justify-content: space-between;
align-items: center;
}
2026-03-19 20:51:07 +08:00
.field-status {
margin-top: 8px;
font-size: 12px;
}
.field-status.success {
color: #52c41a;
}
.field-status.error {
color: #ff4d4f;
}
2026-02-24 09:59:22 +08:00
.forgot-password {
color: #667eea;
cursor: pointer;
}
.social-login {
margin-top: 30px;
}
.divider {
text-align: center;
position: relative;
margin-bottom: 20px;
}
.divider::before {
content: '';
position: absolute;
left: 0;
top: 50%;
width: 100%;
height: 1px;
background: #f0f0f0;
}
.divider span {
background: white;
padding: 0 15px;
color: #999;
font-size: 14px;
position: relative;
}
.social-buttons {
display: flex;
justify-content: center;
gap: 20px;
}
.social-buttons .ant-btn {
border-color: #f0f0f0;
}
.social-buttons .ant-btn:hover {
border-color: #667eea;
color: #667eea;
}
.admin-login {
text-align: center;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #f0f0f0;
}
.admin-login-link {
color: #667eea;
font-size: 14px;
cursor: pointer;
transition: all 0.3s;
}
.admin-login-link:hover {
color: #764ba2;
text-decoration: underline;
}
.phone-login-tip {
margin-top: 8px;
font-size: 12px;
color: #1890ff;
display: flex;
align-items: center;
}
2026-03-19 20:51:07 +08:00
</style>