import type { AxiosError } from 'axios'; import { axiosInstance } from '@/api/rbac'; import { omsUrl, unwrapData } from '@/api/oms/common'; import type { WeChatCustomerBindInput, WeChatCustomerRegisterInput, WeChatCustomerRegisterResult, WeChatOAuthUserInfo, WxFollowQrcodeResult, WxLoginQrcodeResult, WxLoginResult, } from './types'; export * from './types'; function unwrapOrSelf(body: unknown): T { if (body == null || typeof body !== 'object') { return body as T; } const record = body as Record; if ( 'qrCodeUrl' in record || 'jumpUrl' in record || 'accessToken' in record || 'openid' in record ) { return body as T; } return unwrapData(body); } /** 获取微信公众号登录二维码(loginUuid 可选,不传则由后端生成 key) */ export async function getLoginQrcode(loginUuid?: string): Promise { const res = await axiosInstance.get(omsUrl('/wechat/mp/login-qrcode'), { params: loginUuid ? { loginUuid } : undefined, }); return unwrapOrSelf(res.data); } /** 获取注册成功后跳转的公众号链接 */ export async function getFollowQrcode(): Promise { const res = await axiosInstance.get(omsUrl('/wechat/mp/follow-qrcode')); return unwrapOrSelf(res.data); } /** 查询扫码 key 是否已关联账号(轮询用,无值时不抛错) */ export async function getWxLoginStatus(loginUuid: string): Promise { const res = await axiosInstance.get('/rbac/auth/wx-login/status', { params: { loginUuid }, }); return unwrapOrSelf(res.data); } /** 是否属于「等待扫码」类错误(轮询时应静默继续) */ export function isWxLoginPendingError(error: unknown): boolean { const axiosErr = error as AxiosError<{ message?: string; Message?: string }>; const message = axiosErr?.response?.data?.message ?? axiosErr?.response?.data?.Message ?? axiosErr?.message ?? ''; const pendingHints = ['请扫码', '扫码登录', '等待扫码', '未扫码']; return pendingHints.some((hint) => message.includes(hint)); } /** 微信扫码登录轮询 */ export async function wxLogin(loginUuid: string): Promise { const res = await axiosInstance.post('/rbac/auth/wx-login', { loginUuid }); return unwrapOrSelf(res.data); } /** 微信网页授权登录(H5 点按钮,无需扫码) */ export async function wxOAuthLogin(code: string): Promise { const res = await axiosInstance.post('/rbac/auth/wx-oauth-login', { code }); return unwrapOrSelf(res.data); } /** OAuth code 换取微信用户信息 */ export async function getOAuthUserInfo(code: string): Promise { const res = await axiosInstance.post(omsUrl('/wechat/oauth/user-info'), { code }); return unwrapOrSelf(res.data); } /** 微信授权注册客户 */ export async function registerByWechat( input: WeChatCustomerRegisterInput, ): Promise { const res = await axiosInstance.post(omsUrl('/wechat/customer/register'), input); return unwrapOrSelf(res.data); } /** 微信授权绑定已有客户 */ export async function bindByWechat(input: WeChatCustomerBindInput): Promise { const res = await axiosInstance.post(omsUrl('/wechat/customer/bind'), input); unwrapOrSelf(res.data); }