|
|
@@ -29,19 +29,76 @@ interface ApiResponse {
|
|
|
msg: string;
|
|
|
}
|
|
|
|
|
|
+// API响应缓存配置
|
|
|
+const CACHE_EXPIRATION = 10 * 60 * 1000; // 10分钟
|
|
|
+
|
|
|
+// 缓存接口定义
|
|
|
+interface CacheItem {
|
|
|
+ data: ApiResponse;
|
|
|
+ timestamp: number;
|
|
|
+}
|
|
|
+
|
|
|
+// 缓存对象
|
|
|
+const cache: Record<string, CacheItem> = {};
|
|
|
+
|
|
|
+// 生成缓存键
|
|
|
+const generateCacheKey = (url: string, params: Record<string, any>): string => {
|
|
|
+ const sortedParams = Object.keys(params).sort().reduce((acc, key) => {
|
|
|
+ acc[key] = params[key];
|
|
|
+ return acc;
|
|
|
+ }, {} as Record<string, any>);
|
|
|
+ return `${url}?${JSON.stringify(sortedParams)}`;
|
|
|
+};
|
|
|
+
|
|
|
+// 获取缓存数据
|
|
|
+const getCachedData = (key: string): ApiResponse | null => {
|
|
|
+ const cachedItem = cache[key];
|
|
|
+ if (!cachedItem) return null;
|
|
|
+
|
|
|
+ const now = Date.now();
|
|
|
+ if (now - cachedItem.timestamp > CACHE_EXPIRATION) {
|
|
|
+ delete cache[key];
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return cachedItem.data;
|
|
|
+};
|
|
|
+
|
|
|
+// 设置缓存数据
|
|
|
+const setCachedData = (key: string, data: ApiResponse): void => {
|
|
|
+ cache[key] = {
|
|
|
+ data,
|
|
|
+ timestamp: Date.now()
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
// 获取热门文章列表
|
|
|
export const fetchHotArticles = async (lang: number, pageSize: number): Promise<ApiResponse> => {
|
|
|
try {
|
|
|
- const response = await axios.get<ApiResponse>(
|
|
|
- 'http://localhost:48080/ship-api/cms/article/portal/page-list',
|
|
|
- {
|
|
|
- params: {
|
|
|
- lang,
|
|
|
- 'type.remark': 'hot',
|
|
|
- pageSize
|
|
|
- }
|
|
|
- }
|
|
|
- );
|
|
|
+ const baseUrl = import.meta.env.VITE_SHIP_API_BASE_URL || 'http://localhost:48080';
|
|
|
+ const apiPath = import.meta.env.VITE_SHIP_API_PATH || '/ship-api';
|
|
|
+ const url = `${baseUrl}${apiPath}/cms/article/portal/page-list`;
|
|
|
+ const params = {
|
|
|
+ lang,
|
|
|
+ 'type.remark': 'hot',
|
|
|
+ pageSize
|
|
|
+ };
|
|
|
+
|
|
|
+ // 生成缓存键
|
|
|
+ const cacheKey = generateCacheKey(url, params);
|
|
|
+
|
|
|
+ // 检查缓存
|
|
|
+ const cachedData = getCachedData(cacheKey);
|
|
|
+ if (cachedData) {
|
|
|
+ return cachedData;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发起API请求
|
|
|
+ const response = await axios.get<ApiResponse>(url, { params });
|
|
|
+
|
|
|
+ // 缓存响应数据
|
|
|
+ setCachedData(cacheKey, response.data);
|
|
|
+
|
|
|
return response.data;
|
|
|
} catch (error) {
|
|
|
console.error('Failed to fetch hot articles:', error);
|
|
|
@@ -65,15 +122,29 @@ export const extractHeroImagesFromArticles = (articlesData: ApiResponse): string
|
|
|
// 获取视频内容
|
|
|
export const fetchVideoContent = async (lang: number): Promise<ApiResponse> => {
|
|
|
try {
|
|
|
- const response = await axios.get<ApiResponse>(
|
|
|
- 'http://localhost:48080/ship-api/cms/article/portal/page-list',
|
|
|
- {
|
|
|
- params: {
|
|
|
- lang,
|
|
|
- 'type.id': '2001571782243946498'
|
|
|
- }
|
|
|
- }
|
|
|
- );
|
|
|
+ const baseUrl = import.meta.env.VITE_SHIP_API_BASE_URL || 'http://localhost:48080';
|
|
|
+ const apiPath = import.meta.env.VITE_SHIP_API_PATH || '/ship-api';
|
|
|
+ const url = `${baseUrl}${apiPath}/cms/article/portal/page-list`;
|
|
|
+ const params = {
|
|
|
+ lang,
|
|
|
+ 'type.id': '2001571782243946498'
|
|
|
+ };
|
|
|
+
|
|
|
+ // 生成缓存键
|
|
|
+ const cacheKey = generateCacheKey(url, params);
|
|
|
+
|
|
|
+ // 检查缓存
|
|
|
+ const cachedData = getCachedData(cacheKey);
|
|
|
+ if (cachedData) {
|
|
|
+ return cachedData;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发起API请求
|
|
|
+ const response = await axios.get<ApiResponse>(url, { params });
|
|
|
+
|
|
|
+ // 缓存响应数据
|
|
|
+ setCachedData(cacheKey, response.data);
|
|
|
+
|
|
|
return response.data;
|
|
|
} catch (error) {
|
|
|
console.error('Failed to fetch video content:', error);
|