Răsfoiți Sursa

AI 助手改为接口请求方式

- 修改 AI 助手组件,使用 API 调用替代本地回复
- 新增 /upms/ai/chat 接口
- 添加 aiApi 工具函数
- mock 数据添加 AI 对话知识库

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tanlie 3 săptămâni în urmă
părinte
comite
aff4206793
5 a modificat fișierele cu 84 adăugiri și 33 ștergeri
  1. 16 0
      package-lock.json
  2. 1 0
      package.json
  3. 40 0
      src/mock/index.ts
  4. 8 0
      src/utils/api.ts
  5. 19 33
      src/views/ai-chat/index.vue

+ 16 - 0
package-lock.json

@@ -16,6 +16,7 @@
         "vue-router": "^4.2.0"
       },
       "devDependencies": {
+        "@types/node": "^25.5.0",
         "@vitejs/plugin-vue": "^5.0.0",
         "typescript": "^5.3.0",
         "vite": "^5.0.0",
@@ -865,6 +866,15 @@
         "@types/lodash": "*"
       }
     },
+    "node_modules/@types/node": {
+      "version": "25.5.0",
+      "resolved": "https://registry.npmmirror.com/@types/node/-/node-25.5.0.tgz",
+      "integrity": "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw==",
+      "dev": true,
+      "dependencies": {
+        "undici-types": "~7.18.0"
+      }
+    },
     "node_modules/@types/web-bluetooth": {
       "version": "0.0.20",
       "resolved": "https://registry.npmmirror.com/@types/web-bluetooth/-/web-bluetooth-0.0.20.tgz",
@@ -2138,6 +2148,12 @@
         "node": ">=14.17"
       }
     },
+    "node_modules/undici-types": {
+      "version": "7.18.2",
+      "resolved": "https://registry.npmmirror.com/undici-types/-/undici-types-7.18.2.tgz",
+      "integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==",
+      "dev": true
+    },
     "node_modules/unpipe": {
       "version": "1.0.0",
       "resolved": "https://registry.npmmirror.com/unpipe/-/unpipe-1.0.0.tgz",

+ 1 - 0
package.json

@@ -17,6 +17,7 @@
     "vue-router": "^4.2.0"
   },
   "devDependencies": {
+    "@types/node": "^25.5.0",
     "@vitejs/plugin-vue": "^5.0.0",
     "typescript": "^5.3.0",
     "vite": "^5.0.0",

+ 40 - 0
src/mock/index.ts

@@ -152,4 +152,44 @@ export default [
       };
     },
   },
+
+  // AI 对话接口
+  {
+    url: "/api/upms/ai/chat",
+    method: "post",
+    response: ({ body }) => {
+      const { question } = body;
+
+      // AI 回复知识库
+      const knowledgeBase: Record<string, string> = {
+        "如何管理系统用户?": "系统用户管理在「用户管理」菜单中,您可以进行以下操作:\n1. 查看用户列表\n2. 添加新用户\n3. 编辑用户信息\n4. 禁用/启用用户账号",
+        "如何配置菜单权限?": "菜单权限配置在「系统管理 > 菜单管理」中:\n1. 创建菜单项\n2. 设置菜单图标和路径\n3. 为角色分配菜单权限\n4. 保存后权限即时生效",
+        "系统有哪些功能模块?": "本系统包含以下功能模块:\n- 仪表盘:数据概览\n- 用户管理:系统用户管理\n- 系统管理:菜单、角色管理\n- 数据统计:报表分析\n- AI 助手:智能问答",
+        "如何导出数据报表?": "导出数据报表步骤:\n1. 进入「数据统计」页面\n2. 选择查询条件\n3. 点击「导出」按钮\n4. 选择导出格式(Excel/PDF)\n5. 下载生成的报表文件",
+        "如何修改个人资料?": "修改个人资料:\n1. 点击右上角用户名\n2. 选择「个人中心」\n3. 编辑个人信息\n4. 点击「保存」按钮",
+        "你好": "您好!很高兴为您服务,请问有什么可以帮助您的?",
+        "帮助": "我可以帮您:\n- 解答系统使用问题\n- 提供操作指导\n- 解释功能说明\n请直接输入您的问题!"
+      };
+
+      // 模糊匹配
+      for (const key in knowledgeBase) {
+        if (question.includes(key) || key.includes(question)) {
+          return {
+            code: 200,
+            message: "success",
+            data: { reply: knowledgeBase[key] }
+          };
+        }
+      }
+
+      // 默认回复
+      return {
+        code: 200,
+        message: "success",
+        data: {
+          reply: `感谢您的提问!关于"${question}",我建议您:\n\n1. 查看相关功能模块的说明文档\n2. 联系系统管理员获取帮助\n3. 或者尝试在快捷问题中寻找类似问题\n\n如果您需要更详细的帮助,请联系技术支持。`
+        }
+      };
+    },
+  },
 ] as MockMethod[];

+ 8 - 0
src/utils/api.ts

@@ -65,6 +65,14 @@ export const fileApi = {
   }
 }
 
+// AI 对话 API
+export const aiApi = {
+  // 发送对话消息
+  chat(question: string) {
+    return http.post<{ reply: string }>('/upms/ai/chat', { question })
+  }
+}
+
 // 通用 API 示例
 export const commonApi = {
   // 获取字典数据

+ 19 - 33
src/views/ai-chat/index.vue

@@ -170,41 +170,27 @@ const sendMessage = async () => {
   isTyping.value = true
   scrollToBottom()
 
-  // 模拟 AI 回复
-  setTimeout(() => {
-    isTyping.value = false
-    const reply = generateReply(content)
-    messages.value.push({
-      role: 'assistant',
-      content: reply,
-      time: new Date().toLocaleString()
-    })
-    isSending.value = false
-    scrollToBottom()
-  }, 1500)
-}
-
-// 生成回复(模拟)
-const generateReply = (question: string): string => {
-  const replies: Record<string, string> = {
-    '如何管理系统用户?': '系统用户管理在"用户管理"菜单中,您可以进行以下操作:\n1. 查看用户列表\n2. 添加新用户\n3. 编辑用户信息\n4. 禁用/启用用户账号',
-    '如何配置菜单权限?': '菜单权限配置在"系统管理 > 菜单管理"中:\n1. 创建菜单项\n2. 设置菜单图标和路径\n3. 为角色分配菜单权限\n4. 保存后权限即时生效',
-    '系统有哪些功能模块?': '本系统包含以下功能模块:\n- 仪表盘:数据概览\n- 用户管理:系统用户管理\n- 系统管理:菜单、角色管理\n- 数据统计:报表分析\n- AI 助手:智能问答',
-    '如何导出数据报表?': '导出数据报表步骤:\n1. 进入"数据统计"页面\n2. 选择查询条件\n3. 点击"导出"按钮\n4. 选择导出格式(Excel/PDF)\n5. 下载生成的报表文件',
-    '如何修改个人资料?': '修改个人资料:\n1. 点击右上角用户名\n2. 选择"个人中心"\n3. 编辑个人信息\n4. 点击"保存"按钮',
-    '你好': '您好!很高兴为您服务,请问有什么可以帮助您的?',
-    '帮助': '我可以帮您:\n- 解答系统使用问题\n- 提供操作指导\n- 解释功能说明\n请直接输入您的问题!'
-  }
+  // 调用 AI 接口获取回复
+  const reply = await sendChatMessage(content)
+  isTyping.value = false
+  messages.value.push({
+    role: 'assistant',
+    content: reply,
+    time: new Date().toLocaleString()
+  })
+  isSending.value = false
+  scrollToBottom()
+}
 
-  // 模糊匹配
-  for (const key in replies) {
-    if (question.includes(key) || key.includes(question)) {
-      return replies[key]
-    }
+// AI 对话接口
+const sendChatMessage = async (question: string): Promise<string> => {
+  try {
+    const res = await http.post<{ reply: string }>('/upms/ai/chat', { question })
+    return res.reply
+  } catch (error) {
+    console.error('AI 接口调用失败:', error)
+    return '抱歉,AI 服务暂时不可用,请稍后再试。'
   }
-
-  // 默认回复
-  return `感谢您的提问!关于"${question}",我建议您:\n\n1. 查看相关功能模块的说明文档\n2. 联系系统管理员获取帮助\n3. 或者尝试在快捷问题中寻找类似问题\n\n如果您需要更详细的帮助,请联系技术支持。`
 }
 
 // 选择快捷问题