import { useEffect, useRef, useState, type RefObject } from 'react'; import { Link, useNavigate, useSearchParams } from 'react-router-dom'; import AppImage from '@dukang/shared-ui/AppImage'; import { isWxAuthorizeEnabled } from '@dukang/shared-types'; import { stripOAuthParamsFromLocation } from '@dukang/weixin-sdk'; import { useStoreSession } from '../contexts/StoreSessionContext'; import { getLastPhone, getStoreProfile, hasShopWxSession, request, saveAuth, type ShopSessionPayload } from '../lib/api'; import { routeAfterShopLogin } from './SelectStorePage'; import { bindShopWechatAfterSmsLogin, fetchClientConfig, handleShopWechatCallback, handleShopWechatLoginResult, loginShopWithWechat, } from '../lib/wechat-auth'; import { isWechatEnv } from '../lib/weixin'; function maskPhone(phone: string) { if (phone.length < 7) return phone; return `${phone.slice(0, 3)} **** ${phone.slice(-4)}`; } function formatWechatError(e: unknown): string { const text = e instanceof Error ? e.message : '微信登录失败'; if (text.includes('首次登录') || text.includes('手机验证码')) { return '该微信尚未绑定门店账号,请先使用手机验证码登录,登录后将自动关联微信'; } return text; } function ShopAgreementCheckbox({ agreed, onChange, labelRef, }: { agreed: boolean; onChange: (next: boolean) => void; labelRef?: RefObject; }) { return ( ); } export default function LoginPage() { const navigate = useNavigate(); const { applySession } = useStoreSession(); const [params, setSearchParams] = useSearchParams(); const quick = params.get('quick') === '1'; const savedProfile = getStoreProfile(); const [phone, setPhone] = useState(getLastPhone()); const [code, setCode] = useState(''); const [agreed, setAgreed] = useState(false); const [loading, setLoading] = useState(false); const [wxLoading, setWxLoading] = useState(false); const [msg, setMsg] = useState(''); const [codeCooldown, setCodeCooldown] = useState(0); const [wxAuthorize, setWxAuthorize] = useState(false); const agreementRef = useRef(null); useEffect(() => { fetchClientConfig() .then((config) => setWxAuthorize(isWxAuthorizeEnabled(config))) .catch(() => setWxAuthorize(false)); }, []); useEffect(() => { if (!isWechatEnv() || !wxAuthorize || !params.get('code')) return; void handleShopWechatCallback() .then((result) => { if (!result) return; const session = handleShopWechatLoginResult(result); if (session) { applySession(session); stripOAuthParamsFromLocation(); setSearchParams({}, { replace: true }); routeAfterShopLogin(session, navigate); } }) .catch((e) => setMsg(formatWechatError(e))); }, [applySession, navigate, params, setSearchParams, wxAuthorize]); const quickStoreName = savedProfile?.storeName ?? '门店管理中心'; const quickPhone = savedProfile?.phone || phone; function ensureAgreed() { if (!agreed) { setMsg('请先阅读并同意用户协议'); agreementRef.current?.scrollIntoView({ behavior: 'smooth', block: 'center' }); return false; } return true; } async function sendCode() { if (!ensureAgreed()) return; setMsg(''); try { await request('SHOP_H5', '/shop/auth/sms/send', { method: 'POST', body: JSON.stringify({ phone, scene: 'STORE_LOGIN' }), }); setMsg('验证码已发送'); setCodeCooldown(60); const timer = setInterval(() => { setCodeCooldown((c) => { if (c <= 1) { clearInterval(timer); return 0; } return c - 1; }); }, 1000); } catch (e) { setMsg(e instanceof Error ? e.message : '发送失败'); } } async function login() { if (!ensureAgreed()) return; setLoading(true); setMsg(''); try { const data = await request('SHOP_H5', '/shop/auth/login/sms', { method: 'POST', body: JSON.stringify({ phone, code }), }); saveAuth(data); applySession(data); if (isWechatEnv() && wxAuthorize) { setMsg('登录成功,正在关联微信…'); await bindShopWechatAfterSmsLogin(); return; } routeAfterShopLogin(data, navigate); } catch (e) { setMsg(e instanceof Error ? e.message : '登录失败'); } finally { setLoading(false); } } async function wechatLogin() { if (!ensureAgreed()) return; setMsg(''); if (!isWechatEnv()) { setMsg('请在微信内打开以使用微信一键登录'); return; } setWxLoading(true); try { const session = await loginShopWithWechat(); if (session) { applySession(session); routeAfterShopLogin(session, navigate); } } catch (e) { setMsg(formatWechatError(e)); } finally { setWxLoading(false); } } if (quick) { const canWechatQuick = wxAuthorize && isWechatEnv() && hasShopWxSession() && !!savedProfile; return (

欢迎回来

storefront

{quickStoreName}

{maskPhone(quickPhone)}

verified_user 认证门店
sync 切换账号
{msg &&

{msg}

} {canWechatQuick ? ( ) : (

{wxAuthorize && !isWechatEnv() ? '请在微信内打开以使用一键登录' : '请使用验证码登录并绑定微信后,即可 7 天内免登录'}

)} {!canWechatQuick && ( 验证码登录 )}
lock {canWechatQuick ? '微信验证 · 7 天内免登录' : '加密环境安全登录中'}

SECURED BY DUKANG HERITAGE

© 2024 杜康酒业门店管理系统

); } return (

杜康好客

门店管理系统

phone_iphone setPhone(e.target.value)} />
shield setCode(e.target.value)} />
{msg &&

{msg}

} {wxAuthorize && ( <>
或者
)}
); }