index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. __name: "index",
  6. setup(__props) {
  7. const keyword = common_vendor.ref("");
  8. const loading = common_vendor.ref(false);
  9. const hasSearched = common_vendor.ref(false);
  10. const errorMsg = common_vendor.ref("");
  11. const result = common_vendor.ref(null);
  12. const suggestions = common_vendor.ref([]);
  13. const showDropdown = common_vendor.ref(false);
  14. let timer = null;
  15. const onKeywordChange = (e) => {
  16. const value = e.detail.value;
  17. keyword.value = value;
  18. console.log("输入关键词:", value);
  19. if (timer) {
  20. clearTimeout(timer);
  21. }
  22. timer = setTimeout(() => {
  23. doSearchSuggestions(value);
  24. }, 500);
  25. };
  26. const doSearchSuggestions = async (kw) => {
  27. if (!kw || !kw.trim()) {
  28. suggestions.value = [];
  29. showDropdown.value = false;
  30. return;
  31. }
  32. try {
  33. const list = await utils_api.getSuggestions(kw.trim());
  34. console.log("模糊查询返回数据:", list);
  35. suggestions.value = Array.isArray(list) ? list : [];
  36. showDropdown.value = suggestions.value.length > 0;
  37. console.log("下拉框状态:", { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length });
  38. } catch (err) {
  39. console.error("模糊查询错误:", err);
  40. suggestions.value = [];
  41. showDropdown.value = false;
  42. }
  43. };
  44. const onSelectSuggestion = (item) => {
  45. const searchText = `${item.name} (${item.code})`;
  46. keyword.value = searchText;
  47. suggestions.value = [];
  48. showDropdown.value = false;
  49. doSearch(item.code);
  50. };
  51. const onSearch = () => {
  52. const kw = (keyword.value || "").trim();
  53. if (!kw) {
  54. common_vendor.index.showToast({
  55. title: "请输入股票代码或名称",
  56. icon: "none"
  57. });
  58. return;
  59. }
  60. let searchCode = kw;
  61. const codeMatch = kw.match(/\((\d{6})\)/);
  62. if (codeMatch) {
  63. searchCode = codeMatch[1];
  64. }
  65. doSearch(searchCode);
  66. };
  67. const doSearch = async (queryCode) => {
  68. loading.value = true;
  69. hasSearched.value = true;
  70. errorMsg.value = "";
  71. result.value = null;
  72. suggestions.value = [];
  73. showDropdown.value = false;
  74. try {
  75. const res = await utils_api.searchStocks(queryCode);
  76. if (res.code === 0 && res.data) {
  77. result.value = res.data;
  78. } else {
  79. errorMsg.value = res.message || "未查询到相关股票数据";
  80. }
  81. } catch (err) {
  82. errorMsg.value = "网络请求失败,请检查网络连接";
  83. } finally {
  84. loading.value = false;
  85. }
  86. };
  87. const onInputBlur = () => {
  88. setTimeout(() => {
  89. showDropdown.value = false;
  90. }, 300);
  91. };
  92. return (_ctx, _cache) => {
  93. return common_vendor.e({
  94. a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]),
  95. b: common_vendor.o(onSearch),
  96. c: common_vendor.o(onInputBlur),
  97. d: keyword.value,
  98. e: common_vendor.o(onSearch),
  99. f: showDropdown.value && suggestions.value && suggestions.value.length > 0
  100. }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? {
  101. g: common_vendor.f(suggestions.value, (item, index, i0) => {
  102. return {
  103. a: common_vendor.t(item.name),
  104. b: common_vendor.t(item.code),
  105. c: index,
  106. d: common_vendor.o(($event) => onSelectSuggestion(item), index)
  107. };
  108. })
  109. } : {}, {
  110. h: hasSearched.value
  111. }, hasSearched.value ? common_vendor.e({
  112. i: loading.value
  113. }, loading.value ? {} : errorMsg.value ? {
  114. k: common_vendor.t(errorMsg.value)
  115. } : result.value ? {
  116. m: common_vendor.t(result.value.stockName),
  117. n: common_vendor.t(result.value.stockCode),
  118. o: common_vendor.t(result.value.score),
  119. p: common_vendor.f(result.value.history, (item, index, i0) => {
  120. return {
  121. a: common_vendor.t(item.date),
  122. b: common_vendor.t(item.score),
  123. c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"),
  124. d: index
  125. };
  126. }),
  127. q: common_vendor.f(result.value.factors, (item, index, i0) => {
  128. return {
  129. a: common_vendor.t(item.name),
  130. b: common_vendor.t(item.value),
  131. c: index
  132. };
  133. })
  134. } : {}, {
  135. j: errorMsg.value,
  136. l: result.value
  137. }) : {});
  138. };
  139. }
  140. };
  141. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/miniprogram-1/src/pages/index/index.vue"]]);
  142. wx.createPage(MiniProgramPage);