index.js 4.7 KB

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