From 8d74bdbe79b1668ff0a6474eeb16fb24ac22130f Mon Sep 17 00:00:00 2001 From: zyh Date: Thu, 10 Apr 2025 14:16:54 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AFAPI=E8=B7=AF=E7=94=B1=EF=BC=8C=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=BA'/me/profile'=EF=BC=8C=E5=B9=B6=E5=9C=A8=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=A1=B5=E9=9D=A2=E4=B8=AD=E6=95=B4=E5=90=88=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E5=8A=A0=E8=BD=BD=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E6=8F=90=E5=8D=87=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BD=93=E9=AA=8C=E5=92=8C=E4=BB=A3=E7=A0=81=E5=8F=AF=E7=BB=B4?= =?UTF-8?q?=E6=8A=A4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/mobile/api.ts | 6 +++--- client/mobile/pages_settings.tsx | 34 +++++++++++++++++++++++++++++--- server/routes_users.ts | 10 +++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/client/mobile/api.ts b/client/mobile/api.ts index 647cd66..d2da234 100644 --- a/client/mobile/api.ts +++ b/client/mobile/api.ts @@ -152,7 +152,7 @@ interface UsersResponse { }; } -interface UserResponse { +export interface UserResponse { data: User; message?: string; } @@ -227,7 +227,7 @@ export const UserAPI = { // 获取当前用户信息 getCurrentUser: async (): Promise => { try { - const response = await axios.get(`${API_BASE_URL}/users/me`); + const response = await axios.get(`${API_BASE_URL}/users/me/profile`); return response.data; } catch (error) { throw error; @@ -237,7 +237,7 @@ export const UserAPI = { // 更新当前用户信息 updateCurrentUser: async (userData: Partial): Promise => { try { - const response = await axios.put(`${API_BASE_URL}/users/me`, userData); + const response = await axios.put(`${API_BASE_URL}/users/me/profile`, userData); return response.data; } catch (error) { throw error; diff --git a/client/mobile/pages_settings.tsx b/client/mobile/pages_settings.tsx index 6fa6a5c..8a581e4 100644 --- a/client/mobile/pages_settings.tsx +++ b/client/mobile/pages_settings.tsx @@ -1,11 +1,18 @@ import React from 'react' import { useForm } from 'react-hook-form' -import { useMutation } from '@tanstack/react-query' +import { useMutation, useQuery } from '@tanstack/react-query' import { UserAPI } from './api.ts' -import { useAuth } from './hooks.tsx' +import type { User } from '../share/types.ts' +import type { UserResponse } from './api.ts' export default function SettingsPage() { - const { register, handleSubmit, formState: { errors } } = useForm<{ + const { data: userResponse, isLoading } = useQuery({ + queryKey: ['currentUser'], + queryFn: () => UserAPI.getCurrentUser(), + select: (response) => response.data + }) + + const { register, handleSubmit, formState: { errors }, reset } = useForm<{ nickname: string email: string phone?: string @@ -42,6 +49,27 @@ export default function SettingsPage() { updateUser(data) } + React.useEffect(() => { + if (userResponse) { + reset({ + nickname: userResponse.nickname || '', + email: userResponse.email || '', + phone: userResponse.phone || '' + }) + } + }, [userResponse, reset]) + + if (isLoading) { + return ( +
+

设置

+
+

加载中...

+
+
+ ) + } + return (

设置

diff --git a/server/routes_users.ts b/server/routes_users.ts index 1db6478..53c0fb2 100644 --- a/server/routes_users.ts +++ b/server/routes_users.ts @@ -238,14 +238,14 @@ export function createUserRoutes(withAuth: WithAuth) { }) // 获取当前用户信息 - usersRoutes.get('/me', withAuth, async (c) => { + usersRoutes.get('/me/profile', withAuth, async (c) => { try { const user = c.get('user')! const apiClient = c.get('apiClient') const userData = await apiClient.database.table('users') .where('id', user.id) - .select('id', 'username', 'nickname', 'email', 'phone', 'role', 'created_at') + .select('id', 'username', 'nickname', 'email', 'phone', 'created_at') .first() if (!user) { @@ -253,7 +253,7 @@ export function createUserRoutes(withAuth: WithAuth) { } return c.json({ - data: user, + data: userData, message: '获取用户详情成功' }) } catch (error) { @@ -263,7 +263,7 @@ export function createUserRoutes(withAuth: WithAuth) { }) // 更新当前用户信息 - usersRoutes.put('/me', withAuth, async (c) => { + usersRoutes.put('/me/profile', withAuth, async (c) => { try { const user = c.get('user')! const apiClient = c.get('apiClient') @@ -294,7 +294,7 @@ export function createUserRoutes(withAuth: WithAuth) { const updatedUser = await apiClient.database.table('users') .where('id', user.id) - .select('id', 'username', 'nickname', 'email', 'phone', 'role', 'created_at') + .select('id', 'username', 'nickname', 'email', 'phone', 'created_at') .first() return c.json({