index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 closeDropdown = () => {
  176. showDropdown.value = false;
  177. suggestions.value = [];
  178. };
  179. const getPriceClass = (changePercent) => {
  180. if (!changePercent)
  181. return "";
  182. if (changePercent.startsWith("+"))
  183. return "price-up";
  184. if (changePercent.startsWith("-"))
  185. return "price-down";
  186. return "";
  187. };
  188. const getChangeClass = (value) => {
  189. if (value === null || value === void 0)
  190. return "";
  191. const num = parseFloat(value);
  192. if (num > 0)
  193. return "price-up";
  194. if (num < 0)
  195. return "price-down";
  196. return "";
  197. };
  198. const formatPercent = (value) => {
  199. if (value === null || value === void 0)
  200. return "--";
  201. const num = parseFloat(value);
  202. const prefix = num > 0 ? "+" : "";
  203. return `${prefix}${num.toFixed(2)}%`;
  204. };
  205. const formatAmount = (value) => {
  206. if (!value)
  207. return "--";
  208. const num = parseFloat(value);
  209. if (num >= 1e8) {
  210. return (num / 1e8).toFixed(2) + "亿";
  211. } else if (num >= 1e4) {
  212. return (num / 1e4).toFixed(2) + "万";
  213. }
  214. return num.toFixed(2);
  215. };
  216. const formatStrengthScore = (value) => {
  217. if (value === null || value === void 0)
  218. return "--";
  219. const num = parseFloat(value);
  220. const prefix = num >= 0 ? "+" : "";
  221. return `${prefix}${num.toFixed(2)}`;
  222. };
  223. return (_ctx, _cache) => {
  224. return common_vendor.e({
  225. a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]),
  226. b: common_vendor.o(handleSearchClick),
  227. c: common_vendor.o(($event) => showDropdown.value = true),
  228. d: keyword.value,
  229. e: common_vendor.unref(utils_api.getImageUrl)("/static/images/tab_search.png"),
  230. f: common_vendor.o(handleSearchClick),
  231. g: showDropdown.value && suggestions.value && suggestions.value.length > 0
  232. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  233. h: 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. i: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"),
  243. j: showDropdown.value && suggestions.value.length > 0
  244. }, showDropdown.value && suggestions.value.length > 0 ? {
  245. k: common_vendor.o(closeDropdown)
  246. } : {}, {
  247. l: hasSearched.value
  248. }, hasSearched.value ? common_vendor.e({
  249. m: loading.value
  250. }, loading.value ? {} : errorMsg.value ? {
  251. o: common_vendor.t(errorMsg.value)
  252. } : result.value ? common_vendor.e({
  253. q: common_vendor.t(result.value.stockName),
  254. r: common_vendor.t(result.value.stockCode),
  255. s: common_vendor.t(result.value.currentPrice || "--"),
  256. t: common_vendor.n(getPriceClass(result.value.changePercent)),
  257. v: common_vendor.t(result.value.priceChange || "--"),
  258. w: common_vendor.t(result.value.changePercent || "--"),
  259. x: common_vendor.n(getPriceClass(result.value.changePercent)),
  260. y: result.value.currentPrice
  261. }, result.value.currentPrice ? {
  262. z: common_vendor.t(result.value.openPrice || "--"),
  263. A: common_vendor.t(result.value.highPrice || "--"),
  264. B: common_vendor.t(result.value.lowPrice || "--"),
  265. C: common_vendor.t(result.value.volume || "--"),
  266. D: common_vendor.t(result.value.amount || "--"),
  267. E: common_vendor.t(result.value.turnoverRate || "--")
  268. } : {}, {
  269. F: result.value.score
  270. }, result.value.score ? {
  271. G: common_vendor.t(result.value.score)
  272. } : {}, {
  273. H: hasSearched.value && result.value
  274. }, hasSearched.value && result.value ? common_vendor.e({
  275. I: common_vendor.o(onDateChange),
  276. J: historyData.value && historyData.value.recordDate
  277. }, historyData.value && historyData.value.recordDate ? {
  278. K: common_vendor.t(historyData.value.recordDate)
  279. } : {}, {
  280. L: !historyData.value || !historyData.value.strengthScore && !historyData.value.highTrend
  281. }, !historyData.value || !historyData.value.strengthScore && !historyData.value.highTrend ? {
  282. M: common_vendor.t(selectedDate.value ? "未找到该日期的历史数据" : "暂无历史数据")
  283. } : common_vendor.e({
  284. N: historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0
  285. }, historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0 ? {
  286. O: common_vendor.t(formatStrengthScore(historyData.value.strengthScore)),
  287. P: common_vendor.n(historyData.value.strengthScore >= 0 ? "capsule-up" : "capsule-down")
  288. } : {}, {
  289. Q: historyData.value.closePrice
  290. }, historyData.value.closePrice ? {
  291. R: common_vendor.t(historyData.value.closePrice)
  292. } : {}, {
  293. S: historyData.value.changePercent
  294. }, historyData.value.changePercent ? {
  295. T: common_vendor.t(formatPercent(historyData.value.changePercent)),
  296. U: common_vendor.n(getChangeClass(historyData.value.changePercent))
  297. } : {}, {
  298. V: historyData.value.totalAmount
  299. }, historyData.value.totalAmount ? {
  300. W: common_vendor.t(formatAmount(historyData.value.totalAmount))
  301. } : {}, {
  302. X: historyData.value.circulationMarketValue
  303. }, historyData.value.circulationMarketValue ? {
  304. Y: common_vendor.t(formatAmount(historyData.value.circulationMarketValue))
  305. } : {}, {
  306. Z: historyData.value.mainRisePeriod
  307. }, historyData.value.mainRisePeriod ? {
  308. aa: common_vendor.t(historyData.value.mainRisePeriod)
  309. } : {}, {
  310. ab: historyData.value.recentLimitUp
  311. }, historyData.value.recentLimitUp ? {
  312. ac: common_vendor.t(historyData.value.recentLimitUp)
  313. } : {}, {
  314. ad: historyData.value.dayHighestPrice
  315. }, historyData.value.dayHighestPrice ? {
  316. ae: common_vendor.t(historyData.value.dayHighestPrice)
  317. } : {}, {
  318. af: historyData.value.dayLowestPrice
  319. }, historyData.value.dayLowestPrice ? {
  320. ag: common_vendor.t(historyData.value.dayLowestPrice)
  321. } : {}, {
  322. ah: historyData.value.dayClosePrice
  323. }, historyData.value.dayClosePrice ? {
  324. ai: common_vendor.t(historyData.value.dayClosePrice)
  325. } : {}, {
  326. aj: historyData.value.highTrend !== null && historyData.value.highTrend !== void 0
  327. }, historyData.value.highTrend !== null && historyData.value.highTrend !== void 0 ? {
  328. ak: common_vendor.t(formatPercent(historyData.value.highTrend)),
  329. al: common_vendor.n(getChangeClass(historyData.value.highTrend))
  330. } : {})) : {}, {
  331. am: result.value.history && result.value.history.length > 0
  332. }, result.value.history && result.value.history.length > 0 ? {
  333. an: common_vendor.f(result.value.history, (item, index, i0) => {
  334. return {
  335. a: common_vendor.t(item.date),
  336. b: common_vendor.t(item.score),
  337. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  338. d: index
  339. };
  340. })
  341. } : {}, {
  342. ao: result.value.factors && result.value.factors.length > 0
  343. }, result.value.factors && result.value.factors.length > 0 ? {
  344. ap: common_vendor.f(result.value.factors, (item, index, i0) => {
  345. return {
  346. a: common_vendor.t(item.name),
  347. b: common_vendor.t(item.value),
  348. c: index
  349. };
  350. })
  351. } : {}) : {}, {
  352. n: errorMsg.value,
  353. p: result.value
  354. }) : {});
  355. };
  356. }
  357. };
  358. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/index/index.vue"]]);
  359. wx.createPage(MiniProgramPage);