index.js 7.5 KB

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