rank.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. StockListItem();
  7. }
  8. const StockListItem = () => "../../components/StockListItem.js";
  9. const _sfc_main = {
  10. __name: "rank",
  11. setup(__props) {
  12. const isLoggedIn = common_vendor.ref(false);
  13. const myStocks = common_vendor.ref([]);
  14. const viewMode = common_vendor.ref("list");
  15. const toggleViewMode = () => {
  16. viewMode.value = viewMode.value === "list" ? "table" : "list";
  17. };
  18. const indexData = common_vendor.ref({
  19. stockCode: "000001",
  20. stockName: "上证指数",
  21. currentPrice: null,
  22. priceChange: null,
  23. changePercent: null
  24. });
  25. let refreshTimer = null;
  26. const fetchIndexData = async () => {
  27. try {
  28. const res = await utils_api.getIndexQuote("000001");
  29. if (res.code === 200 && res.data) {
  30. indexData.value = { ...indexData.value, ...res.data };
  31. }
  32. } catch (e) {
  33. console.error("[上证指数] 获取失败:", e.message);
  34. }
  35. };
  36. const formatIndexPrice = (price) => {
  37. if (!price)
  38. return "--";
  39. return parseFloat(price).toFixed(2);
  40. };
  41. const formatPrice = (price) => {
  42. if (!price)
  43. return "--";
  44. return parseFloat(price).toFixed(2);
  45. };
  46. const getIndexChangeClass = (changePercent) => {
  47. if (!changePercent)
  48. return "";
  49. const str = String(changePercent).replace("%", "").replace("+", "");
  50. const value = parseFloat(str);
  51. if (value > 0)
  52. return "index-up";
  53. if (value < 0)
  54. return "index-down";
  55. return "";
  56. };
  57. const getProfitClass = (profitPercent) => {
  58. if (!profitPercent)
  59. return "";
  60. const str = String(profitPercent).replace("%", "").replace("+", "");
  61. const value = parseFloat(str);
  62. if (value > 0)
  63. return "profit-up";
  64. if (value < 0)
  65. return "profit-down";
  66. return "";
  67. };
  68. const getMarketTag = (code) => {
  69. if (code.startsWith("6"))
  70. return "沪";
  71. if (code.startsWith("0"))
  72. return "深";
  73. if (code.startsWith("3"))
  74. return "创";
  75. return "沪";
  76. };
  77. const getMarketClass = (code) => {
  78. if (code.startsWith("6"))
  79. return "market-sh";
  80. if (code.startsWith("0"))
  81. return "market-sz";
  82. if (code.startsWith("3"))
  83. return "market-cy";
  84. return "market-sh";
  85. };
  86. const loadMyStocks = async () => {
  87. if (!isLoggedIn.value) {
  88. myStocks.value = [];
  89. stopAutoRefresh();
  90. return;
  91. }
  92. try {
  93. const res = await utils_api.getUserStocks();
  94. console.log("[我的股票] 服务器返回:", JSON.stringify(res));
  95. if (res.code === 200 && res.data) {
  96. myStocks.value = res.data.map((item) => ({
  97. code: item.stockCode,
  98. name: item.stockName,
  99. addPrice: item.addPrice,
  100. addDate: item.addDate,
  101. currentPrice: item.currentPrice,
  102. profitPercent: item.profitPercent,
  103. priceChange: item.priceChange,
  104. changePercent: item.changePercent,
  105. trendData: item.trendData
  106. }));
  107. } else {
  108. myStocks.value = [];
  109. }
  110. await fetchIndexData();
  111. if (myStocks.value.length > 0) {
  112. await refreshAllQuotes();
  113. }
  114. startAutoRefresh();
  115. } catch (e) {
  116. console.error("加载股票列表失败:", e);
  117. myStocks.value = [];
  118. startAutoRefresh();
  119. }
  120. };
  121. const refreshAllQuotes = async () => {
  122. if (myStocks.value.length === 0)
  123. return;
  124. try {
  125. const codes = myStocks.value.map((stock) => stock.code).join(",");
  126. const quoteRes = await utils_api.getStockQuotes(codes);
  127. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  128. quoteRes.data.forEach((quoteData) => {
  129. const index = myStocks.value.findIndex((stock) => stock.code === quoteData.stockCode);
  130. if (index !== -1) {
  131. const stock = myStocks.value[index];
  132. stock.priceChange = quoteData.priceChange;
  133. stock.changePercent = quoteData.changePercent;
  134. stock.currentPrice = quoteData.currentPrice;
  135. stock.name = quoteData.stockName || stock.name;
  136. stock.trendData = quoteData.trendData || null;
  137. if (stock.addPrice && quoteData.currentPrice) {
  138. const addPrice = parseFloat(stock.addPrice);
  139. const currentPrice = parseFloat(quoteData.currentPrice);
  140. if (addPrice > 0) {
  141. const profit = ((currentPrice - addPrice) / addPrice * 100).toFixed(2);
  142. stock.profitPercent = profit >= 0 ? `+${profit}%` : `${profit}%`;
  143. }
  144. }
  145. }
  146. });
  147. }
  148. } catch (e) {
  149. console.error("[我的股票] 刷新异常:", e.message);
  150. }
  151. };
  152. const startAutoRefresh = () => {
  153. if (!isLoggedIn.value)
  154. return;
  155. stopAutoRefresh();
  156. const scheduleNextRefresh = () => {
  157. const delay = 3e3 + Math.random() * 1e3;
  158. refreshTimer = setTimeout(async () => {
  159. await fetchIndexData();
  160. if (myStocks.value.length > 0) {
  161. await refreshAllQuotes();
  162. }
  163. scheduleNextRefresh();
  164. }, delay);
  165. };
  166. scheduleNextRefresh();
  167. };
  168. const stopAutoRefresh = () => {
  169. if (refreshTimer) {
  170. clearTimeout(refreshTimer);
  171. refreshTimer = null;
  172. }
  173. };
  174. const goToLogin = () => {
  175. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  176. };
  177. const handleStockClick = (stockItem, idx) => {
  178. console.log("点击股票:", stockItem.name, idx);
  179. common_vendor.index.showToast({ title: "股票详情功能开发中", icon: "none" });
  180. };
  181. const removeStock = async (idx) => {
  182. const stock = myStocks.value[idx];
  183. common_vendor.index.showModal({
  184. title: "确认删除",
  185. content: `确定要删除 ${stock.name} 吗?`,
  186. confirmText: "删除",
  187. cancelText: "取消",
  188. success: async (res) => {
  189. if (res.confirm) {
  190. try {
  191. await utils_api.deleteUserStock(stock.code);
  192. myStocks.value.splice(idx, 1);
  193. common_vendor.index.showToast({ title: "删除成功", icon: "success" });
  194. if (myStocks.value.length === 0) {
  195. stopAutoRefresh();
  196. }
  197. } catch (e) {
  198. console.error("删除失败:", e);
  199. common_vendor.index.showToast({ title: "删除失败", icon: "none" });
  200. }
  201. }
  202. }
  203. });
  204. };
  205. common_vendor.onLoad(() => {
  206. isLoggedIn.value = utils_auth.isLoggedIn();
  207. loadMyStocks();
  208. });
  209. common_vendor.onShow(() => {
  210. isLoggedIn.value = utils_auth.isLoggedIn();
  211. loadMyStocks();
  212. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  213. });
  214. common_vendor.onHide(() => {
  215. stopAutoRefresh();
  216. });
  217. common_vendor.onUnload(() => {
  218. stopAutoRefresh();
  219. });
  220. return (_ctx, _cache) => {
  221. return common_vendor.e({
  222. a: common_vendor.t(formatIndexPrice(indexData.value.currentPrice)),
  223. b: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  224. c: common_vendor.t(indexData.value.priceChange || "--"),
  225. d: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  226. e: common_vendor.t(indexData.value.stockName || "上证指数"),
  227. f: common_vendor.t(indexData.value.changePercent || "--"),
  228. g: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  229. h: common_vendor.t(viewMode.value === "list" ? "📊" : "📋"),
  230. i: common_vendor.o(toggleViewMode),
  231. j: common_vendor.f(myStocks.value, (stock, index, i0) => {
  232. return {
  233. a: stock.code,
  234. b: common_vendor.o(($event) => removeStock(index), stock.code),
  235. c: common_vendor.o(($event) => handleStockClick(stock, index), stock.code),
  236. d: "2482292d-0-" + i0,
  237. e: common_vendor.p({
  238. stock,
  239. ["show-delete"]: true
  240. })
  241. };
  242. }),
  243. k: viewMode.value === "list",
  244. l: myStocks.value.length === 0 ? 1 : "",
  245. m: common_vendor.f(myStocks.value, (stock, index, i0) => {
  246. return {
  247. a: common_vendor.t(stock.name),
  248. b: common_vendor.t(getMarketTag(stock.code)),
  249. c: common_vendor.n(getMarketClass(stock.code)),
  250. d: common_vendor.t(stock.code),
  251. e: common_vendor.t(stock.addDate || "--"),
  252. f: common_vendor.t(formatPrice(stock.addPrice)),
  253. g: common_vendor.t(stock.profitPercent || "--"),
  254. h: common_vendor.n(getProfitClass(stock.profitPercent)),
  255. i: stock.code,
  256. j: common_vendor.o(($event) => handleStockClick(stock, index), stock.code)
  257. };
  258. }),
  259. n: viewMode.value === "table",
  260. o: myStocks.value.length === 0 ? 1 : "",
  261. p: myStocks.value.length === 0
  262. }, myStocks.value.length === 0 ? {} : {}, {
  263. q: !isLoggedIn.value ? 1 : "",
  264. r: !isLoggedIn.value
  265. }, !isLoggedIn.value ? {
  266. s: common_vendor.o(goToLogin)
  267. } : {});
  268. };
  269. }
  270. };
  271. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/rank/rank.vue"]]);
  272. wx.createPage(MiniProgramPage);