"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 setViewMode = (mode) => { viewMode.value = mode; }; 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 () => { console.log("[我的股票] loadMyStocks 开始执行, isLoggedIn=", isLoggedIn.value); if (!isLoggedIn.value) { myStocks.value = []; stopAutoRefresh(); return; } try { common_vendor.index.showLoading({ title: "加载中..." }); console.log("[我的股票] 调用 getUserStocks 接口"); const res = await utils_api.getUserStocks(); console.log("[我的股票] 服务器返回:", JSON.stringify(res)); common_vendor.index.hideLoading(); 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 })); console.log("[我的股票] 加载完成, 股票数量:", myStocks.value.length); } else { myStocks.value = []; console.log("[我的股票] 返回数据为空"); } await fetchIndexData(); if (myStocks.value.length > 0) { await refreshAllQuotes(); } startAutoRefresh(); } catch (e) { common_vendor.index.hideLoading(); 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 = async (stockItem, idx) => { console.log("点击股票:", stockItem.name, idx); try { const quoteRes = await utils_api.getStockQuotes(stockItem.code); if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) { const quoteData = quoteRes.data[0]; const stock = myStocks.value[idx]; if (stock) { 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); } 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(() => { console.log("[我的股票] onLoad 触发"); isLoggedIn.value = utils_auth.isLoggedIn(); loadMyStocks(); }); common_vendor.onShow(() => { console.log("[我的股票] 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: viewMode.value === "table" ? 1 : "", i: viewMode.value === "list" ? 1 : "", j: common_vendor.o(($event) => setViewMode("list")), k: viewMode.value === "table" ? 1 : "", l: common_vendor.o(($event) => setViewMode("table")), m: 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 }) }; }), n: viewMode.value === "list", o: myStocks.value.length === 0 ? 1 : "", p: 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) }; }), q: viewMode.value === "table", r: myStocks.value.length === 0 ? 1 : "", s: myStocks.value.length === 0 }, myStocks.value.length === 0 ? {} : {}, { t: !isLoggedIn.value ? 1 : "", v: !isLoggedIn.value }, !isLoggedIn.value ? { w: 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);