feat(users): 添加App版本号追踪功能,支持用户版本更新记录

This commit is contained in:
richarjiang
2025-12-02 14:40:59 +08:00
parent 562c66a930
commit 6cdd2bc137
4 changed files with 23 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ export interface UserWithPurchaseStatus {
maxUsageCount: number;
favoriteTopicCount: number;
isVip: boolean;
appVersion?: string;
profile?: Pick<UserProfile, 'dailyStepsGoal' | 'dailyCaloriesGoal' | 'pilatesPurposes' | 'weight' | 'initialWeight' | 'targetWeight' | 'height' | 'activityLevel'>;
}

View File

@@ -126,6 +126,13 @@ export class User extends Model {
})
declare language: string;
@Column({
type: DataType.STRING,
allowNull: true,
comment: '用户当前使用的App版本号',
})
declare appVersion: string;
get isVip(): boolean {
return this.membershipExpiration ? dayjs(this.membershipExpiration).isAfter(dayjs()) : false;
}

View File

@@ -68,9 +68,12 @@ export class UsersController {
@ApiOperation({ summary: '获取用户信息' })
@ApiBody({ type: CreateUserDto })
@ApiResponse({ type: UserResponseDto })
async getProfile(@CurrentUser() user: AccessTokenPayload): Promise<UserResponseDto> {
this.logger.log(`get profile: ${JSON.stringify(user)}`);
return this.usersService.getProfile(user);
async getProfile(
@CurrentUser() user: AccessTokenPayload,
@AppVersion() appVersion: string | undefined,
): Promise<UserResponseDto> {
this.logger.log(`get profile: ${JSON.stringify(user)}, appVersion: ${appVersion}`);
return this.usersService.getProfile(user, appVersion);
}
// 获取历史体重记录

View File

@@ -87,10 +87,10 @@ export class UsersService {
private readonly configService: ConfigService,
) { }
async getProfile(user: AccessTokenPayload): Promise<UserResponseDto> {
async getProfile(user: AccessTokenPayload, appVersion?: string): Promise<UserResponseDto> {
try {
// 使用NestJS Logger (会通过winston输出)
this.logger.log(`getProfile: ${JSON.stringify(user)}`);
this.logger.log(`getProfile: ${JSON.stringify(user)}, appVersion: ${appVersion}`);
// 也可以直接使用winston logger
this.winstonLogger.info('getProfile method called', {
@@ -111,8 +111,13 @@ export class UsersService {
};
}
// 更新用户最后登录时间
// 更新用户最后登录时间和版本信息
existingUser.lastLogin = new Date();
if (appVersion && existingUser.appVersion !== appVersion) {
const oldVersion = existingUser.appVersion;
existingUser.appVersion = appVersion;
this.logger.log(`用户 ${existingUser.id} 版本更新: ${oldVersion || '无'} -> ${appVersion}`);
}
await existingUser.save();
const [profile] = await this.userProfileModel.findOrCreate({
@@ -136,6 +141,7 @@ export class UsersService {
maxUsageCount: DEFAULT_FREE_USAGE_COUNT,
isVip: existingUser.isVip,
gender: existingUser.gender,
appVersion: existingUser.appVersion,
dailyStepsGoal: profile?.dailyStepsGoal,
dailyCaloriesGoal: profile?.dailyCaloriesGoal,
pilatesPurposes: profile?.pilatesPurposes,