pool.js 8.2 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 = () => {
  57. if (!checkLogin()) {
  58. isPurchased.value = false;
  59. stopAutoRefresh();
  60. return;
  61. }
  62. try {
  63. const purchaseInfo = common_vendor.index.getStorageSync("pool_purchase");
  64. if (purchaseInfo) {
  65. const now = Date.now();
  66. if (now < purchaseInfo.expireTime) {
  67. isPurchased.value = true;
  68. loadAndStartRefresh();
  69. } else {
  70. common_vendor.index.removeStorageSync("pool_purchase");
  71. isPurchased.value = false;
  72. stopAutoRefresh();
  73. }
  74. } else {
  75. isPurchased.value = false;
  76. stopAutoRefresh();
  77. }
  78. } catch (e) {
  79. console.error("检查购买状态失败:", e);
  80. isPurchased.value = false;
  81. stopAutoRefresh();
  82. }
  83. };
  84. const loadStockPool = async () => {
  85. try {
  86. const res = await utils_api.getStockPoolList(1);
  87. if (res.code === 200 && res.data) {
  88. stockList.value = res.data;
  89. }
  90. } catch (e) {
  91. console.error("加载股票池失败:", e);
  92. }
  93. };
  94. const loadAndStartRefresh = async () => {
  95. await loadStockPool();
  96. startAutoRefresh();
  97. };
  98. const showPurchaseModal = () => {
  99. if (!checkLogin()) {
  100. common_vendor.index.showModal({
  101. title: "登录提示",
  102. content: "此功能需要登录后使用,是否前往登录?",
  103. confirmText: "去登录",
  104. cancelText: "取消",
  105. success: (res) => {
  106. if (res.confirm) {
  107. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  108. }
  109. }
  110. });
  111. return;
  112. }
  113. showModal.value = true;
  114. };
  115. const closePurchaseModal = () => {
  116. showModal.value = false;
  117. };
  118. const handlePurchase = () => {
  119. const now = Date.now();
  120. const today = /* @__PURE__ */ new Date();
  121. today.setHours(23, 59, 59, 999);
  122. common_vendor.index.setStorageSync("pool_purchase", {
  123. plan: "reward",
  124. purchaseTime: now,
  125. expireTime: today.getTime()
  126. });
  127. isPurchased.value = true;
  128. closePurchaseModal();
  129. common_vendor.index.showToast({ title: "解锁成功", icon: "success" });
  130. loadAndStartRefresh();
  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);