import { useEffect, useState } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import AppImage from '@dukang/shared-ui/AppImage'; import type { WechatLoginResult } from '@dukang/shared-types'; import { request, saveSession, type UserProfile } from '../lib/api'; import { normalizePhoneInput, validateMobilePhone } from '../lib/phone'; import { isWechatEnv, weixinSdk } from '../lib/weixin'; export default function LoginPage() { const navigate = useNavigate(); const [searchParams] = useSearchParams(); const returnTo = searchParams.get('return') || '/'; const [phone, setPhone] = useState('13800000001'); const [code, setCode] = useState('123456'); const [loading, setLoading] = useState(false); const [agreed, setAgreed] = useState(true); const [codeCooldown, setCodeCooldown] = useState(0); const [msg, setMsg] = useState(''); const [wxSessionKey, setWxSessionKey] = useState(null); const [bindMode, setBindMode] = useState(false); useEffect(() => { if (!isWechatEnv()) return; weixinSdk .handleOAuthCallback() .then((result) => { if (!result) return; handleWechatLoginResult(result); }) .catch((e) => setMsg(e instanceof Error ? e.message : '微信登录失败')); }, []); function handleWechatLoginResult(result: WechatLoginResult) { if (result.needBindPhone && result.wxSessionKey) { setBindMode(true); setWxSessionKey(result.wxSessionKey); setMsg('微信授权成功,请绑定手机号完成登录'); return; } if (result.accessToken) { saveSession({ accessToken: result.accessToken, refreshToken: result.refreshToken ?? '', deviceKey: result.deviceKey, phoneVerified: !!result.phoneVerified, user: result.user as never, }); navigate(returnTo.startsWith('/') ? returnTo : '/'); } } function ensureAgreed() { if (!agreed) { setMsg('请先勾选并同意用户协议'); return false; } return true; } async function sendCode() { if (!ensureAgreed()) return; const phoneCheck = validateMobilePhone(phone); if (!phoneCheck.ok) { setMsg(phoneCheck.message ?? '请输入正确的手机号码'); return; } setMsg(''); await request('USER_H5', '/auth/sms/send', { method: 'POST', body: JSON.stringify({ phone, scene: bindMode ? 'BIND_PHONE' : 'USER_LOGIN' }), }); setMsg('验证码已发送(Mock: 123456)'); setCodeCooldown(60); const timer = setInterval(() => { setCodeCooldown((c) => { if (c <= 1) { clearInterval(timer); return 0; } return c - 1; }); }, 1000); } async function login() { if (!ensureAgreed()) return; const phoneCheck = validateMobilePhone(phone); if (!phoneCheck.ok) { setMsg(phoneCheck.message ?? '请输入正确的手机号码'); return; } if (!code.trim()) { setMsg('请输入验证码'); return; } setLoading(true); setMsg(''); try { if (bindMode && wxSessionKey) { const data = await request('USER_H5', '/auth/wechat/bind-phone', { method: 'POST', body: JSON.stringify({ wxSessionKey, phone, code }), }); handleWechatLoginResult(data); return; } const data = await request<{ accessToken: string; refreshToken: string; deviceKey?: string; }>('USER_H5', '/auth/login/sms', { method: 'POST', body: JSON.stringify({ phone, code }), }); saveSession(data); navigate(returnTo.startsWith('/') ? returnTo : '/'); } catch (e) { setMsg(e instanceof Error ? e.message : '登录失败'); } finally { setLoading(false); } } async function wechatLogin() { if (!ensureAgreed()) return; setMsg(''); try { if (!isWechatEnv()) { setMsg('请在微信内打开以使用微信一键授权'); return; } const result = await weixinSdk.login(); if (result) handleWechatLoginResult(result); } catch (e) { setMsg(e instanceof Error ? e.message : '微信登录失败'); } } return (
官方

欢迎来到杜康好客

买美酒,享好礼

{bindMode ? '绑定手机号' : '手机验证码登录'}

+86 { setPhone(normalizePhoneInput(e.target.value)); setMsg(''); }} />
setCode(e.target.value)} />
{msg &&

{msg}

}
{!bindMode && ( <>
或者
)}
); }