# 🏪 useShopInfo Hook 使用指南 ## 📋 概述 `useShopInfo` 是一个用于管理商店信息的React Hook,提供了商店信息的获取、缓存和管理功能。它基于`getShopInfo()`接口,为全站提供统一的商店信息访问方式。 ## ✨ 特性 - 🚀 **自动缓存**:30分钟本地缓存,减少网络请求 - 🔄 **智能刷新**:支持强制刷新和自动过期更新 - 📱 **离线支持**:网络失败时使用缓存数据 - 🛠️ **工具方法**:提供常用信息的便捷获取方法 - 🎯 **TypeScript**:完整的类型支持 - ⚡ **性能优化**:使用useCallback避免不必要的重渲染 ## 🔧 基本用法 ### 1. 导入Hook ```typescript import { useShopInfo } from '@/hooks/useShopInfo'; ``` ### 2. 在组件中使用 ```typescript const MyComponent = () => { const { shopInfo, loading, error, getWebsiteName, getWebsiteLogo } = useShopInfo(); if (loading) { return
加载中...
; } if (error) { return
加载失败: {error}
; } return (
Logo

{getWebsiteName()}

); }; ``` ## 📊 API 参考 ### 状态属性 | 属性 | 类型 | 说明 | |------|------|------| | `shopInfo` | `CmsWebsite \| null` | 商店信息对象 | | `loading` | `boolean` | 是否正在加载 | | `error` | `string \| null` | 错误信息 | ### 方法 | 方法 | 参数 | 返回值 | 说明 | |------|------|--------|------| | `fetchShopInfo` | `forceRefresh?: boolean` | `Promise` | 获取商店信息 | | `refreshShopInfo` | - | `Promise` | 强制刷新商店信息 | | `clearCache` | - | `void` | 清除本地缓存 | ### 工具方法 | 方法 | 返回值 | 说明 | |------|--------|------| | `getWebsiteName()` | `string` | 获取网站名称,默认"商城" | | `getWebsiteLogo()` | `string` | 获取网站Logo URL | | `getDarkLogo()` | `string` | 获取深色模式Logo URL | | `getDomain()` | `string` | 获取网站域名 | | `getPhone()` | `string` | 获取联系电话 | | `getEmail()` | `string` | 获取邮箱地址 | | `getAddress()` | `string` | 获取地址 | | `getIcpNo()` | `string` | 获取ICP备案号 | | `getStatus()` | `object` | 获取网站状态信息 | | `getConfig()` | `any` | 获取网站配置 | | `getNavigation()` | `object` | 获取导航菜单 | | `isSearchEnabled()` | `boolean` | 是否支持搜索 | | `getVersionInfo()` | `object` | 获取版本信息 | ## 🎯 使用场景 ### 1. 页面头部组件 ```typescript // src/pages/index/Header.tsx import { useShopInfo } from '@/hooks/useShopInfo'; const Header = () => { const { getWebsiteName, getWebsiteLogo, loading } = useShopInfo(); return ( {getWebsiteName()} } /> ); }; ``` ### 2. 首页组件 ```typescript // src/pages/index/index.tsx import { useShopInfo } from '@/hooks/useShopInfo'; const Home = () => { const { shopInfo, loading, error } = useShopInfo(); useEffect(() => { if (shopInfo) { // 设置页面标题 Taro.setNavigationBarTitle({ title: shopInfo.websiteName || '商城' }); } }, [shopInfo]); // 分享配置 useShareAppMessage(() => ({ title: shopInfo?.websiteName || '精选商城', imageUrl: shopInfo?.websiteLogo })); return (
{/* 页面内容 */}
); }; ``` ### 3. 商品详情页分享 ```typescript // src/shop/goodsDetail/index.tsx import { useShopInfo } from '@/hooks/useShopInfo'; const GoodsDetail = () => { const { getWebsiteName, getWebsiteLogo } = useShopInfo(); useShareAppMessage(() => ({ title: `${goods?.name} - ${getWebsiteName()}`, path: `/shop/goodsDetail/index?id=${goodsId}`, imageUrl: goods?.image || getWebsiteLogo() })); return (
{/* 商品详情 */}
); }; ``` ### 4. 联系我们页面 ```typescript const ContactPage = () => { const { getPhone, getEmail, getAddress, getIcpNo } = useShopInfo(); return (
电话: {getPhone()}
邮箱: {getEmail()}
地址: {getAddress()}
备案号: {getIcpNo()}
); }; ``` ### 5. 网站状态检查 ```typescript const StatusChecker = () => { const { getStatus, getVersionInfo } = useShopInfo(); const status = getStatus(); const version = getVersionInfo(); return (
运行状态: {status.running ? '正常' : '维护中'}
版本: {version.version === 10 ? '免费版' : version.version === 20 ? '专业版' : '永久授权'}
{version.expirationTime && (
到期时间: {version.expirationTime}
)}
); }; ``` ## 🔄 缓存机制 ### 缓存策略 - **缓存时间**:30分钟 - **存储位置**:微信小程序本地存储 - **缓存键名**:`shop_info` 和 `shop_info_cache_time` ### 缓存行为 1. **首次加载**:从服务器获取数据并缓存 2. **后续加载**:优先使用缓存,缓存过期时自动刷新 3. **网络失败**:使用缓存数据(即使过期) 4. **强制刷新**:忽略缓存,直接从服务器获取 ### 手动管理缓存 ```typescript const { refreshShopInfo, clearCache } = useShopInfo(); // 强制刷新 const handleRefresh = async () => { await refreshShopInfo(); }; // 清除缓存 const handleClearCache = () => { clearCache(); }; ``` ## 🚀 性能优化 ### 1. 避免重复请求 ```typescript // ✅ 推荐:多个组件使用同一个Hook实例 const App = () => { const shopInfo = useShopInfo(); return (