index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <div class="search-results">
  3. <div class="search-box">
  4. <div class="input-wrapper">
  5. <input
  6. v-model="searchQuery"
  7. type="text"
  8. placeholder="Enter your search query"
  9. /><!-- @keyup.enter="handleSearch" -->
  10. <span
  11. v-if="searchQuery"
  12. class="clear-icon"
  13. @click="clearSearch"
  14. >×</span>
  15. </div>
  16. <button @click="handleSearch" :disabled="isSearching">
  17. 搜索一下
  18. </button>
  19. </div>
  20. <div class="results-header">
  21. <h2>Related Search Results</h2>
  22. <div class="result-stats">
  23. Found {{ total }} results
  24. </div>
  25. </div>
  26. <div class="results-container" ref="resultsContainer" @scroll="handleScroll">
  27. <!-- 搜索结果列表 -->
  28. <div v-if="displayResults.length > 0" class="results-list">
  29. <div v-for="(item, index) in displayResults" :key="index" class="result-item">
  30. <h3 class="result-title">
  31. <a :href="item.link" target="_blank" rel="noopener noreferrer">{{ item.title }}</a>
  32. </h3>
  33. <p class="result-url">{{ item.link }}</p>
  34. <p class="result-snippet">{{ item.snippet }}</p>
  35. <div class="result-meta">
  36. <span class="result-date">{{ formatDate(item.date) }}</span>
  37. <span class="result-source">{{ item.source }}</span>
  38. </div>
  39. </div>
  40. <!-- 加载更多指示器 -->
  41. <div v-if="hasMore" class="loading-more" ref="loadingTrigger">
  42. <div v-if="loading" class="loading-spinner"></div>
  43. <span v-else>Loading more results...</span>
  44. </div>
  45. </div>
  46. <!-- 无结果显示 -->
  47. <div v-else class="no-results">
  48. <FileSearchOutlined class="no-results-icon" />
  49. <p>No related results found</p>
  50. </div>
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. import { defineComponent, ref, watch } from 'vue';
  56. import { FileSearchOutlined } from '@ant-design/icons-vue';
  57. import dayjs from 'dayjs';
  58. import axios from 'axios';
  59. export default defineComponent({
  60. name: 'SearchResults',
  61. components: {
  62. FileSearchOutlined
  63. },
  64. props: {
  65. searchResults: {
  66. type: Array,
  67. default: () => []
  68. },
  69. loading: {
  70. type: Boolean,
  71. default: false
  72. },
  73. total: {
  74. type: Number,
  75. default: 0
  76. },
  77. message: {
  78. type: String,
  79. default: ''
  80. },
  81. enableWebSearch: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. },
  86. emits: ['update:total', 'update:enableWebSearch'],
  87. setup(props, { emit }) {
  88. const displayResults = ref([]);
  89. const hasMore = ref(false);
  90. const searchQuery = ref('');
  91. const isSearching = ref(false);
  92. const localTotal = ref(props.total);
  93. const isSearchEnabled = ref(props.enableWebSearch);
  94. // 监听props中的total变化
  95. watch(() => props.total, (newTotal) => {
  96. localTotal.value = newTotal;
  97. });
  98. // 监听 searchResults 变化
  99. watch(() => props.searchResults, (newResults) => {
  100. displayResults.value = newResults;
  101. hasMore.value = displayResults.value.length < props.total;
  102. }, { immediate: true });
  103. // 监听 message prop 的变化
  104. watch(() => props.message, (newMessage) => {
  105. if (newMessage && props.enableWebSearch) {
  106. searchQuery.value = newMessage;
  107. handleSearch(); // 自动触发搜索
  108. }
  109. });
  110. // 添加监听器
  111. watch(
  112. () => props.enableWebSearch,
  113. (newVal) => {
  114. isSearchEnabled.value = newVal;
  115. if (!newVal) {
  116. // 当搜索被关闭时,清空搜索结果
  117. clearSearch();
  118. }
  119. }
  120. );
  121. // 格式化日期
  122. const formatDate = (date) => {
  123. return dayjs(date).format('YYYY-MM-DD');
  124. };
  125. // 更新搜索处理函数
  126. const handleSearch = async () => {
  127. if (!searchQuery.value.trim() || isSearching.value || !props.enableWebSearch) return;
  128. isSearching.value = true;
  129. try {
  130. const response = await axios.post(
  131. `${import.meta.env.VITE_BASE_AI_API}/api/web-search-results/`,
  132. {
  133. query: searchQuery.value,
  134. num_results: 20,
  135. page: 1,
  136. page_size: 20,
  137. engine: "bing",
  138. }
  139. );
  140. // 添加调试日志
  141. console.log('API Response:', response.data);
  142. if (response.data && response.data.code === 200) {
  143. displayResults.value = response.data.data.result || [];
  144. // 添加调试日志
  145. console.log('Total Results:', response.data.data.total);
  146. localTotal.value = response.data.data.total || 0;
  147. emit('update:total', response.data.data.total || 0);
  148. }
  149. } catch (error) {
  150. console.error('Web search failed:', error);
  151. displayResults.value = [];
  152. localTotal.value = 0;
  153. emit('update:total', 0);
  154. } finally {
  155. isSearching.value = false;
  156. }
  157. };
  158. // 清空搜索函数
  159. const clearSearch = () => {
  160. searchQuery.value = '';
  161. displayResults.value = [];
  162. localTotal.value = 0;
  163. emit('update:total', 0);
  164. };
  165. const toggleSearch = () => {
  166. isSearchEnabled.value = !isSearchEnabled.value;
  167. emit('update:enableWebSearch', isSearchEnabled.value);
  168. };
  169. return {
  170. displayResults,
  171. loading: props.loading,
  172. total: localTotal,
  173. hasMore,
  174. formatDate,
  175. searchQuery,
  176. isSearching,
  177. handleSearch,
  178. clearSearch,
  179. isSearchEnabled,
  180. toggleSearch,
  181. };
  182. }
  183. });
  184. </script>
  185. <style lang="less" scoped>
  186. .search-results {
  187. height: 100%;
  188. display: flex;
  189. flex-direction: column;
  190. background: var(--secondary-bg);
  191. width: 100%;
  192. overflow: hidden;
  193. .search-box {
  194. padding: 16px 24px;
  195. border-bottom: 1px solid var(--border-color);
  196. display: flex;
  197. gap: 0;
  198. .input-wrapper {
  199. position: relative;
  200. flex: 1;
  201. display: flex;
  202. align-items: center;
  203. input {
  204. width: 100%;
  205. padding: 8px 30px 8px 12px;
  206. height: 24px;
  207. font-size: 16px;
  208. border: 2px solid #4e6ef2;
  209. border-right: none;
  210. border-radius: 4px 0 0 4px;
  211. &:focus {
  212. outline: none;
  213. }
  214. }
  215. .clear-icon {
  216. position: absolute;
  217. right: 8px;
  218. top: 50%;
  219. transform: translateY(-50%);
  220. cursor: pointer;
  221. color: #999;
  222. font-size: 16px;
  223. width: 16px;
  224. height: 16px;
  225. display: flex;
  226. align-items: center;
  227. justify-content: center;
  228. border-radius: 50%;
  229. &:hover {
  230. color: #666;
  231. background-color: rgba(0, 0, 0, 0.05);
  232. }
  233. }
  234. }
  235. button {
  236. padding: 0 20px;
  237. height: 44px;
  238. background: #4e6ef2;
  239. border: none;
  240. border-radius: 0 4px 4px 0;
  241. color: white;
  242. cursor: pointer;
  243. font-size: 16px;
  244. min-width: 108px;
  245. &:disabled {
  246. background: #7a96fa;
  247. cursor: not-allowed;
  248. }
  249. &:hover:not(:disabled) {
  250. background: #4662d9;
  251. }
  252. }
  253. }
  254. .results-header {
  255. padding: 16px 24px;
  256. border-bottom: 1px solid var(--border-color);
  257. h2 {
  258. margin: 0;
  259. font-size: 18px;
  260. color: var(--primary-text);
  261. }
  262. .result-stats {
  263. margin-top: 4px;
  264. font-size: 14px;
  265. color: var(--primary-text);
  266. }
  267. }
  268. .results-container {
  269. flex: 1;
  270. overflow-y: auto;
  271. padding: 0 24px;
  272. scroll-behavior: smooth;
  273. }
  274. .results-list {
  275. .result-item {
  276. padding: 20px 0;
  277. border-bottom: 1px solid #f0f0f0;
  278. &:last-child {
  279. border-bottom: none;
  280. }
  281. .result-title {
  282. cursor: pointer;
  283. margin: 0 0 8px;
  284. font-size: 16px;
  285. a {
  286. color: #1677ff;
  287. text-decoration: none;
  288. &:hover {
  289. text-decoration: underline;
  290. }
  291. }
  292. }
  293. .result-url {
  294. color: #0ca678;
  295. font-size: 13px;
  296. margin-bottom: 8px;
  297. }
  298. .result-snippet {
  299. color: rgba(0, 0, 0, 0.65);
  300. font-size: 14px;
  301. margin-bottom: 8px;
  302. line-height: 1.5;
  303. }
  304. .result-meta {
  305. font-size: 12px;
  306. color: rgba(0, 0, 0, 0.45);
  307. .result-date {
  308. margin-right: 16px;
  309. }
  310. }
  311. }
  312. }
  313. .no-results {
  314. text-align: center;
  315. padding: 48px 0;
  316. color: var(--primary-text);
  317. .no-results-icon {
  318. font-size: 48px;
  319. margin-bottom: 16px;
  320. }
  321. p {
  322. font-size: 14px;
  323. }
  324. }
  325. .loading-more {
  326. text-align: center;
  327. padding: 20px 0;
  328. color: var(--primary-text);
  329. font-size: 14px;
  330. }
  331. .loading-spinner {
  332. display: inline-block;
  333. width: 20px;
  334. height: 20px;
  335. border: 2px solid #f3f3f3;
  336. border-top: 2px solid #1677ff;
  337. border-radius: 50%;
  338. animation: spin 1s linear infinite;
  339. }
  340. @keyframes spin {
  341. 0% { transform: rotate(0deg); }
  342. 100% { transform: rotate(360deg); }
  343. }
  344. // 添加滚动条样式
  345. .results-container::-webkit-scrollbar {
  346. width: 6px;
  347. }
  348. .results-container::-webkit-scrollbar-thumb {
  349. background-color: rgba(0, 0, 0, 0.2);
  350. border-radius: 3px;
  351. }
  352. .results-container::-webkit-scrollbar-track {
  353. background-color: transparent;
  354. }
  355. }
  356. </style>