strong.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. HistorySearchCard();
  7. }
  8. const HistorySearchCard = () => "../../components/HistorySearchCard.js";
  9. const _sfc_main = {
  10. __name: "strong",
  11. setup(__props) {
  12. const isPageVisible = common_vendor.ref(false);
  13. const stockList = common_vendor.ref([]);
  14. let refreshTimer = null;
  15. const getChangeClass = (changePercent) => {
  16. if (!changePercent || changePercent === "-")
  17. return "";
  18. return changePercent.startsWith("+") ? "text-red" : "text-green";
  19. };
  20. const getRandomInterval = () => 4500 + Math.random() * 1e3;
  21. const startAutoRefresh = () => {
  22. if (!isPageVisible.value)
  23. return;
  24. stopAutoRefresh();
  25. const scheduleNextRefresh = () => {
  26. if (!isPageVisible.value) {
  27. stopAutoRefresh();
  28. return;
  29. }
  30. refreshTimer = setTimeout(async () => {
  31. if (!isPageVisible.value) {
  32. stopAutoRefresh();
  33. return;
  34. }
  35. await loadStockPool();
  36. scheduleNextRefresh();
  37. }, getRandomInterval());
  38. };
  39. scheduleNextRefresh();
  40. };
  41. const stopAutoRefresh = () => {
  42. if (refreshTimer) {
  43. clearTimeout(refreshTimer);
  44. refreshTimer = null;
  45. }
  46. };
  47. const loadStockPool = async () => {
  48. try {
  49. const res = await utils_api.getStockPoolList(2);
  50. if (res.code === 200 && res.data) {
  51. stockList.value = res.data;
  52. }
  53. } catch (e) {
  54. console.error("加载股票池失败:", e);
  55. }
  56. };
  57. const loadAndStartRefresh = async () => {
  58. await loadStockPool();
  59. startAutoRefresh();
  60. };
  61. const addToMyStocks = async (stock) => {
  62. if (!utils_auth.isLoggedIn()) {
  63. common_vendor.index.showModal({
  64. title: "登录提示",
  65. content: "此功能需要登录后使用,是否前往登录?",
  66. confirmText: "去登录",
  67. cancelText: "取消",
  68. success: (res) => {
  69. if (res.confirm) {
  70. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  71. }
  72. }
  73. });
  74. return;
  75. }
  76. try {
  77. common_vendor.index.showLoading({ title: "添加中..." });
  78. let currentPrice = null;
  79. try {
  80. const quoteRes = await utils_api.getStockQuotes(stock.code);
  81. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  82. currentPrice = quoteRes.data[0].currentPrice;
  83. }
  84. } catch (e) {
  85. console.error("获取行情数据失败:", e);
  86. }
  87. const addRes = await utils_api.addUserStock({
  88. stockCode: stock.code,
  89. stockName: stock.name,
  90. currentPrice,
  91. poolType: 2
  92. // 强势池
  93. });
  94. common_vendor.index.hideLoading();
  95. if (addRes.code === 200 && addRes.data === true) {
  96. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  97. } else if (addRes.code === 200 && addRes.data === false) {
  98. common_vendor.index.showToast({ title: "样本已存在", icon: "none" });
  99. } else {
  100. common_vendor.index.showToast({ title: addRes.message || "添加失败", icon: "none" });
  101. }
  102. } catch (e) {
  103. common_vendor.index.hideLoading();
  104. console.error("添加样本失败:", e);
  105. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  106. }
  107. };
  108. common_vendor.onLoad(() => {
  109. console.log("[强势池] onLoad");
  110. isPageVisible.value = true;
  111. loadAndStartRefresh();
  112. });
  113. common_vendor.onShow(() => {
  114. console.log("[强势池] onShow");
  115. isPageVisible.value = true;
  116. loadAndStartRefresh();
  117. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  118. });
  119. common_vendor.onHide(() => {
  120. console.log("[强势池] onHide");
  121. isPageVisible.value = false;
  122. stopAutoRefresh();
  123. });
  124. common_vendor.onUnmounted(() => {
  125. isPageVisible.value = false;
  126. stopAutoRefresh();
  127. });
  128. return (_ctx, _cache) => {
  129. return {
  130. a: common_vendor.f(stockList.value, (stock, index, i0) => {
  131. return {
  132. a: common_vendor.t(stock.name),
  133. b: common_vendor.t(stock.code),
  134. c: common_vendor.t(stock.currentPrice || "-"),
  135. d: common_vendor.t(stock.changePercent || "-"),
  136. e: common_vendor.n(getChangeClass(stock.changePercent)),
  137. f: common_vendor.o(($event) => addToMyStocks(stock), index),
  138. g: index
  139. };
  140. }),
  141. b: common_vendor.p({
  142. poolType: 2,
  143. canSearch: true
  144. })
  145. };
  146. };
  147. }
  148. };
  149. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/strong/strong.vue"]]);
  150. wx.createPage(MiniProgramPage);