1
This commit is contained in:
@@ -4,6 +4,7 @@ import { ElMessage } from 'element-plus'
|
||||
import { User } from '@element-plus/icons-vue'
|
||||
import { authApi } from '../../api/auth'
|
||||
import { deviceApi } from '../../api/device'
|
||||
import { getOrCreateDeviceId } from '../../utils/deviceId'
|
||||
|
||||
interface Props {
|
||||
modelValue: boolean
|
||||
@@ -11,7 +12,7 @@ interface Props {
|
||||
|
||||
interface Emits {
|
||||
(e: 'update:modelValue', value: boolean): void
|
||||
(e: 'loginSuccess', data: { token: string; user: any }): void
|
||||
(e: 'loginSuccess', data: { token: string; permissions?: string; expireTime?: string }): void
|
||||
(e: 'showRegister'): void
|
||||
}
|
||||
|
||||
@@ -31,16 +32,26 @@ async function handleAuth() {
|
||||
|
||||
authLoading.value = true
|
||||
try {
|
||||
await deviceApi.register({ username: authForm.value.username })
|
||||
const loginRes: any = await authApi.login(authForm.value)
|
||||
const data = loginRes?.data || loginRes
|
||||
// 获取或生成设备ID
|
||||
const deviceId = await getOrCreateDeviceId()
|
||||
|
||||
// 注册设备
|
||||
await deviceApi.register({
|
||||
username: authForm.value.username,
|
||||
deviceId: deviceId,
|
||||
os: navigator.platform
|
||||
})
|
||||
|
||||
// 登录
|
||||
const loginRes: any = await authApi.login({
|
||||
...authForm.value,
|
||||
clientId: deviceId
|
||||
})
|
||||
|
||||
emit('loginSuccess', {
|
||||
token: data.token,
|
||||
user: {
|
||||
username: data.username,
|
||||
permissions: data.permissions
|
||||
}
|
||||
token: loginRes.data.accessToken || loginRes.data.token,
|
||||
permissions: loginRes.data.permissions,
|
||||
expireTime: loginRes.data.expireTime
|
||||
})
|
||||
ElMessage.success('登录成功')
|
||||
resetForm()
|
||||
|
||||
@@ -3,6 +3,7 @@ import { ref, computed } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { User } from '@element-plus/icons-vue'
|
||||
import { authApi } from '../../api/auth'
|
||||
import { getOrCreateDeviceId } from '../../utils/deviceId'
|
||||
|
||||
interface Props {
|
||||
modelValue: boolean
|
||||
@@ -10,7 +11,7 @@ interface Props {
|
||||
|
||||
interface Emits {
|
||||
(e: 'update:modelValue', value: boolean): void
|
||||
(e: 'loginSuccess', data: { token: string; user: any }): void
|
||||
(e: 'loginSuccess', data: { token: string; permissions?: string; expireTime?: string }): void
|
||||
(e: 'backToLogin'): void
|
||||
}
|
||||
|
||||
@@ -42,8 +43,8 @@ async function checkUsernameAvailability() {
|
||||
|
||||
try {
|
||||
const res: any = await authApi.checkUsername(registerForm.value.username)
|
||||
const data = res?.data || res
|
||||
usernameCheckResult.value = data?.available || false
|
||||
// 后端返回 {code: 200, data: true/false},data 直接是布尔值
|
||||
usernameCheckResult.value = res.data
|
||||
} catch {
|
||||
usernameCheckResult.value = null
|
||||
}
|
||||
@@ -54,23 +55,34 @@ async function handleRegister() {
|
||||
|
||||
registerLoading.value = true
|
||||
try {
|
||||
await authApi.register({
|
||||
// 获取设备ID
|
||||
const deviceId = await getOrCreateDeviceId()
|
||||
|
||||
// 注册账号(传递设备ID用于判断是否赠送VIP)
|
||||
const registerRes: any = await authApi.register({
|
||||
username: registerForm.value.username,
|
||||
password: registerForm.value.password
|
||||
password: registerForm.value.password,
|
||||
deviceId: deviceId
|
||||
})
|
||||
|
||||
const loginRes: any = await authApi.login({
|
||||
username: registerForm.value.username,
|
||||
password: registerForm.value.password
|
||||
})
|
||||
const loginData = loginRes?.data || loginRes
|
||||
|
||||
emit('loginSuccess', {
|
||||
token: loginData.token,
|
||||
user: {
|
||||
username: loginData.username,
|
||||
permissions: loginData.permissions
|
||||
|
||||
// 显示注册成功和VIP信息
|
||||
if (registerRes.data.expireTime) {
|
||||
const expireDate = new Date(registerRes.data.expireTime)
|
||||
const now = new Date()
|
||||
const daysLeft = Math.ceil((expireDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24))
|
||||
|
||||
if (daysLeft > 0) {
|
||||
ElMessage.success(`注册成功!您获得了 ${daysLeft} 天VIP体验`)
|
||||
} else {
|
||||
ElMessage.warning('注册成功!该设备已使用过新人福利,请联系管理员续费')
|
||||
}
|
||||
}
|
||||
|
||||
// 使用注册返回的token直接登录
|
||||
emit('loginSuccess', {
|
||||
token: registerRes.data.accessToken || registerRes.data.token,
|
||||
permissions: registerRes.data.permissions,
|
||||
expireTime: registerRes.data.expireTime
|
||||
})
|
||||
resetForm()
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user