feat(share): 更新分享关卡进度逻辑,优化时间限制检查

This commit is contained in:
richarjiang
2026-04-08 15:11:52 +08:00
parent 3d52cfe843
commit 2d0fee8a9a
3 changed files with 31 additions and 45 deletions

View File

@@ -10,6 +10,6 @@ NODE_ENV=production
PORT=3000
WX_APPID=wx0f9c909d20d19396
WX_SECRET=c5635680747cccf351f5f323c01178e6
WX_APPID=wxaf07f8bb60098991
WX_SECRET=b390c318fe83600e60fd4ad5d88b603f
JWT_SECRET=mp-xieyingen

View File

@@ -10,7 +10,7 @@ import { validateEnvironment } from './env.validation';
isGlobal: true,
load: [databaseConfig],
validate: validateEnvironment,
envFilePath: ['.env.local', '.env.production', '.env'],
envFilePath: ['.env', '.env.local', '.env.production'],
}),
],
exports: [ConfigModule],

View File

@@ -122,7 +122,13 @@ export class ShareService {
throw new NotFoundException('分享不存在或已过期');
}
// 2. 查找或创建 ShareParticipant
// 2. 查找关卡获取时间限制(提前查找,后面会用到)
const level = await this.levelRepository.findById(dto.levelId);
if (!level) {
throw new NotFoundException('关卡不存在');
}
// 3. 查找或创建 ShareParticipant
let participant =
await this.shareParticipantRepository.findByShareConfigAndParticipant(
config.id,
@@ -136,54 +142,34 @@ export class ShareService {
participant = await this.shareParticipantRepository.save(participant);
}
// 3. 如果 passed=true检查是否已有通关记录
if (dto.passed) {
const existing =
await this.shareLevelProgressRepository.findByParticipantAndLevel(
participant.id,
dto.levelId,
);
if (existing?.passed) {
const existingLevel = await this.levelRepository.findById(dto.levelId);
if (!existingLevel) {
throw new NotFoundException('关卡不存在');
}
const wasWithinTimeLimit =
existingLevel.timeLimit === null ||
existing.timeSpent <= existingLevel.timeLimit;
return {
passed: true,
timeLimit: existingLevel.timeLimit,
withinTimeLimit: wasWithinTimeLimit,
};
}
}
// 4. 查找关卡获取时间限制
const level = await this.levelRepository.findById(dto.levelId);
if (!level) {
throw new NotFoundException('关卡不存在');
}
// 5. 判断是否在时间限制内通过
const withinTimeLimit = dto.passed
? level.timeLimit === null || dto.timeSpent <= level.timeLimit
: false;
// 6. 创建或更新进度
// 4. 查找现有进度
let progress =
await this.shareLevelProgressRepository.findByParticipantAndLevel(
participant.id,
dto.levelId,
);
// 5. 如果 passed=true 且已有通关记录,直接返回(不覆盖)
if (dto.passed && progress?.passed) {
const wasWithinTimeLimit =
level.timeLimit === null || progress.timeSpent <= level.timeLimit;
return {
passed: true,
timeLimit: level.timeLimit,
withinTimeLimit: wasWithinTimeLimit,
};
}
// 6. 判断是否在时间限制内通过
const withinTimeLimit = dto.passed
? level.timeLimit === null || dto.timeSpent <= level.timeLimit
: false;
// 7. 创建或更新进度
if (progress) {
progress = this.shareLevelProgressRepository.create({
...progress,
passed: dto.passed,
timeSpent: dto.timeSpent,
completedAt: dto.passed ? new Date() : progress.completedAt,
});
progress.passed = dto.passed;
progress.timeSpent = dto.timeSpent;
progress.completedAt = dto.passed ? new Date() : progress.completedAt;
} else {
progress = this.shareLevelProgressRepository.create({
participantId: participant.id,