@@ -567,6 +567,8 @@ model PartnerAccount {
|
||||
name String @db.VarChar(64)
|
||||
wxOpenId String? @map("wx_open_id") @db.VarChar(64)
|
||||
wxUnionId String? @map("wx_union_id") @db.VarChar(64)
|
||||
wxNickname String? @map("wx_nickname") @db.VarChar(64)
|
||||
wxAvatarUrl String? @map("wx_avatar_url") @db.VarChar(512)
|
||||
isPrimary Int @default(0) @map("is_primary") @db.TinyInt
|
||||
parentAccountId BigInt? @map("parent_account_id") @db.UnsignedBigInt
|
||||
staffRole PartnerStaffRole? @map("staff_role")
|
||||
|
||||
@@ -48,7 +48,7 @@ export class WechatMockProvider implements IWechatProvider {
|
||||
return {
|
||||
openId,
|
||||
nickname: 'Mock微信用户',
|
||||
headImgUrl: undefined,
|
||||
headImgUrl: `https://api.dicebear.com/7.x/avataaars/svg?seed=${encodeURIComponent(openId)}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -231,6 +231,9 @@ export class AuthService {
|
||||
isPrimary: number;
|
||||
staffRole: string | null;
|
||||
permissions?: unknown;
|
||||
wxOpenId?: string | null;
|
||||
wxNickname?: string | null;
|
||||
wxAvatarUrl?: string | null;
|
||||
},
|
||||
primary: { id: bigint; companyName: string | null },
|
||||
) {
|
||||
@@ -243,9 +246,37 @@ export class AuthService {
|
||||
staffRole: account.staffRole ?? undefined,
|
||||
companyName: primary.companyName ?? undefined,
|
||||
permissions: Array.isArray(account.permissions) ? account.permissions : undefined,
|
||||
hasWechat: !!account.wxOpenId,
|
||||
wxNickname: account.wxNickname ?? undefined,
|
||||
wxAvatarUrl: account.wxAvatarUrl ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/** 公众号 snsapi_userinfo:写入合伙人微信昵称与头像 */
|
||||
private async syncPartnerWechatProfile(
|
||||
partnerAccountId: bigint,
|
||||
accessToken: string | undefined,
|
||||
openId: string,
|
||||
) {
|
||||
if (!accessToken) return null;
|
||||
try {
|
||||
const info = await this.wechatProvider.fetchOAuthUserInfo(accessToken, openId, {
|
||||
refType: 'PARTNER',
|
||||
refId: partnerAccountId,
|
||||
});
|
||||
const data: { wxNickname?: string; wxAvatarUrl?: string } = {};
|
||||
if (info.nickname?.trim()) data.wxNickname = info.nickname.trim().slice(0, 64);
|
||||
if (info.headImgUrl?.trim()) data.wxAvatarUrl = info.headImgUrl.trim().slice(0, 512);
|
||||
if (!data.wxNickname && !data.wxAvatarUrl) return null;
|
||||
return this.prisma.partnerAccount.update({
|
||||
where: { id: partnerAccountId },
|
||||
data,
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async assertPartnerAccountByPhone(phone: string) {
|
||||
const account = await this.prisma.partnerAccount.findUnique({
|
||||
where: { phone },
|
||||
@@ -1547,7 +1578,7 @@ export class AuthService {
|
||||
throw new BadRequestException('该微信已绑定其他合伙人账号');
|
||||
}
|
||||
|
||||
const updated = await this.prisma.partnerAccount.update({
|
||||
let updated = await this.prisma.partnerAccount.update({
|
||||
where: { id: partnerAccountId },
|
||||
data: {
|
||||
wxOpenId: session.openId,
|
||||
@@ -1555,6 +1586,12 @@ export class AuthService {
|
||||
lastLoginAt: new Date(),
|
||||
},
|
||||
});
|
||||
const synced = await this.syncPartnerWechatProfile(
|
||||
updated.id,
|
||||
'accessToken' in session ? session.accessToken : undefined,
|
||||
session.openId,
|
||||
);
|
||||
if (synced) updated = synced;
|
||||
const primary = await this.resolvePrimaryAccount(updated.id);
|
||||
|
||||
this.trackPartnerEvent(updated.id, primary.id, clientApp, 'partner_wechat_bind', { platform });
|
||||
@@ -1593,6 +1630,12 @@ export class AuthService {
|
||||
lastLoginAt: new Date(),
|
||||
},
|
||||
});
|
||||
const synced = await this.syncPartnerWechatProfile(
|
||||
account.id,
|
||||
'accessToken' in session ? session.accessToken : undefined,
|
||||
session.openId,
|
||||
);
|
||||
if (synced) account = synced;
|
||||
const primary = await this.resolvePrimaryAccount(account.id);
|
||||
|
||||
this.trackPartnerEvent(account.id, primary.id, clientApp, 'partner_wechat_login', { platform });
|
||||
|
||||
@@ -267,6 +267,8 @@ export class PartnerMeController {
|
||||
primaryName: primary.name,
|
||||
companyName: primary.companyName ?? undefined,
|
||||
hasWechat: !!account.wxOpenId,
|
||||
wxNickname: account.wxNickname ?? undefined,
|
||||
wxAvatarUrl: account.wxAvatarUrl ?? undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user