43 lines
2.4 KiB
Markdown
43 lines
2.4 KiB
Markdown
# 创建分身未预填登录昵称 — 根因与修复
|
||
|
||
## 现象
|
||
登录成功后,进入「创建数字分身」,分身名称 / 显示名称没有默认填入当前登录用户的昵称;
|
||
而仪表盘(数字分身管理)却正常显示该昵称。两者读数不一致。
|
||
|
||
## 根因(前端取值优先级 bug)
|
||
`AvatarCreate.vue` 的 `onMounted` 原逻辑:
|
||
```js
|
||
const profile = avatarStore.userProfile || userStore.user
|
||
const nick = profile?.nickname
|
||
```
|
||
- `avatarStore.userProfile` 是一个 **truthy 对象**(即使昵称为空字符串),会抢在真正含昵称的
|
||
`userStore.user`(登录会话 + localStorage 持久化)之前被选中。
|
||
- `src/main.ts` 在应用启动时执行 `setNativeProfile({ nickname: userStore.user.nickname || '' })`:
|
||
当 localStorage 里是旧会话、昵称为空时,会把一个「空昵称对象」写进 `userProfile`。
|
||
- 于是 `profile.nickname === ''`,创建页不预填;仪表盘直接读 `userStore.user` 所以照常显示。
|
||
|
||
DB 实证:`avatar-test` 容器内 `/app/avatar.db` 的 `users` 表确有真实昵称(如 `猜猜我是谁`)与真实头像
|
||
URL —— 数据本身没问题,纯前端取值优先级问题。
|
||
|
||
## 修复
|
||
文件:`digital-avatar-app/src/views/AvatarCreate.vue`
|
||
1. `onMounted` 改为 **优先 `userStore.user`,`avatarStore.userProfile` 仅作兜底**:
|
||
```js
|
||
const real = userStore.user
|
||
const native = avatarStore.userProfile
|
||
const nick = real?.nickname || native?.nickname || ''
|
||
const avatar = real?.avatarUrl || native?.avatarUrl || ''
|
||
```
|
||
2. `userAvatarUrl` 计算属性同样改为优先 `userStore.user.avatarUrl`。
|
||
3. 兜底:本地两者皆空且已登录时,调用 `getCurrentUser()`(`GET /huihui/me`,读 DB 权威值)补拉真实昵称/头像。
|
||
|
||
## 部署与验证
|
||
- `./node_modules/.bin/vite build` → 新 chunk `AvatarCreate-CNMENOcc.js`
|
||
- 部署到测试环境 `geo-server`:`rsync dist/` → `docker cp` 进 `avatar-test-frontend:/usr/share/nginx/html/`
|
||
- 容器内清理旧 AvatarCreate 孤儿 chunk(`Bw_iuiqc.js` 等多份历史残留)
|
||
- 验证:首页 HTTP 200;线上 chunk 含 `displayName` 预填逻辑
|
||
|
||
## 经验
|
||
前端「当前用户资料」应统一以 `userStore.user`(登录会话、localStorage 持久化)为权威源;
|
||
`avatarStore.userProfile`(原生壳注入 / 启动桥接)是可空、可被空值覆盖的派生源,绝不能放在 `||` 左侧优先。
|