From c5da01063c2e1987ed5078305f84713775efe625 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Mon, 14 Sep 2026 19:55:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=98=B5=E7=A7=B0=E5=8F=AF=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E2=80=94=E2=80=94PUT=20/api/auth/nickname=20+=20?= =?UTF-8?q?=E6=88=91=E7=9A=84=E9=A1=B5=E6=98=B5=E7=A7=B0=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/pages/profile/profile.vue | 36 ++++++++++++++++++++++++++-- server/src/routes/auth.js | 23 ++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/client/src/pages/profile/profile.vue b/client/src/pages/profile/profile.vue index b397441..1c679a2 100644 --- a/client/src/pages/profile/profile.vue +++ b/client/src/pages/profile/profile.vue @@ -15,8 +15,8 @@ - - {{ userStore.userInfo?.nickName || '探索者' }} + + {{ userStore.userInfo?.nickName || '探索者' }} ✏️ {{ profileSubText }} @@ -229,6 +229,37 @@ function dimPercent(dimId) { return Math.round(((stats.value?.litByDim?.[dimId] || 0) / total) * 100) } +// 修改昵称(点击我的页昵称) +function editNickname() { + if (!userStore.isLoggedIn) { + uni.showModal({ + title: '请先登录', + content: '登录后即可修改昵称', + confirmText: '去登录', + showCancel: false, + success: () => uni.navigateTo({ url: '/pages/login/login' }), + }) + return + } + uni.showModal({ + title: '修改昵称', + editable: true, + placeholderText: '请输入新昵称(最多20字)', + success: async (m) => { + if (!m.confirm) return + const nickName = String(m.content || '').trim() + if (!nickName) return uni.showToast({ title: '昵称不能为空', icon: 'none' }) + try { + await request('PUT', '/api/auth/nickname', { nickName }, userStore.token) + await userStore.fetchMe() + uni.showToast({ title: '昵称已更新', icon: 'success' }) + } catch (e) { + uni.showToast({ title: e.message || '修改失败', icon: 'none' }) + } + }, + }) +} + function goDim(id) { uni.navigateTo({ url: `/pages/dimension/dimension?id=${id}` }) } @@ -325,6 +356,7 @@ onShareTimeline(() => ({ color: var(--text-primary); margin-bottom: 4px; } +.nick-edit { font-size: 14px; opacity: 0.7; } .profile-sub { display: block; font-size: 12px; diff --git a/server/src/routes/auth.js b/server/src/routes/auth.js index d878ad8..a324943 100755 --- a/server/src/routes/auth.js +++ b/server/src/routes/auth.js @@ -539,6 +539,29 @@ router.get('/me', async (req, res) => { } }) +/** + * 修改昵称 + * PUT /api/auth/nickname + */ +router.put('/nickname', async (req, res) => { + try { + const token = req.header('Authorization')?.replace('Bearer ', '') + if (!token) return ApiResponse.unauthorized(res) + const decoded = jwt.verify(token, process.env.JWT_SECRET) + const nickName = String(req.body?.nickName || '').trim().slice(0, 20) + if (!nickName) return ApiResponse.error(res, '昵称不能为空', 400) + const user = await User.findByIdAndUpdate(decoded.userId, { nickName }, { new: true }) + if (!user) return ApiResponse.notFound(res, '用户不存在') + return ApiResponse.success(res, { nickName: user.nickName }, '昵称已更新') + } catch (error) { + if (error.name === 'JsonWebTokenError' || error.name === 'TokenExpiredError') { + return ApiResponse.unauthorized(res, '认证令牌无效或已过期') + } + logger.error('修改昵称失败:', error) + return ApiResponse.serverError(res, error.message) + } +}) + /** * WebView Token 交换接口 * POST /api/auth/webview-token