index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. console.log("[首页] Token:", common_vendor.index.getStorageSync("user_token"));
  21. });
  22. common_vendor.onShow(() => {
  23. isLoggedIn.value = utils_auth.isLoggedIn();
  24. console.log("[首页] 登录状态:", isLoggedIn.value);
  25. });
  26. const handleSearchClick = () => {
  27. console.log("=== 点击搜索按钮 ===");
  28. console.log("当前登录状态:", isLoggedIn.value);
  29. if (!isLoggedIn.value) {
  30. console.log("未登录,显示微信授权弹窗");
  31. utils_auth.checkLogin(() => {
  32. onSearch();
  33. });
  34. return;
  35. }
  36. console.log("已登录,执行搜索");
  37. onSearch();
  38. };
  39. const onKeywordChange = (e) => {
  40. const value = e.detail.value;
  41. keyword.value = value;
  42. console.log("输入关键词:", value);
  43. if (timer) {
  44. clearTimeout(timer);
  45. }
  46. timer = setTimeout(() => {
  47. doSearchSuggestions(value);
  48. }, 500);
  49. };
  50. const doSearchSuggestions = async (kw) => {
  51. if (!kw || !kw.trim()) {
  52. suggestions.value = [];
  53. showDropdown.value = false;
  54. return;
  55. }
  56. try {
  57. const response = await utils_api.getSuggestions(kw.trim());
  58. console.log("模糊查询返回数据:", response);
  59. const list = response.data || [];
  60. suggestions.value = Array.isArray(list) ? list : [];
  61. showDropdown.value = suggestions.value.length > 0;
  62. console.log("下拉框状态:", { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length });
  63. } catch (err) {
  64. console.error("模糊查询错误:", err);
  65. suggestions.value = [];
  66. showDropdown.value = false;
  67. }
  68. };
  69. const onSelectSuggestion = (item) => {
  70. const searchText = `${item.name} (${item.code})`;
  71. keyword.value = searchText;
  72. suggestions.value = [];
  73. showDropdown.value = false;
  74. doSearch(item.code);
  75. };
  76. const onSearch = () => {
  77. const kw = (keyword.value || "").trim();
  78. if (!kw) {
  79. common_vendor.index.showToast({
  80. title: "请输入股票代码或名称",
  81. icon: "none"
  82. });
  83. return;
  84. }
  85. let searchCode = kw;
  86. const codeMatch = kw.match(/\((\d{6})\)/);
  87. if (codeMatch) {
  88. searchCode = codeMatch[1];
  89. }
  90. doSearch(searchCode);
  91. };
  92. const doSearch = async (queryCode) => {
  93. loading.value = true;
  94. hasSearched.value = true;
  95. errorMsg.value = "";
  96. result.value = null;
  97. suggestions.value = [];
  98. showDropdown.value = false;
  99. try {
  100. const res = await utils_api.searchStocks(queryCode);
  101. if (res.code === 0 && res.data) {
  102. result.value = res.data;
  103. } else {
  104. errorMsg.value = res.message || "未查询到相关股票数据";
  105. }
  106. } catch (err) {
  107. errorMsg.value = "网络请求失败,请检查网络连接";
  108. } finally {
  109. loading.value = false;
  110. }
  111. };
  112. const onInputBlur = () => {
  113. setTimeout(() => {
  114. showDropdown.value = false;
  115. }, 300);
  116. };
  117. return (_ctx, _cache) => {
  118. return common_vendor.e({
  119. a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]),
  120. b: common_vendor.o(onSearch),
  121. c: common_vendor.o(onInputBlur),
  122. d: keyword.value,
  123. e: common_vendor.o(handleSearchClick),
  124. f: showDropdown.value && suggestions.value && suggestions.value.length > 0
  125. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  126. g: common_vendor.f(suggestions.value, (item, index, i0) => {
  127. return {
  128. a: common_vendor.t(item.name),
  129. b: common_vendor.t(item.code),
  130. c: index,
  131. d: common_vendor.o(($event) => onSelectSuggestion(item), index)
  132. };
  133. })
  134. } : {}, {
  135. h: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"),
  136. i: hasSearched.value
  137. }, hasSearched.value ? common_vendor.e({
  138. j: loading.value
  139. }, loading.value ? {} : errorMsg.value ? {
  140. l: common_vendor.t(errorMsg.value)
  141. } : result.value ? {
  142. n: common_vendor.t(result.value.stockName),
  143. o: common_vendor.t(result.value.stockCode),
  144. p: common_vendor.t(result.value.score),
  145. q: common_vendor.f(result.value.history, (item, index, i0) => {
  146. return {
  147. a: common_vendor.t(item.date),
  148. b: common_vendor.t(item.score),
  149. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  150. d: index
  151. };
  152. }),
  153. r: common_vendor.f(result.value.factors, (item, index, i0) => {
  154. return {
  155. a: common_vendor.t(item.name),
  156. b: common_vendor.t(item.value),
  157. c: index
  158. };
  159. })
  160. } : {}, {
  161. k: errorMsg.value,
  162. m: result.value
  163. }) : {});
  164. };
  165. }
  166. };
  167. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/index/index.vue"]]);
  168. wx.createPage(MiniProgramPage);