From 8c10fc53e75427f8d754e67516fc35cdfde97a3b Mon Sep 17 00:00:00 2001 From: Leo <98382335+gaoziman@users.noreply.github.com> Date: Fri, 10 Oct 2025 21:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E7=94=A8=E6=88=B7=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E7=B3=BB=E7=BB=9F=E5=92=8C=E7=8A=B6=E6=80=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化登录接口,支持自动创建新用户实现快速体验 - 完善注册接口,添加用户名唯一性校验和积分奖励机制 - 扩展用户Store,新增注册方法和错误处理逻辑 - 改进Mock数据生成,使用随机头像API提升用户体验 - 增强API安全性,添加异常捕获和友好提示 --- src/services/api.ts | 54 +++++++++++++++++++++++++++++++++------ src/store/useUserStore.ts | 20 ++++++++++++++- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/services/api.ts b/src/services/api.ts index 72e74af..2c80a24 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -296,27 +296,65 @@ export const getUserById = async (id: string): Promise => { export const login = async (username: string, password: string): Promise => { 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): Promise => { 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 } diff --git a/src/store/useUserStore.ts b/src/store/useUserStore.ts index 4f4cba8..5458afe 100644 --- a/src/store/useUserStore.ts +++ b/src/store/useUserStore.ts @@ -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 + register: (username: string, email: string, password: string, phone?: string) => Promise logout: () => void updateUser: (userData: Partial) => void addFavorite: (heritageId: string) => void @@ -47,6 +48,23 @@ export const useUserStore = create()( } }, + 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 }) },