Quellcode durchsuchen

我的股票刷新时间修改

Zhangbw vor 3 Monaten
Ursprung
Commit
3055546088
4 geänderte Dateien mit 142 neuen und 31 gelöschten Zeilen
  1. 62 10
      dist/dev/mp-weixin/pages/rank/rank.js
  2. 1 1
      dist/dev/mp-weixin/utils/api.js
  3. 77 15
      src/pages/rank/rank.vue
  4. 2 5
      src/utils/api.js

+ 62 - 10
dist/dev/mp-weixin/pages/rank/rank.js

@@ -12,6 +12,7 @@ const _sfc_main = {
     const lastLoadTime = common_vendor.ref(0);
     const CACHE_DURATION = 5e3;
     const isPageVisible = common_vendor.ref(false);
+    const getRandomInterval = () => 2e3 + Math.random() * 1e3;
     const SLIDE_WIDTH = 100;
     const slideStates = common_vendor.reactive({});
     let slideTimers = {};
@@ -75,7 +76,8 @@ const _sfc_main = {
       priceChange: null,
       changePercent: null
     });
-    let refreshTimer = null;
+    let priceRefreshTimer = null;
+    let trendRefreshTimer = null;
     const fetchIndexData = async () => {
       try {
         const res = await utils_api.getIndexQuote("000001");
@@ -338,6 +340,36 @@ const _sfc_main = {
         isLoading.value = false;
       }
     };
+    const refreshPriceOnly = async () => {
+      if (myStocks.value.length === 0)
+        return;
+      try {
+        const codes = myStocks.value.map((stock) => stock.code).join(",");
+        const quoteRes = await utils_api.getStockQuotes(codes);
+        if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
+          quoteRes.data.forEach((quoteData) => {
+            const index = myStocks.value.findIndex((stock) => stock.code === quoteData.stockCode);
+            if (index !== -1) {
+              const stock = myStocks.value[index];
+              stock.priceChange = quoteData.priceChange;
+              stock.changePercent = quoteData.changePercent;
+              stock.currentPrice = quoteData.currentPrice;
+              stock.name = quoteData.stockName || stock.name;
+              if (stock.addPrice && quoteData.currentPrice) {
+                const addPrice = parseFloat(stock.addPrice);
+                const currentPrice = parseFloat(quoteData.currentPrice);
+                if (addPrice > 0) {
+                  const profit = ((currentPrice - addPrice) / addPrice * 100).toFixed(2);
+                  stock.profitPercent = profit >= 0 ? `+${profit}%` : `${profit}%`;
+                }
+              }
+            }
+          });
+        }
+      } catch (e) {
+        console.error("[我的股票] 价格刷新异常:", e.message);
+      }
+    };
     const refreshAllQuotes = async () => {
       if (myStocks.value.length === 0)
         return;
@@ -373,31 +405,51 @@ const _sfc_main = {
       if (!isLoggedIn.value || !isPageVisible.value)
         return;
       stopAutoRefresh();
-      const scheduleNextRefresh = () => {
+      const schedulePriceRefresh = () => {
         if (!isPageVisible.value) {
           stopAutoRefresh();
           return;
         }
-        const delay = 1e4;
-        refreshTimer = setTimeout(async () => {
+        priceRefreshTimer = setTimeout(async () => {
           if (!isPageVisible.value) {
             stopAutoRefresh();
             return;
           }
           await fetchIndexData();
+          if (myStocks.value.length > 0) {
+            await refreshPriceOnly();
+          }
+          schedulePriceRefresh();
+        }, getRandomInterval());
+      };
+      const scheduleTrendRefresh = () => {
+        if (!isPageVisible.value) {
+          stopAutoRefresh();
+          return;
+        }
+        trendRefreshTimer = setTimeout(async () => {
+          if (!isPageVisible.value) {
+            stopAutoRefresh();
+            return;
+          }
           if (myStocks.value.length > 0) {
             await refreshAllQuotes();
             drawAllTrendCharts();
           }
-          scheduleNextRefresh();
-        }, delay);
+          scheduleTrendRefresh();
+        }, 1e4);
       };
-      scheduleNextRefresh();
+      schedulePriceRefresh();
+      scheduleTrendRefresh();
     };
     const stopAutoRefresh = () => {
-      if (refreshTimer) {
-        clearTimeout(refreshTimer);
-        refreshTimer = null;
+      if (priceRefreshTimer) {
+        clearTimeout(priceRefreshTimer);
+        priceRefreshTimer = null;
+      }
+      if (trendRefreshTimer) {
+        clearTimeout(trendRefreshTimer);
+        trendRefreshTimer = null;
       }
     };
     const goToLogin = () => {

+ 1 - 1
dist/dev/mp-weixin/utils/api.js

@@ -3,7 +3,7 @@ const common_vendor = require("../common/vendor.js");
 const ENV = "dev";
 const CONFIG = {
   dev: "http://localhost:8081",
-  // 开发环境(开发者工具)
+  // 开发环境
   prod: "http://192.168.1.2:8081"
   // 生产环境
 };

+ 77 - 15
src/pages/rank/rank.vue

@@ -177,6 +177,9 @@ const lastLoadTime = ref(0) // 上次加载时间
 const CACHE_DURATION = 5000 // 缓存有效期5秒
 const isPageVisible = ref(false) // 页面是否可见
 
+// 获取随机刷新间隔 (2000-3000ms)
+const getRandomInterval = () => 2000 + Math.random() * 1000
+
 // 滑动删除相关
 const SLIDE_WIDTH = 100 // 滑动距离(三分之一宽度约100rpx转换)
 const slideStates = reactive({}) // 每个股票的滑动状态
@@ -264,7 +267,8 @@ const indexData = ref({
   priceChange: null,
   changePercent: null
 })
-let refreshTimer = null
+let priceRefreshTimer = null  // 价格刷新定时器 (2-3秒)
+let trendRefreshTimer = null  // 趋势图刷新定时器 (10秒)
 
 // 获取上证指数数据
 const fetchIndexData = async () => {
@@ -600,7 +604,43 @@ const loadMyStocks = async (forceRefresh = false) => {
   }
 }
 
-// 批量刷新所有股票行情
+// 批量刷新所有股票行情(只更新价格,不更新趋势图)
+const refreshPriceOnly = async () => {
+  if (myStocks.value.length === 0) return
+  
+  try {
+    const codes = myStocks.value.map(stock => stock.code).join(',')
+    const quoteRes = await getStockQuotes(codes)
+    
+    if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
+      quoteRes.data.forEach(quoteData => {
+        const index = myStocks.value.findIndex(stock => stock.code === quoteData.stockCode)
+        if (index !== -1) {
+          const stock = myStocks.value[index]
+          stock.priceChange = quoteData.priceChange
+          stock.changePercent = quoteData.changePercent
+          stock.currentPrice = quoteData.currentPrice
+          stock.name = quoteData.stockName || stock.name
+          // 不更新 trendData
+          
+          // 计算自选收益(当前价格相对于加入价格的涨跌幅)
+          if (stock.addPrice && quoteData.currentPrice) {
+            const addPrice = parseFloat(stock.addPrice)
+            const currentPrice = parseFloat(quoteData.currentPrice)
+            if (addPrice > 0) {
+              const profit = ((currentPrice - addPrice) / addPrice * 100).toFixed(2)
+              stock.profitPercent = profit >= 0 ? `+${profit}%` : `${profit}%`
+            }
+          }
+        }
+      })
+    }
+  } catch (e) {
+    console.error('[我的股票] 价格刷新异常:', e.message)
+  }
+}
+
+// 批量刷新所有股票行情(包含趋势图)
 const refreshAllQuotes = async () => {
   if (myStocks.value.length === 0) return
   
@@ -636,44 +676,66 @@ const refreshAllQuotes = async () => {
   }
 }
 
-// 启动定时刷新
+// 启动定时刷新(价格2-3秒,趋势图10秒)
 const startAutoRefresh = () => {
   if (!isLoggedIn.value || !isPageVisible.value) return
   stopAutoRefresh()
   
-  const scheduleNextRefresh = () => {
-    // 每次刷新前检查页面是否可见
+  // 价格刷新定时器 (2-3秒随机间隔)
+  const schedulePriceRefresh = () => {
     if (!isPageVisible.value) {
       stopAutoRefresh()
       return
     }
     
-    const delay = 10000 // 10秒刷新一次
-    refreshTimer = setTimeout(async () => {
-      // 再次检查页面是否可见
+    priceRefreshTimer = setTimeout(async () => {
       if (!isPageVisible.value) {
         stopAutoRefresh()
         return
       }
       
       await fetchIndexData()
+      if (myStocks.value.length > 0) {
+        await refreshPriceOnly()
+      }
+      schedulePriceRefresh()
+    }, getRandomInterval())
+  }
+  
+  // 趋势图刷新定时器 (10秒固定间隔)
+  const scheduleTrendRefresh = () => {
+    if (!isPageVisible.value) {
+      stopAutoRefresh()
+      return
+    }
+    
+    trendRefreshTimer = setTimeout(async () => {
+      if (!isPageVisible.value) {
+        stopAutoRefresh()
+        return
+      }
+      
       if (myStocks.value.length > 0) {
         await refreshAllQuotes()
-        // 刷新行情后重绘趋势图
         drawAllTrendCharts()
       }
-      scheduleNextRefresh()
-    }, delay)
+      scheduleTrendRefresh()
+    }, 10000)
   }
   
-  scheduleNextRefresh()
+  schedulePriceRefresh()
+  scheduleTrendRefresh()
 }
 
 // 停止定时刷新
 const stopAutoRefresh = () => {
-  if (refreshTimer) {
-    clearTimeout(refreshTimer)
-    refreshTimer = null
+  if (priceRefreshTimer) {
+    clearTimeout(priceRefreshTimer)
+    priceRefreshTimer = null
+  }
+  if (trendRefreshTimer) {
+    clearTimeout(trendRefreshTimer)
+    trendRefreshTimer = null
   }
 }
 

+ 2 - 5
src/utils/api.js

@@ -2,15 +2,12 @@
  * API请求工具类
  */
 
-// ============ 配置区域 - 请根据实际情况修改 ============
-// 开发环境:微信开发者工具中使用 localhost
-// 真机调试:改为你电脑的局域网IP(通过 ipconfig 或 ifconfig 查看)
-// 生产环境:改为正式服务器地址
+// ============ 配置区域 ============
 
 const ENV = 'dev' // 'dev' | 'prod'
 
 const CONFIG = {
-  dev: 'http://localhost:8081',      // 开发环境(开发者工具)
+  dev: 'http://localhost:8081',      // 开发环境
   prod: 'http://192.168.1.2:8081'    // 生产环境
 }