rank.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. const localStocks = common_vendor.index.getStorageSync("my_stocks") || [];
  109. myStocks.value = localStocks;
  110. }
  111. await fetchIndexData();
  112. if (myStocks.value.length > 0) {
  113. await refreshAllQuotes();
  114. }
  115. startAutoRefresh();
  116. } catch (e) {
  117. console.error("加载股票列表失败:", e);
  118. const localStocks = common_vendor.index.getStorageSync("my_stocks") || [];
  119. myStocks.value = localStocks;
  120. startAutoRefresh();
  121. }
  122. };
  123. const refreshAllQuotes = async () => {
  124. if (myStocks.value.length === 0)
  125. return;
  126. try {
  127. const codes = myStocks.value.map((stock) => stock.code).join(",");
  128. const quoteRes = await utils_api.getStockQuotes(codes);
  129. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  130. quoteRes.data.forEach((quoteData) => {
  131. const index = myStocks.value.findIndex((stock) => stock.code === quoteData.stockCode);
  132. if (index !== -1) {
  133. const stock = myStocks.value[index];
  134. stock.priceChange = quoteData.priceChange;
  135. stock.changePercent = quoteData.changePercent;
  136. stock.currentPrice = quoteData.currentPrice;
  137. stock.name = quoteData.stockName || stock.name;
  138. stock.trendData = quoteData.trendData || null;
  139. if (stock.addPrice && quoteData.currentPrice) {
  140. const addPrice = parseFloat(stock.addPrice);
  141. const currentPrice = parseFloat(quoteData.currentPrice);
  142. if (addPrice > 0) {
  143. const profit = ((currentPrice - addPrice) / addPrice * 100).toFixed(2);
  144. stock.profitPercent = profit >= 0 ? `+${profit}%` : `${profit}%`;
  145. }
  146. }
  147. }
  148. });
  149. common_vendor.index.setStorageSync("my_stocks", myStocks.value);
  150. }
  151. } catch (e) {
  152. console.error("[我的股票] 刷新异常:", e.message);
  153. }
  154. };
  155. const startAutoRefresh = () => {
  156. if (!isLoggedIn.value)
  157. return;
  158. stopAutoRefresh();
  159. const scheduleNextRefresh = () => {
  160. const delay = 3e3 + Math.random() * 1e3;
  161. refreshTimer = setTimeout(async () => {
  162. await fetchIndexData();
  163. if (myStocks.value.length > 0) {
  164. await refreshAllQuotes();
  165. }
  166. scheduleNextRefresh();
  167. }, delay);
  168. };
  169. scheduleNextRefresh();
  170. };
  171. const stopAutoRefresh = () => {
  172. if (refreshTimer) {
  173. clearTimeout(refreshTimer);
  174. refreshTimer = null;
  175. }
  176. };
  177. const goToLogin = () => {
  178. common_vendor.index.navigateTo({ url: "/pages/login/login" });
  179. };
  180. const handleStockClick = (stockItem, idx) => {
  181. console.log("点击股票:", stockItem.name, idx);
  182. common_vendor.index.showToast({ title: "股票详情功能开发中", icon: "none" });
  183. };
  184. const removeStock = async (idx) => {
  185. const stock = myStocks.value[idx];
  186. common_vendor.index.showModal({
  187. title: "确认删除",
  188. content: `确定要删除 ${stock.name} 吗?`,
  189. confirmText: "删除",
  190. cancelText: "取消",
  191. success: async (res) => {
  192. if (res.confirm) {
  193. try {
  194. await utils_api.deleteUserStock(stock.code);
  195. } catch (e) {
  196. console.error("删除失败:", e);
  197. }
  198. myStocks.value.splice(idx, 1);
  199. common_vendor.index.setStorageSync("my_stocks", myStocks.value);
  200. common_vendor.index.showToast({ title: "删除成功", icon: "success" });
  201. if (myStocks.value.length === 0) {
  202. stopAutoRefresh();
  203. }
  204. }
  205. }
  206. });
  207. };
  208. common_vendor.onLoad(() => {
  209. isLoggedIn.value = utils_auth.isLoggedIn();
  210. loadMyStocks();
  211. });
  212. common_vendor.onShow(() => {
  213. isLoggedIn.value = utils_auth.isLoggedIn();
  214. loadMyStocks();
  215. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  216. });
  217. common_vendor.onHide(() => {
  218. stopAutoRefresh();
  219. });
  220. common_vendor.onUnload(() => {
  221. stopAutoRefresh();
  222. });
  223. return (_ctx, _cache) => {
  224. return common_vendor.e({
  225. a: common_vendor.t(formatIndexPrice(indexData.value.currentPrice)),
  226. b: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  227. c: common_vendor.t(indexData.value.priceChange || "--"),
  228. d: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  229. e: common_vendor.t(indexData.value.stockName || "上证指数"),
  230. f: common_vendor.t(indexData.value.changePercent || "--"),
  231. g: common_vendor.n(getIndexChangeClass(indexData.value.changePercent)),
  232. h: common_vendor.t(viewMode.value === "list" ? "📊" : "📋"),
  233. i: common_vendor.o(toggleViewMode),
  234. j: viewMode.value === "list" && myStocks.value.length > 0
  235. }, viewMode.value === "list" && myStocks.value.length > 0 ? {
  236. k: common_vendor.f(myStocks.value, (stock, index, i0) => {
  237. return {
  238. a: stock.code,
  239. b: common_vendor.o(($event) => removeStock(index), stock.code),
  240. c: common_vendor.o(($event) => handleStockClick(stock, index), stock.code),
  241. d: "2482292d-0-" + i0,
  242. e: common_vendor.p({
  243. stock,
  244. ["show-delete"]: true
  245. })
  246. };
  247. })
  248. } : {}, {
  249. l: viewMode.value === "table" && myStocks.value.length > 0
  250. }, viewMode.value === "table" && myStocks.value.length > 0 ? {
  251. m: common_vendor.f(myStocks.value, (stock, index, i0) => {
  252. return {
  253. a: common_vendor.t(stock.name),
  254. b: common_vendor.t(getMarketTag(stock.code)),
  255. c: common_vendor.n(getMarketClass(stock.code)),
  256. d: common_vendor.t(stock.code),
  257. e: common_vendor.t(stock.addDate || "--"),
  258. f: common_vendor.t(formatPrice(stock.addPrice)),
  259. g: common_vendor.t(stock.profitPercent || "--"),
  260. h: common_vendor.n(getProfitClass(stock.profitPercent)),
  261. i: stock.code,
  262. j: common_vendor.o(($event) => handleStockClick(stock, index), stock.code)
  263. };
  264. })
  265. } : {}, {
  266. n: myStocks.value.length === 0
  267. }, myStocks.value.length === 0 ? {} : {}, {
  268. o: !isLoggedIn.value ? 1 : "",
  269. p: !isLoggedIn.value
  270. }, !isLoggedIn.value ? {
  271. q: common_vendor.o(goToLogin)
  272. } : {});
  273. };
  274. }
  275. };
  276. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/rank/rank.vue"]]);
  277. wx.createPage(MiniProgramPage);