index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const utils_auth = require("../../utils/auth.js");
  5. const _sfc_main = {
  6. __name: "index",
  7. setup(__props) {
  8. const keyword = common_vendor.ref("");
  9. const loading = common_vendor.ref(false);
  10. const hasSearched = common_vendor.ref(false);
  11. const errorMsg = common_vendor.ref("");
  12. const result = common_vendor.ref(null);
  13. const historyData = common_vendor.ref(null);
  14. const suggestions = common_vendor.ref([]);
  15. const showDropdown = common_vendor.ref(false);
  16. const isLoggedIn = common_vendor.ref(false);
  17. let timer = null;
  18. common_vendor.onMounted(() => {
  19. isLoggedIn.value = utils_auth.isLoggedIn();
  20. console.log("[首页] 登录状态:", isLoggedIn.value);
  21. });
  22. common_vendor.onShow(() => {
  23. isLoggedIn.value = utils_auth.isLoggedIn();
  24. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  25. });
  26. const handleSearchClick = async () => {
  27. console.log("=== 点击搜索按钮 ===");
  28. console.log("当前登录状态:", isLoggedIn.value);
  29. if (!isLoggedIn.value) {
  30. console.log("未登录,跳转到登录页");
  31. common_vendor.index.showModal({
  32. title: "登录提示",
  33. content: "此功能需要登录后使用,是否前往登录?",
  34. confirmText: "去登录",
  35. cancelText: "取消",
  36. success: (res) => {
  37. if (res.confirm) {
  38. common_vendor.index.navigateTo({
  39. url: "/pages/login/login"
  40. });
  41. }
  42. }
  43. });
  44. return;
  45. }
  46. console.log("已登录,执行搜索");
  47. onSearch();
  48. };
  49. const onKeywordChange = (e) => {
  50. const value = e.detail.value;
  51. keyword.value = value;
  52. console.log("输入关键词:", value);
  53. if (timer) {
  54. clearTimeout(timer);
  55. }
  56. timer = setTimeout(() => {
  57. doSearchSuggestions(value);
  58. }, 500);
  59. };
  60. const doSearchSuggestions = async (kw) => {
  61. if (!kw || !kw.trim()) {
  62. suggestions.value = [];
  63. showDropdown.value = false;
  64. return;
  65. }
  66. try {
  67. const response = await utils_api.getSuggestions(kw.trim());
  68. console.log("模糊查询返回数据:", response);
  69. const list = response.code === 200 && response.data ? response.data : [];
  70. suggestions.value = Array.isArray(list) ? list : [];
  71. showDropdown.value = suggestions.value.length > 0;
  72. console.log("下拉框状态:", { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length });
  73. } catch (err) {
  74. console.error("模糊查询错误:", err);
  75. suggestions.value = [];
  76. showDropdown.value = false;
  77. }
  78. };
  79. const onSelectSuggestion = (item) => {
  80. const searchText = `${item.name} (${item.code})`;
  81. keyword.value = searchText;
  82. suggestions.value = [];
  83. showDropdown.value = false;
  84. doSearch(item.code);
  85. };
  86. const onSearch = () => {
  87. const kw = (keyword.value || "").trim();
  88. if (!kw) {
  89. common_vendor.index.showToast({
  90. title: "请输入股票代码或名称",
  91. icon: "none"
  92. });
  93. return;
  94. }
  95. let searchCode = kw;
  96. const codeMatch = kw.match(/\((\d{6})\)/);
  97. if (codeMatch) {
  98. searchCode = codeMatch[1];
  99. }
  100. doSearch(searchCode);
  101. };
  102. const doSearch = async (queryCode) => {
  103. loading.value = true;
  104. hasSearched.value = true;
  105. errorMsg.value = "";
  106. result.value = null;
  107. historyData.value = null;
  108. suggestions.value = [];
  109. showDropdown.value = false;
  110. try {
  111. const [stockRes, historyRes] = await Promise.all([
  112. utils_api.searchStocks(queryCode),
  113. utils_api.searchStockHistory(queryCode)
  114. ]);
  115. if (stockRes.code === 200 && stockRes.data) {
  116. result.value = stockRes.data;
  117. } else {
  118. errorMsg.value = stockRes.message || "未查询到相关股票数据";
  119. }
  120. if (historyRes.code === 200 && historyRes.data && historyRes.data.found) {
  121. historyData.value = historyRes.data;
  122. console.log("历史数据:", historyData.value);
  123. }
  124. } catch (err) {
  125. errorMsg.value = "网络请求失败,请检查网络连接";
  126. } finally {
  127. loading.value = false;
  128. }
  129. };
  130. const onInputBlur = () => {
  131. setTimeout(() => {
  132. showDropdown.value = false;
  133. }, 300);
  134. };
  135. const getPriceClass = (changePercent) => {
  136. if (!changePercent)
  137. return "";
  138. if (changePercent.startsWith("+"))
  139. return "price-up";
  140. if (changePercent.startsWith("-"))
  141. return "price-down";
  142. return "";
  143. };
  144. const getChangeClass = (value) => {
  145. if (value === null || value === void 0)
  146. return "";
  147. const num = parseFloat(value);
  148. if (num > 0)
  149. return "price-up";
  150. if (num < 0)
  151. return "price-down";
  152. return "";
  153. };
  154. const formatPercent = (value) => {
  155. if (value === null || value === void 0)
  156. return "--";
  157. const num = parseFloat(value);
  158. const prefix = num > 0 ? "+" : "";
  159. return `${prefix}${num.toFixed(2)}%`;
  160. };
  161. const formatAmount = (value) => {
  162. if (!value)
  163. return "--";
  164. const num = parseFloat(value);
  165. if (num >= 1e8) {
  166. return (num / 1e8).toFixed(2) + "亿";
  167. } else if (num >= 1e4) {
  168. return (num / 1e4).toFixed(2) + "万";
  169. }
  170. return num.toFixed(2);
  171. };
  172. const formatStrengthScore = (value) => {
  173. if (value === null || value === void 0)
  174. return "--";
  175. const num = parseFloat(value);
  176. const prefix = num >= 0 ? "+" : "";
  177. return `${prefix}${num.toFixed(2)}`;
  178. };
  179. return (_ctx, _cache) => {
  180. return common_vendor.e({
  181. a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]),
  182. b: common_vendor.o(handleSearchClick),
  183. c: common_vendor.o(onInputBlur),
  184. d: keyword.value,
  185. e: common_vendor.o(handleSearchClick),
  186. f: showDropdown.value && suggestions.value && suggestions.value.length > 0
  187. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  188. g: common_vendor.f(suggestions.value, (item, index, i0) => {
  189. return {
  190. a: common_vendor.t(item.name),
  191. b: common_vendor.t(item.code),
  192. c: index,
  193. d: common_vendor.o(($event) => onSelectSuggestion(item), index)
  194. };
  195. })
  196. } : {}, {
  197. h: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"),
  198. i: hasSearched.value
  199. }, hasSearched.value ? common_vendor.e({
  200. j: loading.value
  201. }, loading.value ? {} : errorMsg.value ? {
  202. l: common_vendor.t(errorMsg.value)
  203. } : result.value ? common_vendor.e({
  204. n: common_vendor.t(result.value.stockName),
  205. o: common_vendor.t(result.value.stockCode),
  206. p: common_vendor.t(result.value.currentPrice || "--"),
  207. q: common_vendor.n(getPriceClass(result.value.changePercent)),
  208. r: common_vendor.t(result.value.priceChange || "--"),
  209. s: common_vendor.t(result.value.changePercent || "--"),
  210. t: common_vendor.n(getPriceClass(result.value.changePercent)),
  211. v: result.value.currentPrice
  212. }, result.value.currentPrice ? {
  213. w: common_vendor.t(result.value.openPrice || "--"),
  214. x: common_vendor.t(result.value.highPrice || "--"),
  215. y: common_vendor.t(result.value.lowPrice || "--"),
  216. z: common_vendor.t(result.value.volume || "--"),
  217. A: common_vendor.t(result.value.amount || "--"),
  218. B: common_vendor.t(result.value.turnoverRate || "--")
  219. } : {}, {
  220. C: result.value.score
  221. }, result.value.score ? {
  222. D: common_vendor.t(result.value.score)
  223. } : {}, {
  224. E: historyData.value && (historyData.value.strengthScore !== null || historyData.value.highTrend !== null)
  225. }, historyData.value && (historyData.value.strengthScore !== null || historyData.value.highTrend !== null) ? common_vendor.e({
  226. F: common_vendor.t(historyData.value.recordDate),
  227. G: historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0
  228. }, historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0 ? {
  229. H: common_vendor.t(formatStrengthScore(historyData.value.strengthScore)),
  230. I: common_vendor.n(historyData.value.strengthScore >= 0 ? "capsule-up" : "capsule-down")
  231. } : {}, {
  232. J: historyData.value.closePrice
  233. }, historyData.value.closePrice ? {
  234. K: common_vendor.t(historyData.value.closePrice)
  235. } : {}, {
  236. L: historyData.value.changePercent
  237. }, historyData.value.changePercent ? {
  238. M: common_vendor.t(formatPercent(historyData.value.changePercent)),
  239. N: common_vendor.n(getChangeClass(historyData.value.changePercent))
  240. } : {}, {
  241. O: historyData.value.totalAmount
  242. }, historyData.value.totalAmount ? {
  243. P: common_vendor.t(formatAmount(historyData.value.totalAmount))
  244. } : {}, {
  245. Q: historyData.value.circulationMarketValue
  246. }, historyData.value.circulationMarketValue ? {
  247. R: common_vendor.t(formatAmount(historyData.value.circulationMarketValue))
  248. } : {}, {
  249. S: historyData.value.mainRisePeriod
  250. }, historyData.value.mainRisePeriod ? {
  251. T: common_vendor.t(historyData.value.mainRisePeriod)
  252. } : {}, {
  253. U: historyData.value.recentLimitUp
  254. }, historyData.value.recentLimitUp ? {
  255. V: common_vendor.t(historyData.value.recentLimitUp)
  256. } : {}, {
  257. W: historyData.value.highTrend !== null && historyData.value.highTrend !== void 0
  258. }, historyData.value.highTrend !== null && historyData.value.highTrend !== void 0 ? common_vendor.e({
  259. X: historyData.value.dayHighestPrice
  260. }, historyData.value.dayHighestPrice ? {
  261. Y: common_vendor.t(historyData.value.dayHighestPrice)
  262. } : {}, {
  263. Z: historyData.value.dayLowestPrice
  264. }, historyData.value.dayLowestPrice ? {
  265. aa: common_vendor.t(historyData.value.dayLowestPrice)
  266. } : {}, {
  267. ab: historyData.value.dayClosePrice
  268. }, historyData.value.dayClosePrice ? {
  269. ac: common_vendor.t(historyData.value.dayClosePrice)
  270. } : {}, {
  271. ad: common_vendor.t(formatPercent(historyData.value.highTrend)),
  272. ae: common_vendor.n(getChangeClass(historyData.value.highTrend))
  273. }) : {}) : {}, {
  274. af: result.value.history && result.value.history.length > 0
  275. }, result.value.history && result.value.history.length > 0 ? {
  276. ag: common_vendor.f(result.value.history, (item, index, i0) => {
  277. return {
  278. a: common_vendor.t(item.date),
  279. b: common_vendor.t(item.score),
  280. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  281. d: index
  282. };
  283. })
  284. } : {}, {
  285. ah: result.value.factors && result.value.factors.length > 0
  286. }, result.value.factors && result.value.factors.length > 0 ? {
  287. ai: common_vendor.f(result.value.factors, (item, index, i0) => {
  288. return {
  289. a: common_vendor.t(item.name),
  290. b: common_vendor.t(item.value),
  291. c: index
  292. };
  293. })
  294. } : {}) : {}, {
  295. k: errorMsg.value,
  296. m: result.value
  297. }) : {});
  298. };
  299. }
  300. };
  301. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/index/index.vue"]]);
  302. wx.createPage(MiniProgramPage);