diff --git a/apps/h5-partner/src/lib/wechat-auth.ts b/apps/h5-partner/src/lib/wechat-auth.ts index 08c8be2..f1ba168 100644 --- a/apps/h5-partner/src/lib/wechat-auth.ts +++ b/apps/h5-partner/src/lib/wechat-auth.ts @@ -1,4 +1,5 @@ import type { WechatLoginResult } from '@dukang/shared-types'; +import { WECHAT_INAPP_REQUIRED_MSG } from '@dukang/weixin-sdk'; import { isWechatEnv, weixinSdk } from './weixin'; import { request, saveAuth } from './api'; @@ -31,37 +32,16 @@ export async function handlePartnerWechatCallback(): Promise { - if (clientConfigCache) return clientConfigCache; - try { - clientConfigCache = await request<{ mockWechat?: boolean }>('PARTNER_H5', '/common/client-config'); - } catch { - clientConfigCache = {}; - } - return clientConfigCache; -} - /** - * 微信授权登录(与 C 端一致:微信内走公众号 OAuth)。 + * 微信授权登录(对齐 C 端:仅微信内置浏览器走 OAuth)。 * 返回 true = 已登录;void = 已跳转授权页等待回调。 */ export async function loginPartnerWithWechat(): Promise { - if (isWechatEnv()) { - const result = await weixinSdk.login(); - if (result) return handlePartnerWechatLoginResult(result); - return; + if (!isWechatEnv()) { + throw new Error(WECHAT_INAPP_REQUIRED_MSG); } - const cfg = await fetchClientConfig(); - if (!cfg.mockWechat) { - throw new Error('请在微信内打开以使用微信一键授权'); - } - const result = await request('PARTNER_H5', '/partner/auth/login/wechat', { - method: 'POST', - body: JSON.stringify({ code: `mockcode_${Date.now()}`, platform: 'h5' }), - }); - return handlePartnerWechatLoginResult(result); + const result = await weixinSdk.login(); + if (result) return handlePartnerWechatLoginResult(result); } /** @@ -74,7 +54,7 @@ export async function bindPartnerWechatAfterSmsLogin(): Promise { export async function authorizePartnerWechat(): Promise { if (!isWechatEnv()) { - throw new Error('请在微信内打开以完成授权'); + throw new Error(WECHAT_INAPP_REQUIRED_MSG); } return weixinSdk.login(); } diff --git a/apps/h5-partner/src/pages/LoginPage.tsx b/apps/h5-partner/src/pages/LoginPage.tsx index ff56f47..acd50fc 100644 --- a/apps/h5-partner/src/pages/LoginPage.tsx +++ b/apps/h5-partner/src/pages/LoginPage.tsx @@ -8,6 +8,7 @@ import { loginPartnerWithWechat, } from '../lib/wechat-auth'; import { isWechatEnv } from '../lib/weixin'; +import { WECHAT_INAPP_REQUIRED_MSG } from '@dukang/weixin-sdk'; const REMEMBER_PHONE_KEY = 'partner_remember_phone'; const REMEMBER_FLAG_KEY = 'partner_remember_account'; @@ -132,6 +133,10 @@ export default function LoginPage() { async function wechatLogin() { if (!ensureAgreed()) return; setMsg(''); + if (!isWechatEnv()) { + setMsg(WECHAT_INAPP_REQUIRED_MSG); + return; + } setWxLoading(true); try { const ok = await loginPartnerWithWechat(); @@ -230,7 +235,7 @@ export default function LoginPage() { - {wxLoading ? '登录中...' : '微信一键登录'} + {wxLoading ? '登录中...' : '微信一键授权'}