微信授权逻辑修改

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
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import type { WechatLoginResult } from '@dukang/shared-types';
import { SmsScene } from '@dukang/shared-types';
import { bindPhone, type SessionPayload } from '../lib/api';
import { bindPhone, request, type SessionPayload } from '../lib/api';
import { normalizePhoneInput, validateMobilePhone } from '../lib/phone';
import { useSmsCode } from '../lib/use-sms-code';
import { useUserSession } from '../contexts/UserSessionContext';
@@ -9,6 +10,10 @@ type PhoneVerifySheetProps = {
open: boolean;
/** 打开时预填手机号(如收货地址中的手机号) */
defaultPhone?: string;
mode?: 'bind_phone' | 'wechat_bind_phone';
wxSessionKey?: string;
title?: string;
description?: string;
onClose: () => void;
onSuccess: () => void;
};
@@ -16,6 +21,10 @@ type PhoneVerifySheetProps = {
export default function PhoneVerifySheet({
open,
defaultPhone,
mode = 'bind_phone',
wxSessionKey,
title,
description,
onClose,
onSuccess,
}: PhoneVerifySheetProps) {
@@ -59,8 +68,28 @@ export default function PhoneVerifySheet({
setLoading(true);
setError('');
try {
const session = await bindPhone(phone, code);
applySession(session as SessionPayload);
if (mode === 'wechat_bind_phone') {
if (!wxSessionKey) {
setError('微信会话已过期,请重新授权');
return;
}
const data = await request<WechatLoginResult>('USER_H5', '/auth/wechat/bind-phone', {
method: 'POST',
body: JSON.stringify({ wxSessionKey, phone, code }),
});
if (data.accessToken) {
applySession({
accessToken: data.accessToken,
refreshToken: data.refreshToken ?? '',
deviceKey: data.deviceKey,
phoneVerified: !!data.phoneVerified,
user: data.user as SessionPayload['user'],
});
}
} else {
const session = await bindPhone(phone, code);
applySession(session as SessionPayload);
}
onSuccess();
onClose();
} catch (e) {
@@ -72,12 +101,19 @@ export default function PhoneVerifySheet({
if (!open) return null;
const sheetTitle = title ?? (mode === 'wechat_bind_phone' ? '绑定手机号' : '验证手机号');
const sheetDesc =
description ??
(mode === 'wechat_bind_phone'
? '微信授权成功,请绑定手机号以完成支付'
: '下单前需验证手机号,以便接收订单通知');
return (
<div className="phone-verify-overlay" role="dialog" aria-modal="true">
<button type="button" className="phone-verify-backdrop" aria-label="关闭" onClick={onClose} />
<div className="phone-verify-sheet">
<h3 className="phone-verify-title"></h3>
<p className="phone-verify-desc">便</p>
<h3 className="phone-verify-title">{sheetTitle}</h3>
<p className="phone-verify-desc">{sheetDesc}</p>
<div className="login-field">
<span className="login-field-prefix">+86</span>
<input
@@ -121,7 +157,7 @@ export default function PhoneVerifySheet({
<p className={`login-msg${sentHint && !error ? ' login-msg--hint' : ''}`}>{error || sentHint}</p>
)}
<button type="button" className="login-sms-btn" disabled={loading} onClick={submit}>
{loading ? '验证中...' : '确认验证'}
{loading ? '验证中...' : mode === 'wechat_bind_phone' ? '确认绑定' : '确认验证'}
</button>
</div>
</div>