微信授权逻辑修改

This commit is contained in:
2026-07-06 20:56:25 +08:00
parent b12ee13232
commit 9d221cfcd2
12 changed files with 475 additions and 28 deletions
@@ -9,6 +9,12 @@ import { track } from '../lib/analytics';
import PhoneVerifySheet from '../components/PhoneVerifySheet';
import { useUserSession } from '../contexts/UserSessionContext';
import { tryGetClientGpsLocation } from '../lib/client-location';
import {
applyWechatLoginResult,
ensureWechatAuthForPay,
handleWechatAuthCallback,
} from '../lib/wechat-auth';
import { isWechatEnv } from '../lib/weixin';
type Address = {
id: string;
@@ -55,6 +61,8 @@ export default function OrderConfirmPage() {
const navigate = useNavigate();
const { phoneVerified, refreshProfile } = useUserSession();
const [showPhoneVerify, setShowPhoneVerify] = useState(false);
const [showWechatBindPhone, setShowWechatBindPhone] = useState(false);
const [wxSessionKey, setWxSessionKey] = useState<string | null>(null);
const [pendingSubmit, setPendingSubmit] = useState(false);
const productId = params.get('productId') || '';
const forceCross = params.get('cross') === '1';
@@ -84,6 +92,17 @@ export default function OrderConfirmPage() {
}
}, [productId, quantity]);
useEffect(() => {
if (!isWechatEnv()) return;
handleWechatAuthCallback()
.then((result) => {
if (result && applyWechatLoginResult(result)) {
void refreshProfile();
}
})
.catch(() => {});
}, [refreshProfile]);
useEffect(() => {
if (!productId || !addressId) return;
request<OrderPreview>('USER_H5', '/trade/orders/preview', {
@@ -158,6 +177,33 @@ export default function OrderConfirmPage() {
setShowPhoneVerify(true);
return;
}
const authResult = await ensureWechatAuthForPay();
if (!authResult.ok) {
if ('needBindPhone' in authResult) {
setWxSessionKey(authResult.wxSessionKey);
setShowWechatBindPhone(true);
setPendingSubmit(true);
return;
}
return;
}
setLoading(true);
setMsg('');
try {
await doSubmit();
} catch (e) {
setMsg(e instanceof Error ? e.message : '下单失败');
} finally {
setLoading(false);
}
}
async function handleWechatPhoneBound() {
await refreshProfile();
if (!pendingSubmit) return;
setPendingSubmit(false);
setLoading(true);
setMsg('');
try {
@@ -347,6 +393,19 @@ export default function OrderConfirmPage() {
}}
onSuccess={handlePhoneVerified}
/>
<PhoneVerifySheet
open={showWechatBindPhone}
mode="wechat_bind_phone"
wxSessionKey={wxSessionKey ?? undefined}
defaultPhone={selectedAddress?.phone}
onClose={() => {
setShowWechatBindPhone(false);
setWxSessionKey(null);
setPendingSubmit(false);
}}
onSuccess={handleWechatPhoneBound}
/>
</div>
);
}