import { useCallback, useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import PullToRefresh from '@dukang/shared-ui/PullToRefresh'; import { useStoreSession } from '../contexts/StoreSessionContext'; import { needsStoreSelection, request, selectStore, type ShopSessionPayload, type ShopStoreOption, } from '../lib/api'; export default function SelectStorePage() { const navigate = useNavigate(); const { applySession, store, authenticated } = useStoreSession(); const [stores, setStores] = useState(store?.stores ?? []); const [loadingId, setLoadingId] = useState(null); const [msg, setMsg] = useState(''); const currentStoreId = store?.storeId || ''; const canGoBack = Boolean(currentStoreId); const loadStores = useCallback(() => { return request('SHOP_H5', '/shop/auth/stores') .then((list) => setStores(list)) .catch((e) => setMsg(e instanceof Error ? e.message : '加载门店失败')); }, []); useEffect(() => { if (!authenticated) { navigate('/login', { replace: true }); return; } void loadStores(); }, [authenticated, navigate, loadStores]); async function onSelect(storeId: string) { if (loadingId) return; if (storeId === currentStoreId) { navigate('/', { replace: true }); return; } setLoadingId(storeId); setMsg(''); try { const session = await selectStore(storeId); applySession(session); navigate('/', { replace: true }); } catch (e) { setMsg(e instanceof Error ? e.message : '选店失败'); } finally { setLoadingId(null); } } // 仅一家店时自动选 useEffect(() => { if (stores.length === 1 && needsStoreSelection({ store, stores })) { void onSelect(stores[0].storeId); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [stores.length]); function statusLabel(status: string) { if (status === 'OPEN') return '营业中'; if (status === 'PAUSED') return '临时闭店'; if (status === 'CLOSED') return '永久关闭'; return status || '门店'; } return (
{canGoBack ? ( ) : ( )}

切换门店

store

选择要进入的门店

该账号绑定了 {stores.length || '多'} 家门店,进入后可直接核销与查看营业数据

{msg ? (

{msg}

) : null}

我的门店

    {stores.map((item) => { const active = item.storeId === currentStoreId; const busy = loadingId === item.storeId; return (
  • ); })}
{!stores.length && !msg ? (
store

暂无绑定门店

) : null}
); } /** After login/wechat: route to select-store or home */ export function routeAfterShopLogin( session: ShopSessionPayload, navigate: (path: string, opts?: { replace?: boolean }) => void, ) { if (needsStoreSelection(session)) { navigate('/select-store', { replace: true }); return; } navigate('/', { replace: true }); }