|
|
|
@@ -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 });
|
|
|
|
|