不再使用的代码清理
This commit is contained in:
@@ -189,6 +189,21 @@ const routes: RouteRecordRaw[] = [
|
||||
hidden: true
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/forgot-password',
|
||||
name: 'ForgotPassword',
|
||||
component: () => import('@/views/ForgotPassword/index.vue'),
|
||||
meta: {
|
||||
title: '重置密码',
|
||||
requiresAuth: false,
|
||||
hidden: true
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/reset-password',
|
||||
redirect: '/forgot-password',
|
||||
meta: { hidden: true }
|
||||
},
|
||||
// 调试页面(仅开发环境)
|
||||
{
|
||||
path: '/debug',
|
||||
|
||||
+26
-13
@@ -44,13 +44,22 @@ export class AuthService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码
|
||||
* 获取图形验证码(备用)
|
||||
*/
|
||||
static async getCaptcha(): Promise<CaptchaResponse> {
|
||||
const response = await http.get<CaptchaResponse>('/auth/captcha')
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信验证码(用于登录/注册/找回密码等场景)
|
||||
*/
|
||||
static async sendSmsCode(phone: string) {
|
||||
// backend-single: GET /auth/sms-code?phone=xxx
|
||||
const response = await http.get('/auth/sms-code', { params: { phone } })
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*/
|
||||
@@ -71,7 +80,8 @@ export class AuthService {
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
static async getCurrentUserInfo(): Promise<UserInfo> {
|
||||
const response = await http.get<UserInfo>('/auth/user/info')
|
||||
// backend-single: GET /auth/userInfo
|
||||
const response = await http.get<UserInfo>('/auth/userInfo')
|
||||
return response.data
|
||||
}
|
||||
|
||||
@@ -94,21 +104,24 @@ export class AuthService {
|
||||
* 重置密码
|
||||
*/
|
||||
static async resetPassword(data: ResetPasswordRequest): Promise<void> {
|
||||
return http.post<void>('/auth/reset-password', data)
|
||||
return http.post<void>('/auth/resetPassword', data)
|
||||
}
|
||||
|
||||
// 废弃:后端未提供该接口,保留新 sendSmsCode()
|
||||
/**
|
||||
* 发送验证码(已废弃:统一使用 sendSmsCode(phone))
|
||||
*/
|
||||
static async sendCode(_data: SendCodeRequest): Promise<void> {
|
||||
console.warn('sendCode 已废弃,请使用 sendSmsCode(phone)')
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* 验证验证码(不使用,登录由后端校验短信验证码)
|
||||
*/
|
||||
static async sendCode(data: SendCodeRequest): Promise<void> {
|
||||
return http.post<void>('/auth/send-code', data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证验证码
|
||||
*/
|
||||
static async verifyCode(data: VerifyCodeRequest): Promise<boolean> {
|
||||
return http.post<boolean>('/auth/verify-code', data)
|
||||
static async verifyCode(_data: VerifyCodeRequest): Promise<boolean> {
|
||||
console.warn('verifyCode 不需要在前端调用,登录时由后端校验短信验证码')
|
||||
return Promise.resolve(true)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+9
-17
@@ -2,17 +2,13 @@
|
||||
* 认证相关类型定义
|
||||
*/
|
||||
|
||||
// 登录请求
|
||||
// 登录请求(对齐 backend-single:手机号 + 短信验证码)
|
||||
export interface LoginRequest {
|
||||
/** 账号(支持账号/邮箱/手机号) */
|
||||
account: string
|
||||
/** 密码 */
|
||||
password: string
|
||||
/** 验证码 */
|
||||
captcha: string
|
||||
/** 验证码key */
|
||||
captchaKey: string
|
||||
/** 记住我 */
|
||||
/** 手机号 */
|
||||
phone: string
|
||||
/** 短信验证码(开发期为固定 123456) */
|
||||
smsCode: string
|
||||
/** 记住我(前端本地使用,可选) */
|
||||
rememberMe?: boolean
|
||||
}
|
||||
|
||||
@@ -138,16 +134,12 @@ export interface ChangePasswordRequest {
|
||||
|
||||
// 重置密码请求
|
||||
export interface ResetPasswordRequest {
|
||||
/** 账号 */
|
||||
account: string
|
||||
/** 手机号 */
|
||||
phone: string
|
||||
/** 新密码 */
|
||||
newPassword: string
|
||||
/** 确认新密码 */
|
||||
confirmPassword: string
|
||||
/** 验证码 */
|
||||
/** 验证码(固定 123456) */
|
||||
captcha: string
|
||||
/** 验证码key */
|
||||
captchaKey: string
|
||||
}
|
||||
|
||||
// 发送验证码请求
|
||||
|
||||
@@ -14,49 +14,31 @@
|
||||
label-width="80px"
|
||||
@submit.prevent="handleLogin"
|
||||
>
|
||||
<el-form-item label="账号" prop="account">
|
||||
<el-form-item label="手机号" prop="phone">
|
||||
<el-input
|
||||
v-model="loginForm.account"
|
||||
placeholder="请输入账号/邮箱/手机号"
|
||||
v-model="loginForm.phone"
|
||||
placeholder="请输入11位手机号"
|
||||
:prefix-icon="User"
|
||||
clearable
|
||||
@keyup.enter="handleLogin"
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="密码" prop="password">
|
||||
<el-input
|
||||
v-model="loginForm.password"
|
||||
type="password"
|
||||
placeholder="请输入密码"
|
||||
:prefix-icon="Lock"
|
||||
show-password
|
||||
clearable
|
||||
@keyup.enter="handleLogin"
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="验证码" prop="captcha">
|
||||
<div class="flex gap-2">
|
||||
<el-form-item label="短信验证码" prop="smsCode">
|
||||
<div class="flex gap-2 w-full">
|
||||
<el-input
|
||||
v-model="loginForm.captcha"
|
||||
placeholder="请输入验证码"
|
||||
v-model="loginForm.smsCode"
|
||||
placeholder="请输入6位验证码"
|
||||
:prefix-icon="Key"
|
||||
clearable
|
||||
class="flex-1"
|
||||
@keyup.enter="handleLogin"
|
||||
maxlength="6"
|
||||
show-word-limit
|
||||
/>
|
||||
<div class="captcha-container" @click="refreshCaptcha">
|
||||
<img
|
||||
v-if="captchaImage"
|
||||
:src="captchaImage"
|
||||
alt="验证码"
|
||||
class="captcha-image"
|
||||
/>
|
||||
<div v-else class="captcha-loading">
|
||||
<el-icon class="is-loading"><Loading /></el-icon>
|
||||
</div>
|
||||
</div>
|
||||
<el-button type="primary" plain @click="handleSendSmsCode" :disabled="smsSending || countdown > 0">
|
||||
{{ countdown > 0 ? `${countdown}s` : (smsSending ? '发送中...' : '获取验证码') }}
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
@@ -135,56 +117,61 @@ const loginFormRef = ref<FormInstance>()
|
||||
// 加载状态
|
||||
const loading = ref(false)
|
||||
|
||||
// 验证码相关
|
||||
const captchaImage = ref('')
|
||||
const captchaKey = ref('')
|
||||
// 短信验证码发送状态
|
||||
const smsSending = ref(false)
|
||||
const countdown = ref(0)
|
||||
let timer: number | null = null
|
||||
|
||||
// 登录表单数据
|
||||
// 登录表单数据(手机号+短信验证码)
|
||||
const loginForm = reactive<LoginRequest>({
|
||||
account: '',
|
||||
password: '',
|
||||
captcha: '',
|
||||
captchaKey: '',
|
||||
phone: '',
|
||||
smsCode: '',
|
||||
rememberMe: false
|
||||
})
|
||||
|
||||
// 表单验证规则
|
||||
// 表单验证规则(手机号 + 短信验证码)
|
||||
const loginRules: FormRules = {
|
||||
account: [
|
||||
{ required: true, message: '请输入账号', trigger: 'blur' }
|
||||
phone: [
|
||||
{ required: true, message: '请输入手机号', trigger: 'blur' },
|
||||
{ pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
|
||||
],
|
||||
password: [
|
||||
{ required: true, message: '请输入密码', trigger: 'blur' },
|
||||
{ min: 6, max: 20, message: '密码长度必须在6-20位之间', trigger: 'blur' }
|
||||
],
|
||||
captcha: [
|
||||
smsCode: [
|
||||
{ required: true, message: '请输入验证码', trigger: 'blur' },
|
||||
{ min: 4, max: 6, message: '验证码长度不正确', trigger: 'blur' }
|
||||
{ min: 6, max: 6, message: '验证码必须为6位', trigger: 'blur' }
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码
|
||||
* 发送短信验证码
|
||||
*/
|
||||
const getCaptcha = async () => {
|
||||
try {
|
||||
const response = await AuthService.getCaptcha()
|
||||
// 后端返回的数据已经包含了 data:image/png;base64, 前缀,直接使用
|
||||
captchaImage.value = response.captchaImage
|
||||
captchaKey.value = response.captchaKey
|
||||
loginForm.captchaKey = response.captchaKey
|
||||
} catch (error) {
|
||||
console.error('获取验证码失败:', error)
|
||||
ElMessage.error('获取验证码失败')
|
||||
const handleSendSmsCode = async () => {
|
||||
if (!loginForm.phone) {
|
||||
ElMessage.warning('请先填写手机号')
|
||||
return
|
||||
}
|
||||
if (!/^1[3-9]\d{9}$/.test(loginForm.phone)) {
|
||||
ElMessage.warning('手机号格式不正确')
|
||||
return
|
||||
}
|
||||
try {
|
||||
smsSending.value = true
|
||||
await AuthService.sendSmsCode(loginForm.phone)
|
||||
ElMessage.success('验证码已发送,请注意查收')
|
||||
// 启动倒计时
|
||||
countdown.value = 60
|
||||
timer && clearInterval(timer)
|
||||
timer = window.setInterval(() => {
|
||||
countdown.value -= 1
|
||||
if (countdown.value <= 0 && timer) {
|
||||
clearInterval(timer)
|
||||
timer = null
|
||||
}
|
||||
}, 1000)
|
||||
} catch (error) {
|
||||
console.error('发送短信验证码失败:', error)
|
||||
} finally {
|
||||
smsSending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新验证码
|
||||
*/
|
||||
const refreshCaptcha = () => {
|
||||
loginForm.captcha = ''
|
||||
getCaptcha()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,44 +181,23 @@ const handleLogin = async () => {
|
||||
if (!loginFormRef.value) return
|
||||
|
||||
try {
|
||||
console.log('开始登录流程...')
|
||||
console.log('登录表单数据:', loginForm)
|
||||
console.log('🔐 开始登录流程...')
|
||||
|
||||
// 表单验证
|
||||
await loginFormRef.value.validate()
|
||||
console.log('表单验证通过')
|
||||
|
||||
loading.value = true
|
||||
|
||||
// 调用登录接口
|
||||
console.log('调用登录接口...')
|
||||
// 调用登录接口(手机号 + 短信验证码)
|
||||
const success = await authStore.login(loginForm)
|
||||
console.log('登录结果:', success)
|
||||
|
||||
if (success) {
|
||||
// 登录成功,确保认证状态已正确设置
|
||||
console.log('登录成功,当前认证状态:', {
|
||||
isLoggedIn: authStore.isLoggedIn,
|
||||
hasToken: !!authStore.accessToken,
|
||||
hasUserInfo: !!authStore.userInfo
|
||||
})
|
||||
|
||||
// 跳转到目标页面或首页
|
||||
const redirect = route.query.redirect as string || '/'
|
||||
console.log('登录成功,跳转到:', redirect)
|
||||
|
||||
// 使用路由跳转而不是window.location.href,避免base路径问题
|
||||
const redirect = (route.query.redirect as string) || '/'
|
||||
await router.push(redirect)
|
||||
} else {
|
||||
// 登录失败,刷新验证码
|
||||
console.log('登录失败,刷新验证码')
|
||||
refreshCaptcha()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录过程中发生错误:', error)
|
||||
ElMessage.error('登录失败,请检查网络连接或稍后重试')
|
||||
// 刷新验证码
|
||||
refreshCaptcha()
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -245,13 +211,10 @@ const handleSocialLogin = (platform: 'wechat' | 'qq') => {
|
||||
// TODO: 实现第三方登录逻辑
|
||||
}
|
||||
|
||||
// 组件挂载时获取验证码
|
||||
// 组件挂载时:如果已登录直接跳转
|
||||
onMounted(() => {
|
||||
getCaptcha()
|
||||
|
||||
// 如果已经登录,直接跳转
|
||||
if (authStore.isLoggedIn) {
|
||||
const redirect = route.query.redirect as string || '/'
|
||||
const redirect = (route.query.redirect as string) || '/'
|
||||
router.push(redirect)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user