import { useCallback, useEffect, useState } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { isWxAuthorizeEnabled } from '@dukang/shared-types'; import PullToRefresh from '@dukang/shared-ui/PullToRefresh'; import { stripOAuthParamsFromLocation } from '@dukang/weixin-sdk'; import { useStoreSession } from '../contexts/StoreSessionContext'; import { getStoreProfile, request } from '../lib/api'; import { authorizeShopWechat, fetchClientConfig, fetchShopAccount, handleShopWechatCallback, handleShopWechatLoginResult, } from '../lib/wechat-auth'; import { isWechatEnv } from '../lib/weixin'; import { useStorePageView } from '../lib/usePageView'; export default function MinePage() { useStorePageView('store_mine_view'); const navigate = useNavigate(); const [searchParams, setSearchParams] = useSearchParams(); const { resetSession, store: sessionStore, applySession } = useStoreSession(); const profile = sessionStore ?? getStoreProfile(); const [store, setStore] = useState | null>(null); const [hasWechat, setHasWechat] = useState(null); const [wxAuthorize, setWxAuthorize] = useState(false); const [binding, setBinding] = useState(false); const [bindMsg, setBindMsg] = useState(''); const loadMine = useCallback(() => { return Promise.all([ request('SHOP_H5', '/shop/store').then(setStore).catch(() => setStore(null)), fetchClientConfig() .then((config) => setWxAuthorize(isWxAuthorizeEnabled(config))) .catch(() => setWxAuthorize(false)), fetchShopAccount() .then((me) => setHasWechat(!!(me.hasWechat || me.wxOpenId))) .catch(() => setHasWechat(null)), ]); }, []); useEffect(() => { void loadMine(); }, [loadMine]); useEffect(() => { if (!isWechatEnv() || !wxAuthorize || !searchParams.get('code')) return; void handleShopWechatCallback() .then((result) => { if (!result) return; const session = handleShopWechatLoginResult(result); if (session) { applySession(session); setHasWechat(true); setBindMsg('微信绑定成功'); } stripOAuthParamsFromLocation(); setSearchParams({}, { replace: true }); }) .catch((e) => setBindMsg(e instanceof Error ? e.message : '微信绑定失败')); }, [applySession, searchParams, setSearchParams, wxAuthorize]); const hoursParts: string[] = []; if (store?.openTime && store?.closeTime) hoursParts.push(`${store.openTime} - ${store.closeTime}`); if (store?.openTime2 && store?.closeTime2) hoursParts.push(`${store.openTime2} - ${store.closeTime2}`); const hoursText = hoursParts.length ? hoursParts.join(',') : '09:30 - 22:00'; const multiStore = (profile?.stores?.length ?? 0) > 1; const showBindWechat = wxAuthorize && hasWechat === false; async function bindWechat() { if (binding) return; setBindMsg(''); if (!isWechatEnv()) { setBindMsg('请在微信内打开门店端以绑定微信'); return; } setBinding(true); try { const result = await authorizeShopWechat(); if (!result) { // 跳转公众号 OAuth,回跳后由上面 useEffect 处理 return; } const session = handleShopWechatLoginResult(result); if (session) { applySession(session); setHasWechat(true); setBindMsg('微信绑定成功'); } else { setBindMsg('绑定未完成,请重试'); } } catch (e) { setBindMsg(e instanceof Error ? e.message : '微信绑定失败'); } finally { setBinding(false); } } return (

我的

门店信息

门店名称

{String(store?.name || profile?.storeName || '—')}

lock

地理位置

{String(store?.address || '—')}

lock

联系电话

{String(store?.phone || profile?.phone || '—')}

lock

营业时间

{hoursText}

lock
{multiStore ? ( ) : null} {profile?.isPrimary ? ( ) : null} {profile?.isPrimary ? ( ) : null}
help

如需修改信息请联系城市合伙人

{bindMsg ?

{bindMsg}

: null} {showBindWechat ? ( ) : null}
); }