index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 suggestions = common_vendor.ref([]);
  14. const showDropdown = common_vendor.ref(false);
  15. const isLoggedIn = common_vendor.ref(false);
  16. let timer = null;
  17. common_vendor.onMounted(() => {
  18. isLoggedIn.value = utils_auth.isLoggedIn();
  19. console.log("[首页] 登录状态:", isLoggedIn.value);
  20. });
  21. common_vendor.onShow(() => {
  22. isLoggedIn.value = utils_auth.isLoggedIn();
  23. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  24. });
  25. const handleSearchClick = async () => {
  26. console.log("=== 点击搜索按钮 ===");
  27. console.log("当前登录状态:", isLoggedIn.value);
  28. if (!isLoggedIn.value) {
  29. console.log("未登录,跳转到登录页");
  30. common_vendor.index.showModal({
  31. title: "登录提示",
  32. content: "此功能需要登录后使用,是否前往登录?",
  33. confirmText: "去登录",
  34. cancelText: "取消",
  35. success: (res) => {
  36. if (res.confirm) {
  37. common_vendor.index.navigateTo({
  38. url: "/pages/login/login"
  39. });
  40. }
  41. }
  42. });
  43. return;
  44. }
  45. console.log("已登录,执行搜索");
  46. onSearch();
  47. };
  48. const onKeywordChange = (e) => {
  49. const value = e.detail.value;
  50. keyword.value = value;
  51. console.log("输入关键词:", value);
  52. if (timer) {
  53. clearTimeout(timer);
  54. }
  55. timer = setTimeout(() => {
  56. doSearchSuggestions(value);
  57. }, 500);
  58. };
  59. const doSearchSuggestions = async (kw) => {
  60. if (!kw || !kw.trim()) {
  61. suggestions.value = [];
  62. showDropdown.value = false;
  63. return;
  64. }
  65. try {
  66. const response = await utils_api.getSuggestions(kw.trim());
  67. console.log("模糊查询返回数据:", response);
  68. const list = response.code === 200 && response.data ? response.data : [];
  69. suggestions.value = Array.isArray(list) ? list : [];
  70. showDropdown.value = suggestions.value.length > 0;
  71. console.log("下拉框状态:", { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length });
  72. } catch (err) {
  73. console.error("模糊查询错误:", err);
  74. suggestions.value = [];
  75. showDropdown.value = false;
  76. }
  77. };
  78. const onSelectSuggestion = (item) => {
  79. const searchText = `${item.name} (${item.code})`;
  80. keyword.value = searchText;
  81. suggestions.value = [];
  82. showDropdown.value = false;
  83. doSearch(item.code);
  84. };
  85. const onSearch = () => {
  86. const kw = (keyword.value || "").trim();
  87. if (!kw) {
  88. common_vendor.index.showToast({
  89. title: "请输入股票代码或名称",
  90. icon: "none"
  91. });
  92. return;
  93. }
  94. let searchCode = kw;
  95. const codeMatch = kw.match(/\((\d{6})\)/);
  96. if (codeMatch) {
  97. searchCode = codeMatch[1];
  98. }
  99. doSearch(searchCode);
  100. };
  101. const doSearch = async (queryCode) => {
  102. loading.value = true;
  103. hasSearched.value = true;
  104. errorMsg.value = "";
  105. result.value = null;
  106. suggestions.value = [];
  107. showDropdown.value = false;
  108. try {
  109. const res = await utils_api.searchStocks(queryCode);
  110. if (res.code === 200 && res.data) {
  111. result.value = res.data;
  112. } else {
  113. errorMsg.value = res.message || "未查询到相关股票数据";
  114. }
  115. } catch (err) {
  116. errorMsg.value = "网络请求失败,请检查网络连接";
  117. } finally {
  118. loading.value = false;
  119. }
  120. };
  121. const onInputBlur = () => {
  122. setTimeout(() => {
  123. showDropdown.value = false;
  124. }, 300);
  125. };
  126. const getPriceClass = (changePercent) => {
  127. if (!changePercent)
  128. return "";
  129. if (changePercent.startsWith("+"))
  130. return "price-up";
  131. if (changePercent.startsWith("-"))
  132. return "price-down";
  133. return "";
  134. };
  135. return (_ctx, _cache) => {
  136. return common_vendor.e({
  137. a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]),
  138. b: common_vendor.o(handleSearchClick),
  139. c: common_vendor.o(onInputBlur),
  140. d: keyword.value,
  141. e: common_vendor.o(handleSearchClick),
  142. f: showDropdown.value && suggestions.value && suggestions.value.length > 0
  143. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  144. g: common_vendor.f(suggestions.value, (item, index, i0) => {
  145. return {
  146. a: common_vendor.t(item.name),
  147. b: common_vendor.t(item.code),
  148. c: index,
  149. d: common_vendor.o(($event) => onSelectSuggestion(item), index)
  150. };
  151. })
  152. } : {}, {
  153. h: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"),
  154. i: hasSearched.value
  155. }, hasSearched.value ? common_vendor.e({
  156. j: loading.value
  157. }, loading.value ? {} : errorMsg.value ? {
  158. l: common_vendor.t(errorMsg.value)
  159. } : result.value ? common_vendor.e({
  160. n: common_vendor.t(result.value.stockName),
  161. o: common_vendor.t(result.value.stockCode),
  162. p: common_vendor.t(result.value.currentPrice || "--"),
  163. q: common_vendor.n(getPriceClass(result.value.changePercent)),
  164. r: common_vendor.t(result.value.priceChange || "--"),
  165. s: common_vendor.t(result.value.changePercent || "--"),
  166. t: common_vendor.n(getPriceClass(result.value.changePercent)),
  167. v: result.value.currentPrice
  168. }, result.value.currentPrice ? {
  169. w: common_vendor.t(result.value.openPrice || "--"),
  170. x: common_vendor.t(result.value.highPrice || "--"),
  171. y: common_vendor.t(result.value.lowPrice || "--"),
  172. z: common_vendor.t(result.value.volume || "--"),
  173. A: common_vendor.t(result.value.amount || "--"),
  174. B: common_vendor.t(result.value.turnoverRate || "--")
  175. } : {}, {
  176. C: result.value.score
  177. }, result.value.score ? {
  178. D: common_vendor.t(result.value.score)
  179. } : {}, {
  180. E: result.value.history && result.value.history.length > 0
  181. }, result.value.history && result.value.history.length > 0 ? {
  182. F: common_vendor.f(result.value.history, (item, index, i0) => {
  183. return {
  184. a: common_vendor.t(item.date),
  185. b: common_vendor.t(item.score),
  186. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  187. d: index
  188. };
  189. })
  190. } : {}, {
  191. G: result.value.factors && result.value.factors.length > 0
  192. }, result.value.factors && result.value.factors.length > 0 ? {
  193. H: common_vendor.f(result.value.factors, (item, index, i0) => {
  194. return {
  195. a: common_vendor.t(item.name),
  196. b: common_vendor.t(item.value),
  197. c: index
  198. };
  199. })
  200. } : {}) : {}, {
  201. k: errorMsg.value,
  202. m: result.value
  203. }) : {});
  204. };
  205. }
  206. };
  207. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/index/index.vue"]]);
  208. wx.createPage(MiniProgramPage);