23 lines
753 B
TypeScript
23 lines
753 B
TypeScript
import type { WechatLoginResult } from '@dukang/shared-types';
|
|
import Taro from '@tarojs/taro';
|
|
import { request } from './api';
|
|
import { syncMiniWechatProfile } from './mini-wechat-profile';
|
|
|
|
/** 小程序微信授权登录:Taro.login → /auth/login/wechat */
|
|
export async function loginWithWechat(): Promise<WechatLoginResult> {
|
|
const res = await Taro.login();
|
|
if (!res.code) {
|
|
throw new Error(res.errMsg || '微信登录失败,未获取到 code');
|
|
}
|
|
const result = await request<WechatLoginResult>('/auth/login/wechat', {
|
|
method: 'POST',
|
|
data: { code: res.code, platform: 'mini' },
|
|
});
|
|
if (result.accessToken) {
|
|
await syncMiniWechatProfile();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export { bindWechatForUser } from './pay-wechat';
|