strong.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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: "strong",
  13. setup(__props) {
  14. const isPurchased = common_vendor.ref(false);
  15. const showModal = common_vendor.ref(false);
  16. const isPageVisible = common_vendor.ref(false);
  17. const stockList = common_vendor.ref([]);
  18. let refreshTimer = null;
  19. const getChangeClass = (changePercent) => {
  20. if (!changePercent || changePercent === "-")
  21. return "";
  22. return changePercent.startsWith("+") ? "text-red" : "text-green";
  23. };
  24. const getRandomInterval = () => 2e3 + Math.random() * 1e3;
  25. const startAutoRefresh = () => {
  26. if (!isPageVisible.value)
  27. return;
  28. stopAutoRefresh();
  29. const scheduleNextRefresh = () => {
  30. if (!isPageVisible.value) {
  31. stopAutoRefresh();
  32. return;
  33. }
  34. refreshTimer = setTimeout(async () => {
  35. if (!isPageVisible.value) {
  36. stopAutoRefresh();
  37. return;
  38. }
  39. await loadStockPool();
  40. scheduleNextRefresh();
  41. }, getRandomInterval());
  42. };
  43. scheduleNextRefresh();
  44. };
  45. const stopAutoRefresh = () => {
  46. if (refreshTimer) {
  47. clearTimeout(refreshTimer);
  48. refreshTimer = null;
  49. }
  50. };
  51. const checkPurchaseStatus = () => {
  52. if (!utils_auth.isLoggedIn()) {
  53. isPurchased.value = false;
  54. stopAutoRefresh();
  55. return;
  56. }
  57. try {
  58. const purchaseInfo = common_vendor.index.getStorageSync("strong_pool_purchase");
  59. if (purchaseInfo) {
  60. const now = Date.now();
  61. if (now < purchaseInfo.expireTime) {
  62. isPurchased.value = true;
  63. loadAndStartRefresh();
  64. } else {
  65. common_vendor.index.removeStorageSync("strong_pool_purchase");
  66. isPurchased.value = false;
  67. stopAutoRefresh();
  68. }
  69. } else {
  70. isPurchased.value = false;
  71. stopAutoRefresh();
  72. }
  73. } catch (e) {
  74. console.error("检查购买状态失败:", e);
  75. isPurchased.value = false;
  76. stopAutoRefresh();
  77. }
  78. };
  79. const loadStockPool = async () => {
  80. try {
  81. const res = await utils_api.getStockPoolList(2);
  82. if (res.code === 200 && res.data) {
  83. stockList.value = res.data;
  84. }
  85. } catch (e) {
  86. console.error("加载股票池失败:", e);
  87. }
  88. };
  89. const loadAndStartRefresh = async () => {
  90. await loadStockPool();
  91. startAutoRefresh();
  92. };
  93. const showPurchaseModal = () => {
  94. if (!utils_auth.isLoggedIn()) {
  95. common_vendor.index.showModal({
  96. title: "登录提示",
  97. content: "此功能需要登录后使用,是否前往登录?",
  98. confirmText: "去登录",
  99. cancelText: "取消",
  100. success: (res) => {
  101. if (res.confirm) {
  102. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  103. }
  104. }
  105. });
  106. return;
  107. }
  108. showModal.value = true;
  109. };
  110. const closePurchaseModal = () => {
  111. showModal.value = false;
  112. };
  113. const handlePurchase = () => {
  114. const now = Date.now();
  115. const expireTime = now + 365 * 24 * 60 * 60 * 1e3;
  116. common_vendor.index.setStorageSync("strong_pool_purchase", {
  117. plan: "yearly",
  118. purchaseTime: now,
  119. expireTime
  120. });
  121. isPurchased.value = true;
  122. closePurchaseModal();
  123. common_vendor.index.showToast({ title: "解锁成功", icon: "success" });
  124. loadAndStartRefresh();
  125. };
  126. const onHistorySearch = ({ startMonth, endMonth }) => {
  127. const formatMonth = (monthStr) => {
  128. const [year, month] = monthStr.split("-");
  129. return `${year}年${month}月`;
  130. };
  131. common_vendor.index.showToast({
  132. title: `查询${formatMonth(startMonth)}至${formatMonth(endMonth)}`,
  133. icon: "none",
  134. duration: 2e3
  135. });
  136. };
  137. const addToMyStocks = async (stock) => {
  138. if (!utils_auth.isLoggedIn()) {
  139. common_vendor.index.showModal({
  140. title: "登录提示",
  141. content: "添加自选股票需要登录,是否前往登录?",
  142. confirmText: "去登录",
  143. cancelText: "取消",
  144. success: (res) => {
  145. if (res.confirm) {
  146. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  147. }
  148. }
  149. });
  150. return;
  151. }
  152. try {
  153. common_vendor.index.showLoading({ title: "添加中..." });
  154. let currentPrice = null;
  155. try {
  156. const quoteRes = await utils_api.getStockQuotes(stock.code);
  157. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  158. currentPrice = quoteRes.data[0].currentPrice;
  159. }
  160. } catch (e) {
  161. console.error("获取行情数据失败:", e);
  162. }
  163. const addRes = await utils_api.addUserStock({
  164. stockCode: stock.code,
  165. stockName: stock.name,
  166. currentPrice,
  167. poolType: 2
  168. // 强势池
  169. });
  170. common_vendor.index.hideLoading();
  171. if (addRes.code === 200 && addRes.data === true) {
  172. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  173. } else if (addRes.code === 200 && addRes.data === false) {
  174. common_vendor.index.showToast({ title: "股票已存在", icon: "none" });
  175. } else {
  176. common_vendor.index.showToast({ title: addRes.message || "添加失败", icon: "none" });
  177. }
  178. } catch (e) {
  179. common_vendor.index.hideLoading();
  180. console.error("添加股票失败:", e);
  181. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  182. }
  183. };
  184. common_vendor.onLoad(() => {
  185. console.log("[强势池] onLoad");
  186. isPageVisible.value = true;
  187. checkPurchaseStatus();
  188. });
  189. common_vendor.onShow(() => {
  190. console.log("[强势池] onShow");
  191. isPageVisible.value = true;
  192. checkPurchaseStatus();
  193. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  194. });
  195. common_vendor.onHide(() => {
  196. console.log("[强势池] onHide");
  197. isPageVisible.value = false;
  198. stopAutoRefresh();
  199. });
  200. common_vendor.onUnmounted(() => {
  201. isPageVisible.value = false;
  202. stopAutoRefresh();
  203. });
  204. return (_ctx, _cache) => {
  205. return common_vendor.e({
  206. a: common_vendor.p({
  207. successRate: "88%",
  208. profitRate: "+12.5%",
  209. totalTrades: 45
  210. }),
  211. b: !isPurchased.value
  212. }, !isPurchased.value ? {
  213. c: common_vendor.o(showPurchaseModal)
  214. } : {
  215. d: common_vendor.f(stockList.value, (stock, index, i0) => {
  216. return {
  217. a: common_vendor.t(stock.name),
  218. b: common_vendor.t(stock.code),
  219. c: common_vendor.t(stock.currentPrice || "-"),
  220. d: common_vendor.t(stock.changePercent || "-"),
  221. e: common_vendor.n(getChangeClass(stock.changePercent)),
  222. f: common_vendor.o(($event) => addToMyStocks(stock), index),
  223. g: index
  224. };
  225. })
  226. }, {
  227. e: common_vendor.o(onHistorySearch),
  228. f: common_vendor.o(closePurchaseModal),
  229. g: common_vendor.o(handlePurchase),
  230. h: common_vendor.p({
  231. visible: showModal.value,
  232. icon: "📅",
  233. title: "年订阅解锁",
  234. description: "订阅全年,解锁强势趋势池内容",
  235. amountLabel: "订阅金额:",
  236. amount: 98
  237. })
  238. });
  239. };
  240. }
  241. };
  242. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/strong/strong.vue"]]);
  243. wx.createPage(MiniProgramPage);