import React, {useCallback, useState} from 'react' import {View, Text} from '@tarojs/components' import {Avatar, Button, ConfigProvider, Grid} from '@nutui/nutui-react-taro' import {Location, Scan, Shop, Shopping, User} from '@nutui/icons-react-taro' import Taro, {useDidShow} from '@tarojs/taro' import {useThemeStyles} from '@/hooks/useTheme' import {useUser} from '@/hooks/useUser' import {getSelectedStoreFromStorage} from '@/utils/storeSelection' import {listShopStoreUser} from '@/api/shop/shopStoreUser' import {getShopStore} from '@/api/shop/shopStore' import type {ShopStore as ShopStoreModel} from '@/api/shop/shopStore/model' import { goToRegister } from '@/utils/auth' const StoreIndex: React.FC = () => { const themeStyles = useThemeStyles() const {isLoggedIn, loading: userLoading, getAvatarUrl, getDisplayName, getRoleName, hasRole} = useUser() const [boundStoreId, setBoundStoreId] = useState(undefined) const [selectedStore, setSelectedStore] = useState(getSelectedStoreFromStorage()) const [store, setStore] = useState(selectedStore) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const storeId = boundStoreId || selectedStore?.id const parseStoreCoords = (s: ShopStoreModel): {lng: number; lat: number} | null => { const raw = (s.lngAndLat || s.location || '').trim() if (!raw) return null const parts = raw.split(/[,\s]+/).filter(Boolean) if (parts.length < 2) return null const a = parseFloat(parts[0]) const b = parseFloat(parts[1]) if (Number.isNaN(a) || Number.isNaN(b)) return null // 常见格式是 "lng,lat";这里做一个简单兜底 const looksLikeLngLat = Math.abs(a) <= 180 && Math.abs(b) <= 90 const looksLikeLatLng = Math.abs(a) <= 90 && Math.abs(b) <= 180 if (looksLikeLngLat) return {lng: a, lat: b} if (looksLikeLatLng) return {lng: b, lat: a} return null } const navigateToPage = (url: string) => { if (!isLoggedIn) { goToRegister({ redirect: '/store/index' }) return } Taro.navigateTo({url}) } const refresh = useCallback(async () => { setError(null) setLoading(true) try { const latestSelectedStore = getSelectedStoreFromStorage() setSelectedStore(latestSelectedStore) const userIdRaw = Number(Taro.getStorageSync('UserId')) const userId = Number.isFinite(userIdRaw) && userIdRaw > 0 ? userIdRaw : undefined let foundStoreId: number | undefined = undefined if (userId) { // 优先按“店员绑定关系”确定门店归属 try { const list = await listShopStoreUser({userId}) const first = (list || []).find(i => i?.isDelete !== 1 && i?.storeId) foundStoreId = first?.storeId setBoundStoreId(foundStoreId) } catch { // fallback to SelectedStore foundStoreId = undefined setBoundStoreId(undefined) } } else { foundStoreId = undefined setBoundStoreId(undefined) } const nextStoreId = (foundStoreId || latestSelectedStore?.id) if (!nextStoreId) { setStore(latestSelectedStore) return } // 获取门店详情(用于展示门店名称/地址/仓库等) const full = await getShopStore(nextStoreId) setStore(full || (latestSelectedStore?.id === nextStoreId ? latestSelectedStore : ({id: nextStoreId} as ShopStoreModel))) } catch (e: any) { const msg = e?.message || '获取门店信息失败' setError(msg) } finally { setLoading(false) } }, []) // 返回/切换到该页面时,同步最新的已选门店与绑定门店 useDidShow(() => { refresh().catch(() => {}) }) const openStoreLocation = () => { if (!store?.id) { return Taro.showToast({title: '请先选择门店', icon: 'none'}) } const coords = parseStoreCoords(store) if (!coords) { return Taro.showToast({title: '门店未配置定位', icon: 'none'}) } Taro.openLocation({ latitude: coords.lat, longitude: coords.lng, name: store.name || '门店', address: store.address || '' }) } if (!isLoggedIn && !userLoading) { return ( 请先登录后再进入门店中心 ) } return ( {/* 头部信息 */} } className="mr-4" style={{border: '2px solid rgba(255, 255, 255, 0.3)'}} /> {getDisplayName()} {hasRole('store') ? '门店' : hasRole('rider') ? '配送员' : getRoleName()} {/* 门店信息 */} 当前门店 Taro.switchTab({url: '/pages/index/index'})} > 切换门店 {!storeId ? ( 未选择门店,请先去首页选择门店。 ) : ( {store?.name || `门店ID: ${storeId}`} {!!store?.address && ( {store.address} )} {!!store?.warehouseName && ( 默认仓库:{store.warehouseName} )} {!!error && ( {error} )} )} {/* 功能入口 */} 门店工具 navigateToPage('/store/orders/index')}> navigateToPage('/user/store/verification')}> Taro.switchTab({url: '/pages/index/index'})}> ) } export default StoreIndex