pool.js 8.3 KB

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