feat: 新用户默认昵称及个人中心加载状态修复

- 新用户注册时随机生成普拉提主题默认昵称(16 个可选)
- 修复 App 启动后个人中心首次进入不展示用户信息的 bug
  - loggedIn 改为仅依赖 token 是否存在
  - 新增 hasProfile 判断用户数据是否已加载
  - 未加载时显示骨架屏而非空白
- 抽离随机函数为可注入依赖,消除 Math.random 测试耦合
This commit is contained in:
richarjiang
2026-04-05 10:21:58 +08:00
parent 982e569fa3
commit 640cfbf467
5 changed files with 89 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { User } from '@prisma/client'
import { UserRole } from '@mp-pilates/shared'
@@ -22,12 +22,39 @@ export interface JwtPayload {
*/
const sessionKeyStore = new Map<string, string>()
export const RANDOM_FN_TOKEN = 'RANDOM_FN_TOKEN'
const DEFAULT_NICKNAMES = [
'优雅普拉提',
'柔韧时光',
'轻盈姿态',
'身心合一',
'舒展生活',
'静享流动',
'普拉提修行者',
'姿态雕塑师',
'呼吸艺术家',
'柔美力量',
'线条雕刻师',
'优雅行者',
'轻盈韵律',
'内在平和',
'舒展之美',
]
function generateDefaultNickname(
randomFn: () => number = Math.random,
): string {
return DEFAULT_NICKNAMES[Math.floor(randomFn() * DEFAULT_NICKNAMES.length)]
}
@Injectable()
export class AuthService {
constructor(
private readonly prisma: PrismaService,
private readonly jwtService: JwtService,
private readonly wechatService: WechatService,
@Inject(RANDOM_FN_TOKEN) private readonly randomFn: () => number = Math.random,
) {}
async login(code: string): Promise<LoginResult> {
@@ -44,6 +71,7 @@ export class AuthService {
data: {
openid,
...(unionid !== undefined && { unionid }),
nickname: generateDefaultNickname(this.randomFn),
},
}))