改进用户认证系统和状态管理
- 优化登录接口,支持自动创建新用户实现快速体验 - 完善注册接口,添加用户名唯一性校验和积分奖励机制 - 扩展用户Store,新增注册方法和错误处理逻辑 - 改进Mock数据生成,使用随机头像API提升用户体验 - 增强API安全性,添加异常捕获和友好提示
This commit is contained in:
parent
3e06138b7f
commit
8c10fc53e7
@ -296,27 +296,65 @@ export const getUserById = async (id: string): Promise<User | null> => {
|
||||
|
||||
export const login = async (username: string, password: string): Promise<User | null> => {
|
||||
await delay()
|
||||
// Mock 登录逻辑
|
||||
const user = mockData.users.find((u) => u.username === username)
|
||||
return user || null
|
||||
|
||||
// Mock 登录逻辑 - 任意用户名密码都可以登录
|
||||
// 先查找是否已存在该用户
|
||||
let user = mockData.users.find((u) => u.username === username)
|
||||
|
||||
// 如果用户不存在,自动创建一个新用户(模拟登录)
|
||||
if (!user) {
|
||||
user = {
|
||||
id: `u${Date.now()}`,
|
||||
username: username,
|
||||
nickname: username,
|
||||
avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${username}`,
|
||||
email: `${username}@example.com`,
|
||||
phone: '13800138000',
|
||||
bio: `我是 ${username},热爱非遗文化`,
|
||||
favorites: [],
|
||||
followedInheritors: [],
|
||||
enrolledCourses: [],
|
||||
registeredEvents: [],
|
||||
points: 0,
|
||||
level: 1,
|
||||
createdAt: new Date().toISOString().split('T')[0],
|
||||
}
|
||||
// 将新用户添加到mockData中
|
||||
mockData.users.push(user)
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
export const register = async (userData: Partial<User>): Promise<User> => {
|
||||
await delay()
|
||||
|
||||
// Mock 注册逻辑 - 自动创建用户
|
||||
const username = userData.username || `user${Date.now()}`
|
||||
|
||||
// 检查用户名是否已存在
|
||||
const existingUser = mockData.users.find((u) => u.username === username)
|
||||
if (existingUser) {
|
||||
throw new Error('用户名已存在')
|
||||
}
|
||||
|
||||
const newUser: User = {
|
||||
id: `u${Date.now()}`,
|
||||
username: userData.username || '',
|
||||
nickname: userData.nickname || '',
|
||||
avatar: userData.avatar || 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=100',
|
||||
email: userData.email || '',
|
||||
username: username,
|
||||
nickname: userData.nickname || username,
|
||||
avatar: userData.avatar || `https://api.dicebear.com/7.x/avataaars/svg?seed=${username}`,
|
||||
email: userData.email || `${username}@example.com`,
|
||||
phone: userData.phone,
|
||||
bio: userData.bio,
|
||||
favorites: [],
|
||||
followedInheritors: [],
|
||||
enrolledCourses: [],
|
||||
registeredEvents: [],
|
||||
points: 0,
|
||||
points: 100, // 新用户赠送100积分
|
||||
level: 1,
|
||||
createdAt: new Date().toISOString().split('T')[0],
|
||||
}
|
||||
|
||||
mockData.users.push(newUser)
|
||||
return newUser
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
import type { User } from '@types/index'
|
||||
import { login as apiLogin, getUserById } from '@services/api'
|
||||
import { login as apiLogin, register as apiRegister, getUserById } from '@services/api'
|
||||
|
||||
interface UserState {
|
||||
user: User | null
|
||||
@ -14,6 +14,7 @@ interface UserState {
|
||||
|
||||
// Actions
|
||||
login: (username: string, password: string) => Promise<boolean>
|
||||
register: (username: string, email: string, password: string, phone?: string) => Promise<boolean>
|
||||
logout: () => void
|
||||
updateUser: (userData: Partial<User>) => void
|
||||
addFavorite: (heritageId: string) => void
|
||||
@ -47,6 +48,23 @@ export const useUserStore = create<UserState>()(
|
||||
}
|
||||
},
|
||||
|
||||
register: async (username: string, email: string, password: string, phone?: string) => {
|
||||
set({ isLoading: true })
|
||||
try {
|
||||
const user = await apiRegister({ username, email, password, phone })
|
||||
if (user) {
|
||||
set({ user, isAuthenticated: true, isLoading: false })
|
||||
return true
|
||||
}
|
||||
set({ isLoading: false })
|
||||
return false
|
||||
} catch (error) {
|
||||
console.error('Register failed:', error)
|
||||
set({ isLoading: false })
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
set({ user: null, isAuthenticated: false })
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user