From 2d0fee8a9a1f2870e9d8ac1112be87220794dc81 Mon Sep 17 00:00:00 2001 From: richarjiang Date: Wed, 8 Apr 2026 15:11:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(share):=20=E6=9B=B4=E6=96=B0=E5=88=86?= =?UTF-8?q?=E4=BA=AB=E5=85=B3=E5=8D=A1=E8=BF=9B=E5=BA=A6=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E6=97=B6=E9=97=B4=E9=99=90=E5=88=B6?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.production | 4 +- src/config/config.module.ts | 2 +- src/modules/share/share.service.ts | 70 ++++++++++++------------------ 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/.env.production b/.env.production index 67128fe..3056a44 100644 --- a/.env.production +++ b/.env.production @@ -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 \ No newline at end of file diff --git a/src/config/config.module.ts b/src/config/config.module.ts index 2ea12b4..a58c04f 100644 --- a/src/config/config.module.ts +++ b/src/config/config.module.ts @@ -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], diff --git a/src/modules/share/share.service.ts b/src/modules/share/share.service.ts index c810184..57529b2 100644 --- a/src/modules/share/share.service.ts +++ b/src/modules/share/share.service.ts @@ -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,