index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.o(handleSearchClick),
  230. f: showDropdown.value && suggestions.value && suggestions.value.length > 0
  231. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  232. g: common_vendor.f(suggestions.value, (item, index, i0) => {
  233. return {
  234. a: common_vendor.t(item.name),
  235. b: common_vendor.t(item.code),
  236. c: index,
  237. d: common_vendor.o(($event) => onSelectSuggestion(item), index)
  238. };
  239. })
  240. } : {}, {
  241. h: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"),
  242. i: showDropdown.value && suggestions.value.length > 0
  243. }, showDropdown.value && suggestions.value.length > 0 ? {
  244. j: common_vendor.o(closeDropdown)
  245. } : {}, {
  246. k: hasSearched.value
  247. }, hasSearched.value ? common_vendor.e({
  248. l: loading.value
  249. }, loading.value ? {} : errorMsg.value ? {
  250. n: common_vendor.t(errorMsg.value)
  251. } : result.value ? common_vendor.e({
  252. p: common_vendor.t(result.value.stockName),
  253. q: common_vendor.t(result.value.stockCode),
  254. r: common_vendor.t(result.value.currentPrice || "--"),
  255. s: common_vendor.n(getPriceClass(result.value.changePercent)),
  256. t: common_vendor.t(result.value.priceChange || "--"),
  257. v: common_vendor.t(result.value.changePercent || "--"),
  258. w: common_vendor.n(getPriceClass(result.value.changePercent)),
  259. x: result.value.currentPrice
  260. }, result.value.currentPrice ? {
  261. y: common_vendor.t(result.value.openPrice || "--"),
  262. z: common_vendor.t(result.value.highPrice || "--"),
  263. A: common_vendor.t(result.value.lowPrice || "--"),
  264. B: common_vendor.t(result.value.volume || "--"),
  265. C: common_vendor.t(result.value.amount || "--"),
  266. D: common_vendor.t(result.value.turnoverRate || "--")
  267. } : {}, {
  268. E: result.value.score
  269. }, result.value.score ? {
  270. F: common_vendor.t(result.value.score)
  271. } : {}, {
  272. G: hasSearched.value && result.value
  273. }, hasSearched.value && result.value ? common_vendor.e({
  274. H: common_vendor.o(onDateChange),
  275. I: historyData.value && historyData.value.recordDate
  276. }, historyData.value && historyData.value.recordDate ? {
  277. J: common_vendor.t(historyData.value.recordDate)
  278. } : {}, {
  279. K: !historyData.value || !historyData.value.strengthScore && !historyData.value.highTrend
  280. }, !historyData.value || !historyData.value.strengthScore && !historyData.value.highTrend ? {
  281. L: common_vendor.t(selectedDate.value ? "未找到该日期的历史数据" : "暂无历史数据")
  282. } : common_vendor.e({
  283. M: historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0
  284. }, historyData.value.strengthScore !== null && historyData.value.strengthScore !== void 0 ? {
  285. N: common_vendor.t(formatStrengthScore(historyData.value.strengthScore)),
  286. O: common_vendor.n(historyData.value.strengthScore >= 0 ? "capsule-up" : "capsule-down")
  287. } : {}, {
  288. P: historyData.value.closePrice
  289. }, historyData.value.closePrice ? {
  290. Q: common_vendor.t(historyData.value.closePrice)
  291. } : {}, {
  292. R: historyData.value.changePercent
  293. }, historyData.value.changePercent ? {
  294. S: common_vendor.t(formatPercent(historyData.value.changePercent)),
  295. T: common_vendor.n(getChangeClass(historyData.value.changePercent))
  296. } : {}, {
  297. U: historyData.value.totalAmount
  298. }, historyData.value.totalAmount ? {
  299. V: common_vendor.t(formatAmount(historyData.value.totalAmount))
  300. } : {}, {
  301. W: historyData.value.circulationMarketValue
  302. }, historyData.value.circulationMarketValue ? {
  303. X: common_vendor.t(formatAmount(historyData.value.circulationMarketValue))
  304. } : {}, {
  305. Y: historyData.value.mainRisePeriod
  306. }, historyData.value.mainRisePeriod ? {
  307. Z: common_vendor.t(historyData.value.mainRisePeriod)
  308. } : {}, {
  309. aa: historyData.value.recentLimitUp
  310. }, historyData.value.recentLimitUp ? {
  311. ab: common_vendor.t(historyData.value.recentLimitUp)
  312. } : {}, {
  313. ac: historyData.value.dayHighestPrice
  314. }, historyData.value.dayHighestPrice ? {
  315. ad: common_vendor.t(historyData.value.dayHighestPrice)
  316. } : {}, {
  317. ae: historyData.value.dayLowestPrice
  318. }, historyData.value.dayLowestPrice ? {
  319. af: common_vendor.t(historyData.value.dayLowestPrice)
  320. } : {}, {
  321. ag: historyData.value.dayClosePrice
  322. }, historyData.value.dayClosePrice ? {
  323. ah: common_vendor.t(historyData.value.dayClosePrice)
  324. } : {}, {
  325. ai: historyData.value.highTrend !== null && historyData.value.highTrend !== void 0
  326. }, historyData.value.highTrend !== null && historyData.value.highTrend !== void 0 ? {
  327. aj: common_vendor.t(formatPercent(historyData.value.highTrend)),
  328. ak: common_vendor.n(getChangeClass(historyData.value.highTrend))
  329. } : {})) : {}, {
  330. al: result.value.history && result.value.history.length > 0
  331. }, result.value.history && result.value.history.length > 0 ? {
  332. am: common_vendor.f(result.value.history, (item, index, i0) => {
  333. return {
  334. a: common_vendor.t(item.date),
  335. b: common_vendor.t(item.score),
  336. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  337. d: index
  338. };
  339. })
  340. } : {}, {
  341. an: result.value.factors && result.value.factors.length > 0
  342. }, result.value.factors && result.value.factors.length > 0 ? {
  343. ao: common_vendor.f(result.value.factors, (item, index, i0) => {
  344. return {
  345. a: common_vendor.t(item.name),
  346. b: common_vendor.t(item.value),
  347. c: index
  348. };
  349. })
  350. } : {}) : {}, {
  351. m: errorMsg.value,
  352. o: result.value
  353. }) : {});
  354. };
  355. }
  356. };
  357. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao/gupiao-wx/src/pages/index/index.vue"]]);
  358. wx.createPage(MiniProgramPage);