Bladeren bron

♻️ refactor(stock-chart): 重构股票数据查询逻辑

- 更新股票数据响应类型定义为StockHistoryResponse
- 修改股票数据查询接口调用方式,使用history/:code端点
- 添加股票代码非空验证和接口响应错误处理
- 简化股票数据格式转换逻辑,直接使用接口返回数据
- 调整查询启用条件为code存在时自动触发
yourname 5 maanden geleden
bovenliggende
commit
71d6da5e93
1 gewijzigde bestanden met toevoegingen van 13 en 19 verwijderingen
  1. 13 19
      src/client/mobile/components/stock/components/stock-chart/src/hooks/useStockQueries.ts

+ 13 - 19
src/client/mobile/components/stock/components/stock-chart/src/hooks/useStockQueries.ts

@@ -7,7 +7,7 @@ import { useEffect } from 'react';
 import type { InferResponseType } from 'hono/client';
 
 // 定义响应类型
-type StockDataListResponse = InferResponseType<typeof stockDataClient.$get, 200>;
+type StockHistoryResponse = InferResponseType<typeof stockDataClient.history[':code']['$get'], 200>;
 type DateNotesListResponse = InferResponseType<typeof dateNotesClient.$get, 200>;
 
 export function useStockQueries(code?: string) {
@@ -17,16 +17,19 @@ export function useStockQueries(code?: string) {
     isLoading: isLoadingStock,
     error: stockError,
     refetch: refetchStock
-  } = useQuery<StockDataListResponse>({
+  } = useQuery<StockHistoryResponse>({
     queryKey: ['stockHistory', code],
-    queryFn: () => stockDataClient.$get({
-      query: {
-        filters: JSON.stringify(code ? { code } : {}),
-        page: 1,
-        pageSize: 1000
+    queryFn: async () => {
+      if (!code) throw new Error('股票代码不能为空');
+      const response = await stockDataClient.history[':code'].$get({
+        param: { code }
+      });
+      if (!response.ok) {
+        throw new Error('获取股票历史数据失败');
       }
-    }).then(res => res.json()),
-    enabled: false,
+      return response.json();
+    },
+    enabled: !!code,
     retry: 0,
   });
 
@@ -49,16 +52,7 @@ export function useStockQueries(code?: string) {
   });
 
   // 转换数据格式
-  const stockData = stockDataResponse?.data?.map(item => ({
-    o: String(item.data.o),
-    c: String(item.data.c),
-    h: String(item.data.h),
-    l: String(item.data.l),
-    v: String(item.data.v),
-    d: String(item.data.d),
-    zd: String(item.data.zd),
-    pc: item.data.pc ? String(item.data.pc) : undefined,
-  })) as StockData[] || [];
+  const stockData = stockDataResponse?.data || [] as StockData[];
 
   const memoData = memoDataResponse?.data?.map(item => ({
     _id: item.id,