forked from gxwebsoft/mp-10550
feat(config): 新增网站配置字段和自定义Hook
- 在cmsWebsiteField模型中新增deliveryText、guaranteeText和openComments三个字段 - 创建useConfig自定义Hook用于获取和管理网站配置数据 - 更新Banner组件中的默认轮播图高度从200px调整为300px- 修改Banner组件中热门商品列表项的右边距样式 - 在ArticleList组件中为map函数添加类型注解 - 移除IsDealer组件中旧的配置获取逻辑,改用新的useConfig Hook - 删除src/pages/user_bak目录下的所有旧版用户相关组件文件 - 删除src/pages/user_bak目录下的配置文件和样式文件
This commit is contained in:
50
src/hooks/useConfig.ts
Normal file
50
src/hooks/useConfig.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import Taro from '@tarojs/taro';
|
||||
import { configWebsiteField } from '@/api/cms/cmsWebsiteField';
|
||||
import { Config } from '@/api/cms/cmsWebsiteField/model';
|
||||
|
||||
/**
|
||||
* 自定义Hook用于获取和管理网站配置数据
|
||||
* @returns {Object} 包含配置数据和加载状态的对象
|
||||
*/
|
||||
export const useConfig = () => {
|
||||
const [config, setConfig] = useState<Config | null>(null);
|
||||
const [loading, setLoading] = useState<boolean>(true);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchConfig = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await configWebsiteField();
|
||||
setConfig(data);
|
||||
Taro.setStorageSync('config', data);
|
||||
|
||||
// 设置主题
|
||||
if (data.theme && !Taro.getStorageSync('user_theme')) {
|
||||
Taro.setStorageSync('user_theme', data.theme);
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err : new Error('获取配置失败'));
|
||||
console.error('获取网站配置失败:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchConfig();
|
||||
}, []);
|
||||
|
||||
return { config, loading, error, refetch: () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
configWebsiteField().then(data => {
|
||||
setConfig(data);
|
||||
Taro.setStorageSync('config', data);
|
||||
setLoading(false);
|
||||
}).catch(err => {
|
||||
setError(err instanceof Error ? err : new Error('获取配置失败'));
|
||||
setLoading(false);
|
||||
});
|
||||
}};
|
||||
};
|
||||
Reference in New Issue
Block a user