strong.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if (!utils_auth.isLoggedIn()) {
  140. common_vendor.index.showModal({
  141. title: "登录提示",
  142. content: "添加自选股票需要登录,是否前往登录?",
  143. confirmText: "去登录",
  144. cancelText: "取消",
  145. success: (res) => {
  146. if (res.confirm) {
  147. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  148. }
  149. }
  150. });
  151. return;
  152. }
  153. try {
  154. common_vendor.index.showLoading({ title: "添加中..." });
  155. let currentPrice = null;
  156. try {
  157. const quoteRes = await utils_api.getStockQuotes(stock.code);
  158. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  159. currentPrice = quoteRes.data[0].currentPrice;
  160. }
  161. } catch (e) {
  162. console.error("获取行情数据失败:", e);
  163. }
  164. const addRes = await utils_api.addUserStock({
  165. stockCode: stock.code,
  166. stockName: stock.name,
  167. currentPrice,
  168. poolType: 2
  169. // 强势池
  170. });
  171. common_vendor.index.hideLoading();
  172. if (addRes.code === 200 && addRes.data === true) {
  173. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  174. } else if (addRes.code === 200 && addRes.data === false) {
  175. common_vendor.index.showToast({ title: "股票已存在", icon: "none" });
  176. } else {
  177. common_vendor.index.showToast({ title: addRes.message || "添加失败", icon: "none" });
  178. }
  179. } catch (e) {
  180. common_vendor.index.hideLoading();
  181. console.error("添加股票失败:", e);
  182. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  183. }
  184. };
  185. common_vendor.onLoad(() => {
  186. console.log("[强势池] onLoad");
  187. isPageVisible.value = true;
  188. checkPurchaseStatus();
  189. });
  190. common_vendor.onShow(() => {
  191. console.log("[强势池] onShow");
  192. isPageVisible.value = true;
  193. checkPurchaseStatus();
  194. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  195. });
  196. common_vendor.onHide(() => {
  197. console.log("[强势池] onHide");
  198. isPageVisible.value = false;
  199. stopAutoRefresh();
  200. });
  201. common_vendor.onUnmounted(() => {
  202. isPageVisible.value = false;
  203. stopAutoRefresh();
  204. });
  205. return (_ctx, _cache) => {
  206. return common_vendor.e({
  207. a: common_vendor.p({
  208. successRate: "88%",
  209. profitRate: "+12.5%",
  210. totalTrades: 45
  211. }),
  212. b: !isPurchased.value
  213. }, !isPurchased.value ? {
  214. c: common_vendor.o(showPurchaseModal)
  215. } : {
  216. d: common_vendor.f(stockList.value, (stock, index, i0) => {
  217. return {
  218. a: common_vendor.t(stock.name),
  219. b: common_vendor.t(stock.code),
  220. c: common_vendor.t(stock.currentPrice || "-"),
  221. d: common_vendor.t(stock.changePercent || "-"),
  222. e: common_vendor.n(getChangeClass(stock.changePercent)),
  223. f: common_vendor.o(($event) => addToMyStocks(stock), index),
  224. g: index
  225. };
  226. })
  227. }, {
  228. e: common_vendor.o(onHistorySearch),
  229. f: common_vendor.o(closePurchaseModal),
  230. g: common_vendor.o(handlePurchase),
  231. h: common_vendor.p({
  232. visible: showModal.value,
  233. icon: "📅",
  234. title: "年订阅解锁",
  235. description: "订阅全年,解锁强势趋势池内容",
  236. amountLabel: "订阅金额:",
  237. amount: 98
  238. })
  239. });
  240. };
  241. }
  242. };
  243. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/strong/strong.vue"]]);
  244. wx.createPage(MiniProgramPage);