index.js 14 KB

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