"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_auth = require("../../utils/auth.js"); const utils_api = require("../../utils/api.js"); if (!Math) { StockListItem(); } const StockListItem = () => "../../components/StockListItem.js"; const _sfc_main = { __name: "rank", setup(__props) { const isLoggedIn = common_vendor.ref(false); const myStocks = common_vendor.ref([]); const viewMode = common_vendor.ref("list"); const toggleViewMode = () => { viewMode.value = viewMode.value === "list" ? "table" : "list"; }; const indexData = common_vendor.ref({ stockCode: "000001", stockName: "上证指数", currentPrice: null, priceChange: null, changePercent: null }); let refreshTimer = null; const fetchIndexData = async () => { try { const res = await utils_api.getIndexQuote("000001"); if (res.code === 200 && res.data) { indexData.value = { ...indexData.value, ...res.data }; } } catch (e) { console.error("[上证指数] 获取失败:", e.message); } }; const formatIndexPrice = (price) => { if (!price) return "--"; return parseFloat(price).toFixed(2); }; const formatPrice = (price) => { if (!price) return "--"; return parseFloat(price).toFixed(2); }; const getIndexChangeClass = (changePercent) => { if (!changePercent) return ""; const str = String(changePercent).replace("%", "").replace("+", ""); const value = parseFloat(str); if (value > 0) return "index-up"; if (value < 0) return "index-down"; return ""; }; const getProfitClass = (profitPercent) => { if (!profitPercent) return ""; const str = String(profitPercent).replace("%", "").replace("+", ""); const value = parseFloat(str); if (value > 0) return "profit-up"; if (value < 0) return "profit-down"; return ""; }; const getMarketTag = (code) => { if (code.startsWith("6")) return "沪"; if (code.startsWith("0")) return "深"; if (code.startsWith("3")) return "创"; return "沪"; }; const getMarketClass = (code) => { if (code.startsWith("6")) return "market-sh"; if (code.startsWith("0")) return "market-sz"; if (code.startsWith("3")) return "market-cy"; return "market-sh"; }; const loadMyStocks = async () => { if (!isLoggedIn.value) { myStocks.value = []; stopAutoRefresh(); return; } try { const res = await utils_api.getUserStocks(); console.log("[我的股票] 服务器返回:", JSON.stringify(res)); if (res.code === 200 && res.data) { myStocks.value = res.data.map((item) => ({ code: item.stockCode, name: item.stockName, addPrice: item.addPrice, addDate: item.addDate, currentPrice: item.currentPrice, profitPercent: item.profitPercent, priceChange: item.priceChange, changePercent: item.changePercent, trendData: item.trendData })); } else { myStocks.value = []; } await fetchIndexData(); if (myStocks.value.length > 0) { await refreshAllQuotes(); } startAutoRefresh(); } catch (e) { console.error("加载股票列表失败:", e); myStocks.value = []; startAutoRefresh(); } }; const refreshAllQuotes = 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; stock.trendData = quoteData.trendData || null; 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 startAutoRefresh = () => { if (!isLoggedIn.value) return; stopAutoRefresh(); const scheduleNextRefresh = () => { const delay = 3e3 + Math.random() * 1e3; refreshTimer = setTimeout(async () => { await fetchIndexData(); if (myStocks.value.length > 0) { await refreshAllQuotes(); } scheduleNextRefresh(); }, delay); }; scheduleNextRefresh(); }; const stopAutoRefresh = () => { if (refreshTimer) { clearTimeout(refreshTimer); refreshTimer = null; } }; const goToLogin = () => { common_vendor.index.navigateTo({ url: "/pages/login/login" }); }; const handleStockClick = (stockItem, idx) => { console.log("点击股票:", stockItem.name, idx); common_vendor.index.showToast({ title: "股票详情功能开发中", icon: "none" }); }; const removeStock = async (idx) => { const stock = myStocks.value[idx]; common_vendor.index.showModal({ title: "确认删除", content: `确定要删除 ${stock.name} 吗?`, confirmText: "删除", cancelText: "取消", success: async (res) => { if (res.confirm) { try { await utils_api.deleteUserStock(stock.code); myStocks.value.splice(idx, 1); common_vendor.index.showToast({ title: "删除成功", icon: "success" }); if (myStocks.value.length === 0) { stopAutoRefresh(); } } catch (e) { console.error("删除失败:", e); common_vendor.index.showToast({ title: "删除失败", icon: "none" }); } } } }); }; common_vendor.onLoad(() => { isLoggedIn.value = utils_auth.isLoggedIn(); loadMyStocks(); }); common_vendor.onShow(() => { isLoggedIn.value = utils_auth.isLoggedIn(); loadMyStocks(); common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" }); }); common_vendor.onHide(() => { stopAutoRefresh(); }); common_vendor.onUnload(() => { stopAutoRefresh(); }); return (_ctx, _cache) => { return common_vendor.e({ a: common_vendor.t(formatIndexPrice(indexData.value.currentPrice)), b: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)), c: common_vendor.t(indexData.value.priceChange || "--"), d: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)), e: common_vendor.t(indexData.value.stockName || "上证指数"), f: common_vendor.t(indexData.value.changePercent || "--"), g: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)), h: common_vendor.t(viewMode.value === "list" ? "📊" : "📋"), i: common_vendor.o(toggleViewMode), j: common_vendor.f(myStocks.value, (stock, index, i0) => { return { a: stock.code, b: common_vendor.o(($event) => removeStock(index), stock.code), c: common_vendor.o(($event) => handleStockClick(stock, index), stock.code), d: "2482292d-0-" + i0, e: common_vendor.p({ stock, ["show-delete"]: true }) }; }), k: viewMode.value === "list", l: myStocks.value.length === 0 ? 1 : "", m: common_vendor.f(myStocks.value, (stock, index, i0) => { return { a: common_vendor.t(stock.name), b: common_vendor.t(getMarketTag(stock.code)), c: common_vendor.n(getMarketClass(stock.code)), d: common_vendor.t(stock.code), e: common_vendor.t(stock.addDate || "--"), f: common_vendor.t(formatPrice(stock.addPrice)), g: common_vendor.t(stock.profitPercent || "--"), h: common_vendor.n(getProfitClass(stock.profitPercent)), i: stock.code, j: common_vendor.o(($event) => handleStockClick(stock, index), stock.code) }; }), n: viewMode.value === "table", o: myStocks.value.length === 0 ? 1 : "", p: myStocks.value.length === 0 }, myStocks.value.length === 0 ? {} : {}, { q: !isLoggedIn.value ? 1 : "", r: !isLoggedIn.value }, !isLoggedIn.value ? { s: common_vendor.o(goToLogin) } : {}); }; } }; const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/rank/rank.vue"]]); wx.createPage(MiniProgramPage);