strong.js 8.4 KB

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