Files
dukang/apps/h5-partner/src/lib/wechat-auth.ts
T

39 lines
1.2 KiB
TypeScript

import type { WechatLoginResult } from '@dukang/shared-types';
import { isWechatEnv, weixinSdk } from './weixin';
import { request, saveAuth } from './api';
export type PartnerProfile = {
id: string;
name: string;
phone: string;
companyName: string;
hasWechat?: boolean;
};
export async function fetchPartnerProfile(): Promise<PartnerProfile> {
return request<PartnerProfile>('PARTNER_H5', '/partner/me');
}
/** 微信内上传照片前需完成公众号授权绑定 */
export function needsWechatAuth(profile: PartnerProfile | null): boolean {
return isWechatEnv() && !!profile && !profile.hasWechat;
}
export function savePartnerWechatAuth(result: WechatLoginResult): boolean {
if (!result.accessToken) return false;
saveAuth({ accessToken: result.accessToken });
return true;
}
export async function authorizePartnerWechat(): Promise<WechatLoginResult | void> {
if (!isWechatEnv()) {
throw new Error('请在微信内打开以完成授权');
}
return weixinSdk.login();
}
export async function handlePartnerWechatCallback(): Promise<WechatLoginResult | null> {
if (!isWechatEnv()) return null;
return weixinSdk.handleOAuthCallback();
}