feat(users): add version checking endpoint

Add app version checking functionality to notify users when updates are available. The feature extracts the current version from the x-App-Version header, compares it with the latest configured version, and returns update information including download links and release notes.
This commit is contained in:
2025-11-29 20:47:01 +08:00
parent ff2dfd5bb3
commit ae41a2b643
4 changed files with 204 additions and 1 deletions

View File

@@ -15,6 +15,8 @@ import { ResponseCode } from 'src/base.dto';
import { Transaction, Op } from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
import { UpdateUserDto, UpdateUserResponseDto } from './dto/update-user.dto';
import { ConfigService } from '@nestjs/config';
import { VersionCheckDto, VersionCheckResponseDto, VersionInfo } from './dto/version-check.dto';
import { UserPurchase, PurchaseType, PurchaseStatus, PurchasePlatform } from './models/user-purchase.model';
import { ApplePurchaseService } from './services/apple-purchase.service';
@@ -82,6 +84,7 @@ export class UsersService {
private readonly activityLogsService: ActivityLogsService,
private readonly userActivityService: UserActivityService,
private readonly badgeService: BadgeService,
private readonly configService: ConfigService,
) { }
async getProfile(user: AccessTokenPayload): Promise<UserResponseDto> {
@@ -2868,6 +2871,98 @@ export class UsersService {
}
}
/**
* 检查应用版本更新
*/
async checkVersion(query: VersionCheckDto): Promise<VersionCheckResponseDto> {
try {
this.logger.log(`版本检查请求 - 当前版本: ${query.currentVersion}, 平台: ${query.platform}`);
const currentVersion = query.currentVersion
if (!currentVersion) {
this.logger.log('当前版本号为空,返回默认版本信息');
return {
code: ResponseCode.SUCCESS,
message: '当前版本号为空',
data: null as any,
};
}
// 从环境变量获取配置
const latestVersion = this.configService.get<string>('APP_VERSION', '1.0.0');
const appStoreUrl = this.configService.get<string>('APP_STORE_URL', '');
// 版本比较
const needsUpdate = this.compareVersions(latestVersion, currentVersion) > 0;
// 构建响应数据
const versionInfo: VersionInfo = {
latestVersion,
appStoreUrl,
needsUpdate: needsUpdate,
updateMessage: this.getUpdateMessage(needsUpdate),
releaseNotes: this.getReleaseNotes(latestVersion),
};
this.logger.log(`版本检查结果: ${JSON.stringify(versionInfo)}`);
return {
code: ResponseCode.SUCCESS,
message: '版本检查成功',
data: versionInfo,
};
} catch (error) {
this.logger.error(`版本检查失败: ${error instanceof Error ? error.message : '未知错误'}`);
return {
code: ResponseCode.ERROR,
message: `版本检查失败: ${error instanceof Error ? error.message : '未知错误'}`,
data: null as any,
};
}
}
/**
* 比较两个语义化版本号
* @param version1 版本1
* @param version2 版本2
* @returns 1: version1 > version2, 0: version1 = version2, -1: version1 < version2
*/
private compareVersions(version1: string, version2: string): number {
const v1Parts = version1.split('.').map(Number);
const v2Parts = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part) return 1;
if (v1Part < v2Part) return -1;
}
return 0;
}
/**
* 获取更新消息
*/
private getUpdateMessage(needsUpdate: boolean): string {
if (needsUpdate) {
return '发现新版本,建议更新到最新版本以获得更好的体验';
}
return '当前已是最新版本';
}
/**
* 获取版本发布说明
*/
private getReleaseNotes(version: string): string {
// 这里可以从数据库或配置文件中获取版本发布说明
// 暂时返回示例数据
return '1. 优化多语言配置\n2. 锻炼通知点击直接查看锻炼详情\n3. 修复已知问题';
}
/**
* 标记勋章已展示
*/