| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- "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) {
- (PerformanceCard + HistorySearchCard + PurchaseModal)();
- }
- const PurchaseModal = () => "../../components/PurchaseModal.js";
- const PerformanceCard = () => "../../components/PerformanceCard.js";
- const HistorySearchCard = () => "../../components/HistorySearchCard.js";
- const _sfc_main = {
- __name: "pool",
- setup(__props) {
- const isPurchased = common_vendor.ref(false);
- const showModal = common_vendor.ref(false);
- const isLoggedIn = common_vendor.ref(false);
- const isPageVisible = common_vendor.ref(false);
- const shortPrice = common_vendor.ref(1);
- const stockList = common_vendor.ref([]);
- let refreshTimer = null;
- const performanceStats = common_vendor.reactive({
- successRate: "0%",
- avgTrend: "+0%",
- totalCount: 0
- });
- const onDateChange = async ({ startDate, endDate, poolType }) => {
- try {
- const res = await utils_api.getStockHistoryStats({
- startDate,
- endDate,
- poolType
- });
- if (res.code === 200 && res.data) {
- performanceStats.successRate = res.data.successRate || "0%";
- performanceStats.avgTrend = res.data.avgTrend || "+0%";
- performanceStats.totalCount = res.data.totalCount || 0;
- }
- } catch (e) {
- console.error("加载统计数据失败:", e);
- }
- };
- const getChangeClass = (changePercent) => {
- if (!changePercent || changePercent === "-")
- return "";
- return changePercent.startsWith("+") ? "text-red" : "text-green";
- };
- const getRandomInterval = () => 2e3 + Math.random() * 1e3;
- const startAutoRefresh = () => {
- if (!isPageVisible.value)
- return;
- stopAutoRefresh();
- const scheduleNextRefresh = () => {
- if (!isPageVisible.value) {
- stopAutoRefresh();
- return;
- }
- refreshTimer = setTimeout(async () => {
- if (!isPageVisible.value) {
- stopAutoRefresh();
- return;
- }
- await loadStockPool();
- scheduleNextRefresh();
- }, getRandomInterval());
- };
- scheduleNextRefresh();
- };
- const stopAutoRefresh = () => {
- if (refreshTimer) {
- clearTimeout(refreshTimer);
- refreshTimer = null;
- }
- };
- const checkLogin = () => {
- isLoggedIn.value = utils_auth.isLoggedIn();
- return isLoggedIn.value;
- };
- const checkPurchaseStatus = async () => {
- if (!checkLogin()) {
- isPurchased.value = false;
- stopAutoRefresh();
- return;
- }
- try {
- const res = await utils_api.checkSubscription(1);
- if (res.code === 200 && res.data.hasSubscription) {
- isPurchased.value = true;
- loadAndStartRefresh();
- } else {
- isPurchased.value = false;
- stopAutoRefresh();
- }
- } catch (e) {
- console.error("检查订阅状态失败:", e);
- isPurchased.value = false;
- stopAutoRefresh();
- }
- };
- const loadStockPool = async () => {
- try {
- const res = await utils_api.getStockPoolList(1);
- if (res.code === 200 && res.data) {
- stockList.value = res.data;
- }
- } catch (e) {
- console.error("加载标的池失败:", e);
- }
- };
- const loadAndStartRefresh = async () => {
- await loadStockPool();
- startAutoRefresh();
- };
- const showPurchaseModal = async () => {
- if (!checkLogin()) {
- common_vendor.index.showModal({
- title: "登录提示",
- content: "此功能需要登录后使用,是否前往登录?",
- confirmText: "去登录",
- cancelText: "取消",
- success: (res) => {
- if (res.confirm) {
- common_vendor.index.navigateTo({ url: "/pages/login/login" });
- }
- }
- });
- return;
- }
- try {
- const res = await utils_api.getPaymentConfig(1);
- if (res.code === 200 && res.data) {
- shortPrice.value = res.data.price || 1;
- }
- } catch (e) {
- console.error("获取价格配置失败:", e);
- }
- showModal.value = true;
- };
- const closePurchaseModal = () => {
- showModal.value = false;
- };
- const pollOrderStatus = async (orderNo, maxRetries = 5, interval = 1e3) => {
- for (let i = 0; i < maxRetries; i++) {
- try {
- const res = await utils_api.queryOrder(orderNo);
- if (res.code === 200 && res.data && res.data.orderStatus === 1) {
- return true;
- }
- } catch (e) {
- console.log("查询订单状态失败:", e);
- }
- if (i < maxRetries - 1) {
- await new Promise((r) => setTimeout(r, interval));
- }
- }
- return false;
- };
- const handlePurchase = async () => {
- try {
- common_vendor.index.showLoading({ title: "正在支付..." });
- const res = await utils_api.createOrder({ poolType: 1 });
- if (res.code !== 200) {
- throw new Error(res.message || "创建订单失败");
- }
- const orderNo = res.data.orderNo;
- common_vendor.index.hideLoading();
- await utils_api.wxPay(res.data);
- common_vendor.index.showLoading({ title: "确认支付结果..." });
- const confirmed = await pollOrderStatus(orderNo);
- common_vendor.index.hideLoading();
- if (confirmed) {
- isPurchased.value = true;
- closePurchaseModal();
- common_vendor.index.showToast({ title: "支付成功", icon: "success" });
- loadAndStartRefresh();
- } else {
- closePurchaseModal();
- common_vendor.index.showToast({ title: "支付处理中,请稍后刷新", icon: "none" });
- }
- } catch (e) {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({ title: e.message || "支付失败", icon: "none" });
- }
- };
- const addToMyStocks = async (stock) => {
- if (!checkLogin()) {
- common_vendor.index.showModal({
- title: "登录提示",
- content: "添加自选需要登录,是否前往登录?",
- confirmText: "去登录",
- cancelText: "取消",
- success: (res) => {
- if (res.confirm) {
- common_vendor.index.navigateTo({ url: "/pages/login/login" });
- }
- }
- });
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "添加中..." });
- let currentPrice = null;
- try {
- const quoteRes = await utils_api.getStockQuotes(stock.code);
- if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
- currentPrice = quoteRes.data[0].currentPrice;
- }
- } catch (e) {
- console.error("获取行情数据失败:", e);
- }
- const addRes = await utils_api.addUserStock({
- stockCode: stock.code,
- stockName: stock.name,
- currentPrice,
- poolType: 1
- // 超短池
- });
- common_vendor.index.hideLoading();
- if (addRes.code === 200 && addRes.data === true) {
- common_vendor.index.showToast({ title: "添加成功", icon: "success" });
- } else if (addRes.code === 200 && addRes.data === false) {
- common_vendor.index.showToast({ title: "标的已存在", icon: "none" });
- } else {
- common_vendor.index.showToast({ title: addRes.message || "添加失败", icon: "none" });
- }
- } catch (e) {
- common_vendor.index.hideLoading();
- console.error("添加标的失败:", e);
- common_vendor.index.showToast({ title: "添加失败", icon: "none" });
- }
- };
- common_vendor.onLoad(() => {
- console.log("[超短池] onLoad");
- isPageVisible.value = true;
- checkPurchaseStatus();
- });
- common_vendor.onShow(() => {
- console.log("[超短池] onShow");
- isPageVisible.value = true;
- checkPurchaseStatus();
- common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
- });
- common_vendor.onHide(() => {
- console.log("[超短池] onHide");
- isPageVisible.value = false;
- stopAutoRefresh();
- });
- common_vendor.onUnmounted(() => {
- isPageVisible.value = false;
- stopAutoRefresh();
- });
- return (_ctx, _cache) => {
- return common_vendor.e({
- a: common_vendor.p({
- successRate: performanceStats.successRate,
- profitRate: performanceStats.avgTrend,
- totalTrades: performanceStats.totalCount
- }),
- b: !isPurchased.value
- }, !isPurchased.value ? {
- c: common_vendor.o(showPurchaseModal)
- } : {
- d: common_vendor.f(stockList.value, (stock, index, i0) => {
- return {
- a: common_vendor.t(stock.name),
- b: common_vendor.t(stock.code),
- c: common_vendor.t(stock.currentPrice || "-"),
- d: common_vendor.t(stock.changePercent || "-"),
- e: common_vendor.n(getChangeClass(stock.changePercent)),
- f: common_vendor.o(($event) => addToMyStocks(stock), index),
- g: index
- };
- })
- }, {
- e: common_vendor.o(onDateChange),
- f: common_vendor.p({
- poolType: 1,
- canSearch: isPurchased.value
- }),
- g: common_vendor.o(closePurchaseModal),
- h: common_vendor.o(handlePurchase),
- i: common_vendor.p({
- visible: showModal.value,
- icon: "💰",
- title: "打赏解锁",
- description: "支持作者,解锁今日超短池内容",
- amountLabel: "打赏金额:",
- amount: shortPrice.value
- })
- });
- };
- }
- };
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/pool/pool.vue"]]);
- wx.createPage(MiniProgramPage);
|