微信登录日志

This commit is contained in:
2026-07-07 17:17:03 +08:00
parent 2c74f6a08c
commit 7e3c0011bb
4 changed files with 146 additions and 17 deletions
@@ -5,6 +5,11 @@ export type WechatActorRef = {
refId: bigint;
};
export function wechatActorRefFromAuth(actorType?: string, actorId?: bigint): WechatActorRef | undefined {
if (!actorType || actorId == null) return undefined;
return { refType: actorType, refId: actorId };
}
type LogWechatAuthInput = {
scene: string;
requestUrl?: string;
@@ -128,4 +128,29 @@ export class WechatLocationService {
return result;
}
async reportChooseImage(input: {
status: 'success' | 'fail';
errMsg?: string;
sourceType?: string;
stage?: string;
pageUrl?: string;
actorRef?: WechatActorRef;
}) {
const logId = await logWechatAuth(this.prisma, {
scene: 'CHOOSE_IMAGE',
requestUrl: input.pageUrl?.split('#')[0]?.slice(0, 512),
requestBody: {
status: input.status,
...(input.sourceType ? { sourceType: input.sourceType } : {}),
...(input.stage ? { stage: input.stage } : {}),
...(input.errMsg ? { errMsg: input.errMsg.slice(0, 200) } : {}),
},
responseBody: { reported: true },
status: input.status === 'success' ? 'SUCCESS' : 'FAILED',
errorMessage: input.status === 'fail' ? input.errMsg?.slice(0, 512) : undefined,
actorRef: input.actorRef,
});
return { ok: true, logId: logId.toString() };
}
}
@@ -6,6 +6,7 @@ import { WECHAT_PROVIDER } from '../../integrations/integrations.constants';
import type { IWechatProvider } from '../../integrations/wechat/wechat.interface';
import { OptionalJwtAuthGuard } from '../../common/guards/optional-jwt-auth.guard';
import type { AuthUser } from '../../common/guards/jwt-auth.guard';
import { wechatActorRefFromAuth } from '../../integrations/wechat/wechat-log.util';
import { WechatLocationService } from './wechat-location.service';
class PhoneNumberDto {
@@ -41,6 +42,29 @@ class WechatLocationDto {
errMsg?: string;
}
class WechatChooseImageDto {
@IsString()
@IsIn(['success', 'fail'])
status: 'success' | 'fail';
@IsString()
@IsOptional()
errMsg?: string;
@IsString()
@IsOptional()
sourceType?: string;
@IsString()
@IsIn(['jssdk', 'choose', 'read', 'empty'])
@IsOptional()
stage?: 'jssdk' | 'choose' | 'read' | 'empty';
@IsString()
@IsOptional()
pageUrl?: string;
}
@Controller('common/wechat')
export class WechatController {
constructor(
@@ -91,4 +115,18 @@ export class WechatController {
userId,
});
}
@Post('choose-image')
@UseGuards(OptionalJwtAuthGuard)
reportChooseImage(@Req() req: Request, @Body() dto: WechatChooseImageDto) {
const user = (req as Request & { user?: AuthUser }).user;
return this.locationService.reportChooseImage({
status: dto.status,
errMsg: dto.errMsg,
sourceType: dto.sourceType,
stage: dto.stage,
pageUrl: dto.pageUrl,
actorRef: wechatActorRefFromAuth(user?.actorType, user?.actorId),
});
}
}