rank.js 11 KB

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