pool.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_auth = require("../../utils/auth.js");
  4. const utils_api = require("../../utils/api.js");
  5. if (!Math) {
  6. (PerformanceCard + HistorySearchCard + PurchaseModal)();
  7. }
  8. const PurchaseModal = () => "../../components/PurchaseModal.js";
  9. const PerformanceCard = () => "../../components/PerformanceCard.js";
  10. const HistorySearchCard = () => "../../components/HistorySearchCard.js";
  11. const _sfc_main = {
  12. __name: "pool",
  13. setup(__props) {
  14. const isPurchased = common_vendor.ref(false);
  15. const showModal = common_vendor.ref(false);
  16. const isLoggedIn = common_vendor.ref(false);
  17. const isPageVisible = common_vendor.ref(false);
  18. const shortPrice = common_vendor.ref(1);
  19. const stockList = common_vendor.ref([]);
  20. let refreshTimer = null;
  21. const performanceStats = common_vendor.reactive({
  22. successRate: "0%",
  23. avgTrend: "+0%",
  24. totalCount: 0
  25. });
  26. const onDateChange = async ({ startDate, endDate, poolType }) => {
  27. try {
  28. const res = await utils_api.getStockHistoryStats({
  29. startDate,
  30. endDate,
  31. poolType
  32. });
  33. if (res.code === 200 && res.data) {
  34. performanceStats.successRate = res.data.successRate || "0%";
  35. performanceStats.avgTrend = res.data.avgTrend || "+0%";
  36. performanceStats.totalCount = res.data.totalCount || 0;
  37. }
  38. } catch (e) {
  39. console.error("加载统计数据失败:", e);
  40. }
  41. };
  42. const getChangeClass = (changePercent) => {
  43. if (!changePercent || changePercent === "-")
  44. return "";
  45. return changePercent.startsWith("+") ? "text-red" : "text-green";
  46. };
  47. const getRandomInterval = () => 2e3 + Math.random() * 1e3;
  48. const startAutoRefresh = () => {
  49. if (!isPageVisible.value)
  50. return;
  51. stopAutoRefresh();
  52. const scheduleNextRefresh = () => {
  53. if (!isPageVisible.value) {
  54. stopAutoRefresh();
  55. return;
  56. }
  57. refreshTimer = setTimeout(async () => {
  58. if (!isPageVisible.value) {
  59. stopAutoRefresh();
  60. return;
  61. }
  62. await loadStockPool();
  63. scheduleNextRefresh();
  64. }, getRandomInterval());
  65. };
  66. scheduleNextRefresh();
  67. };
  68. const stopAutoRefresh = () => {
  69. if (refreshTimer) {
  70. clearTimeout(refreshTimer);
  71. refreshTimer = null;
  72. }
  73. };
  74. const checkLogin = () => {
  75. isLoggedIn.value = utils_auth.isLoggedIn();
  76. return isLoggedIn.value;
  77. };
  78. const checkPurchaseStatus = async () => {
  79. if (!checkLogin()) {
  80. isPurchased.value = false;
  81. stopAutoRefresh();
  82. return;
  83. }
  84. try {
  85. const res = await utils_api.checkSubscription(1);
  86. if (res.code === 200 && res.data.hasSubscription) {
  87. isPurchased.value = true;
  88. loadAndStartRefresh();
  89. } else {
  90. isPurchased.value = false;
  91. stopAutoRefresh();
  92. }
  93. } catch (e) {
  94. console.error("检查订阅状态失败:", e);
  95. isPurchased.value = false;
  96. stopAutoRefresh();
  97. }
  98. };
  99. const loadStockPool = async () => {
  100. try {
  101. const res = await utils_api.getStockPoolList(1);
  102. if (res.code === 200 && res.data) {
  103. stockList.value = res.data;
  104. }
  105. } catch (e) {
  106. console.error("加载标的池失败:", e);
  107. }
  108. };
  109. const loadAndStartRefresh = async () => {
  110. await loadStockPool();
  111. startAutoRefresh();
  112. };
  113. const showPurchaseModal = async () => {
  114. if (!checkLogin()) {
  115. common_vendor.index.showModal({
  116. title: "登录提示",
  117. content: "此功能需要登录后使用,是否前往登录?",
  118. confirmText: "去登录",
  119. cancelText: "取消",
  120. success: (res) => {
  121. if (res.confirm) {
  122. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  123. }
  124. }
  125. });
  126. return;
  127. }
  128. try {
  129. const res = await utils_api.getPaymentConfig(1);
  130. if (res.code === 200 && res.data) {
  131. shortPrice.value = res.data.price || 1;
  132. }
  133. } catch (e) {
  134. console.error("获取价格配置失败:", e);
  135. }
  136. showModal.value = true;
  137. };
  138. const closePurchaseModal = () => {
  139. showModal.value = false;
  140. };
  141. const pollOrderStatus = async (orderNo, maxRetries = 5, interval = 1e3) => {
  142. for (let i = 0; i < maxRetries; i++) {
  143. try {
  144. const res = await utils_api.queryOrder(orderNo);
  145. if (res.code === 200 && res.data && res.data.orderStatus === 1) {
  146. return true;
  147. }
  148. } catch (e) {
  149. console.log("查询订单状态失败:", e);
  150. }
  151. if (i < maxRetries - 1) {
  152. await new Promise((r) => setTimeout(r, interval));
  153. }
  154. }
  155. return false;
  156. };
  157. const handlePurchase = async () => {
  158. try {
  159. common_vendor.index.showLoading({ title: "正在支付..." });
  160. const res = await utils_api.createOrder({ poolType: 1 });
  161. if (res.code !== 200) {
  162. throw new Error(res.message || "创建订单失败");
  163. }
  164. const orderNo = res.data.orderNo;
  165. common_vendor.index.hideLoading();
  166. await utils_api.wxPay(res.data);
  167. common_vendor.index.showLoading({ title: "确认支付结果..." });
  168. const confirmed = await pollOrderStatus(orderNo);
  169. common_vendor.index.hideLoading();
  170. if (confirmed) {
  171. isPurchased.value = true;
  172. closePurchaseModal();
  173. common_vendor.index.showToast({ title: "支付成功", icon: "success" });
  174. loadAndStartRefresh();
  175. } else {
  176. closePurchaseModal();
  177. common_vendor.index.showToast({ title: "支付处理中,请稍后刷新", icon: "none" });
  178. }
  179. } catch (e) {
  180. common_vendor.index.hideLoading();
  181. common_vendor.index.showToast({ title: e.message || "支付失败", icon: "none" });
  182. }
  183. };
  184. const addToMyStocks = async (stock) => {
  185. if (!checkLogin()) {
  186. common_vendor.index.showModal({
  187. title: "登录提示",
  188. content: "添加自选需要登录,是否前往登录?",
  189. confirmText: "去登录",
  190. cancelText: "取消",
  191. success: (res) => {
  192. if (res.confirm) {
  193. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  194. }
  195. }
  196. });
  197. return;
  198. }
  199. try {
  200. common_vendor.index.showLoading({ title: "添加中..." });
  201. let currentPrice = null;
  202. try {
  203. const quoteRes = await utils_api.getStockQuotes(stock.code);
  204. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  205. currentPrice = quoteRes.data[0].currentPrice;
  206. }
  207. } catch (e) {
  208. console.error("获取行情数据失败:", e);
  209. }
  210. const addRes = await utils_api.addUserStock({
  211. stockCode: stock.code,
  212. stockName: stock.name,
  213. currentPrice,
  214. poolType: 1
  215. // 超短池
  216. });
  217. common_vendor.index.hideLoading();
  218. if (addRes.code === 200 && addRes.data === true) {
  219. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  220. } else if (addRes.code === 200 && addRes.data === false) {
  221. common_vendor.index.showToast({ title: "标的已存在", icon: "none" });
  222. } else {
  223. common_vendor.index.showToast({ title: addRes.message || "添加失败", icon: "none" });
  224. }
  225. } catch (e) {
  226. common_vendor.index.hideLoading();
  227. console.error("添加标的失败:", e);
  228. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  229. }
  230. };
  231. common_vendor.onLoad(() => {
  232. console.log("[超短池] onLoad");
  233. isPageVisible.value = true;
  234. checkPurchaseStatus();
  235. });
  236. common_vendor.onShow(() => {
  237. console.log("[超短池] onShow");
  238. isPageVisible.value = true;
  239. checkPurchaseStatus();
  240. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  241. });
  242. common_vendor.onHide(() => {
  243. console.log("[超短池] onHide");
  244. isPageVisible.value = false;
  245. stopAutoRefresh();
  246. });
  247. common_vendor.onUnmounted(() => {
  248. isPageVisible.value = false;
  249. stopAutoRefresh();
  250. });
  251. return (_ctx, _cache) => {
  252. return common_vendor.e({
  253. a: common_vendor.p({
  254. successRate: performanceStats.successRate,
  255. profitRate: performanceStats.avgTrend,
  256. totalTrades: performanceStats.totalCount
  257. }),
  258. b: !isPurchased.value
  259. }, !isPurchased.value ? {
  260. c: common_vendor.o(showPurchaseModal)
  261. } : {
  262. d: common_vendor.f(stockList.value, (stock, index, i0) => {
  263. return {
  264. a: common_vendor.t(stock.name),
  265. b: common_vendor.t(stock.code),
  266. c: common_vendor.t(stock.currentPrice || "-"),
  267. d: common_vendor.t(stock.changePercent || "-"),
  268. e: common_vendor.n(getChangeClass(stock.changePercent)),
  269. f: common_vendor.o(($event) => addToMyStocks(stock), index),
  270. g: index
  271. };
  272. })
  273. }, {
  274. e: common_vendor.o(onDateChange),
  275. f: common_vendor.p({
  276. poolType: 1,
  277. canSearch: isPurchased.value
  278. }),
  279. g: common_vendor.o(closePurchaseModal),
  280. h: common_vendor.o(handlePurchase),
  281. i: common_vendor.p({
  282. visible: showModal.value,
  283. icon: "💰",
  284. title: "打赏解锁",
  285. description: "支持作者,解锁今日超短池内容",
  286. amountLabel: "打赏金额:",
  287. amount: shortPrice.value
  288. })
  289. });
  290. };
  291. }
  292. };
  293. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/pool/pool.vue"]]);
  294. wx.createPage(MiniProgramPage);