自己的网页加一个搜索框,调用deepseek的API-前端实现

时间:2025-03-06 12:11:29

前端使用vue实现的

首先是整体页面:AIWidget.vue

<template>
  <div>
    <!-- 悬浮 AI 按钮 -->
    <el-button class="floating-button" @click="dialogVisible = true">
      <el-icon><Search /></el-icon>
    </el-button>

    <!-- AI 搜索框 -->
    <el-dialog v-model="dialogVisible" title="AI 搜索" width="400px">
      <el-input
        v-model="query"
        placeholder="请输入搜索内容..."
        @keyup.enter="handleSearch"
      />
      <el-button type="primary" class="search-btn" @click="handleSearch">搜索</el-button>

      <el-divider />

      <el-scrollbar height="200px">
        <ul v-if="results.length">
          <li v-for="(item, index) in results" :key="index">{{ item }}</li>
        </ul>
        <p v-else class="no-result">暂无搜索结果</p>
      </el-scrollbar>
    </el-dialog>
  </div>
</template>

<script>
import { ref, watch } from "vue";
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { aiSearch } from "@/api/aiSearch";
import { debounce } from "lodash-es";

export default {
  components: { Search },
  setup() {
    const dialogVisible = ref(false);
    const query = ref("");
    const results = ref([]);
    const loading = ref(false);

    const handleSearch = debounce(async () => {
      const cleanQuery = query.value.trim().replace(/<[^>]*>?/gm, "");
      if (!cleanQuery) return;
      
      loading.value = true;
      try {
        const response = await aiSearch(cleanQuery);
        results.value = response.data || [];
      } catch (error) {
        ElMessage.error("搜索失败:" + error.message);
        results.value = [];
      } finally {
        loading.value = false;
      }
    }, 500);

    watch(dialogVisible, (val) => {
      if (!val) {
        query.value = "";
        results.value = [];
      }
    });

    return { dialogVisible, query, results, loading, handleSearch };
  },
};
</script>

<style scoped>
.floating-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 50px;
  height: 50px;
  font-size: 20px;
  border-radius: 50%;
  background-color: #409eff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: background-color 0.3s;
}

.floating-button:hover {
  background-color: #66b1ff;
}

.search-btn {
  margin-top: 10px;
  width: 100%;
}

.no-result {
  text-align: center;
  color: gray;
}
</style>

直接放在src/commpoents
接下来是JS文件,直接放在 src/api/
aiSearch.js

import request from '@/utils/request';

// AI 搜索 API 请求
export function aiSearch(query) {
  return request({
    url: '/ai-search',
    method: 'get',
    params: { q: query }
  });
}

为了在每个页面的右下角都显示这个搜索框,我们直接导入组件到App.vue
App.vue

<template>
   <div>
    <router-view />
    <AIWidget /> <!-- 悬浮 AI 按钮 -->
  </div>

</template>

<script setup>
import useSettingsStore from '@/store/modules/settings'
import { handleThemeStyle } from '@/utils/theme'
import AIWidget from "@/components/AIWidget.vue"; // 导入 AI 搜索组件

onMounted(() => {
  nextTick(() => {
    // 初始化主题样式
    handleThemeStyle(useSettingsStore().theme)
  })
})

</script>