小程序修改

This commit is contained in:
2026-07-12 19:44:36 +08:00
parent 3b4833b51a
commit e2dfb08de3
45 changed files with 2028 additions and 347 deletions
@@ -5,6 +5,7 @@ import {
BindPhoneDto,
BindWechatDto,
BindWechatPhoneDto,
MiniWechatProfileDto,
BootstrapSessionDto,
CheckPartnerPhoneDto,
LoginSmsDto,
@@ -96,6 +97,23 @@ export class UserAuthController {
);
}
@Post('auth/wechat/mini-profile')
@UseGuards(JwtAuthGuard)
updateMiniWechatProfile(
@Req() req: Request,
@CurrentUser() user: AuthUser,
@Body() dto: MiniWechatProfileDto,
) {
if (user.actorType !== 'USER') {
throw new BadRequestException('仅用户可更新资料');
}
const clientApp = resolveUserClientApp(req);
if (clientApp !== ClientApp.USER_MINI) {
throw new BadRequestException('仅小程序端可调用');
}
return this.authService.updateMiniWechatProfile(user.actorId, dto);
}
@Get('auth/me')
@UseGuards(JwtAuthGuard)
me(@CurrentUser() user: AuthUser) {
@@ -1240,6 +1240,54 @@ export class AuthService {
return this.buildSessionResponse(user, clientApp, user.deviceKey);
}
async updateMiniWechatProfile(
userId: bigint,
input: { nickname?: string; avatarUrl?: string },
) {
const user = await this.assertActiveUser(userId);
if (!user.wxOpenId) {
throw new BadRequestException('请先完成微信授权');
}
const data: {
nickname?: string;
avatarResourceId?: bigint;
} = {};
const nickname = input.nickname?.trim();
if (nickname && this.isDefaultNickname(user.nickname)) {
data.nickname = nickname.slice(0, 64);
}
const avatarUrl = input.avatarUrl?.trim();
if (avatarUrl && !user.avatarResourceId) {
const avatar = await this.prisma.commonResource.create({
data: {
ownerType: 'USER',
ownerId: userId,
bizType: 'AVATAR',
mediaType: 'IMAGE',
ossBucket: 'wechat',
ossKey: `wx-avatar/${user.wxOpenId}`,
url: avatarUrl,
status: 'ACTIVE',
},
});
data.avatarResourceId = avatar.id;
}
if (!data.nickname && !data.avatarResourceId) {
return this.formatUserProfile(user);
}
const updated = await this.prisma.user.update({
where: { id: userId },
data,
include: { avatar: true },
});
return this.formatUserProfile(updated);
}
async bindUserWechat(
userId: bigint,
input: { code?: string; wxSessionKey?: string },
@@ -94,6 +94,16 @@ export class BindWechatDto {
platform?: 'h5' | 'mini';
}
export class MiniWechatProfileDto {
@IsString()
@IsOptional()
nickname?: string;
@IsString()
@IsOptional()
avatarUrl?: string;
}
export class CheckPartnerPhoneDto {
@IsString()
@IsNotEmpty()