Просмотр исходного кода

perf(base-卡7): AI 接口地址配置化——config.js 增加 aiChatUrl/difyUrl/difyApiKey,新增 aiChatApi.ts,AIChat store 6 处与 MiniChat 9 处硬编码 IP 全部替换,.env.development 增加 /aiChat /dify 代理

86132 1 неделя назад
Родитель
Сommit
94bd96742c

+ 4 - 1
.env.development

@@ -6,7 +6,10 @@ VITE_PUBLIC_PATH = /
 
 # 跨域代理,您可以配置多个 ,请注意,没有换行符
 #VITE_PROXY = [["/jeecgboot","http://localhost:8080/jeecg-boot"],["/upload","http://localhost:3300/upload"]]
-VITE_PROXY = [["/sw","http://182.92.126.35:6008"],["/jeecgsystem","http://182.92.126.35:9999"],["/upload","http://182.92.126.35:9999/upload"],["/documents", "http://182.92.126.35:9050"],["/modelreq", "http://182.92.126.35:9999"],["/webRtc", "http://182.92.126.35:8051"], ["/python", "http://127.0.0.1:8008"], ["/res", "http://182.92.126.35:9999/"],["/dataCenter", "http://39.97.59.228:10999"],["/ventAI", "http://182.92.126.35:8070"]]
+# [perf-base-v1] 卡7: 新增 /aiChat(AI 会话服务)与 /dify(Dify 服务)两条代理。
+# createProxy 会剥离前缀:/aiChat/xxx -> :6005/xxx;/dify/v1/xxx -> :8000/v1/xxx,
+# 因此前端调用 Dify 时必须写 ${difyBase}/v1/...(前缀剥离后路径需自带 /v1)。
+VITE_PROXY = [["/sw","http://182.92.126.35:6008"],["/jeecgsystem","http://182.92.126.35:9999"],["/upload","http://182.92.126.35:9999/upload"],["/documents", "http://182.92.126.35:9050"],["/modelreq", "http://182.92.126.35:9999"],["/webRtc", "http://182.92.126.35:8051"], ["/python", "http://127.0.0.1:8008"], ["/res", "http://182.92.126.35:9999/"],["/dataCenter", "http://39.97.59.228:10999"],["/ventAI", "http://182.92.126.35:8070"],["/aiChat", "http://182.92.126.35:6005"],["/dify", "http://39.97.59.228:8000"]]
 # VITE_PROXY = [["/jeecgsystem","http://192.168.183.88:9999"],["/upload","http://192.168.183.88:9999/upload"],["/documents", "http://192.168.183.88:9050"],["/modelreq", "http://192.168.183.88:9999"],["/webRtc", "http://192.168.183.88:8051"]]
 # VITE_PROXY = [["/jeecgsystem","http://10.10.150.72:9999"],["/upload","http://localhost:3300/upload"],["/documents", "http://10.10.150.72:9050"],["/modelreq", "http://10.10.150.72:9999"],["/webRtc", "http://192.168.183.216:8051"]]
 # VITE_PROXY = [["/jeecgsystem","http://192.168.1.8:9999"],["/upload","http://localhost:3300/upload"]]

+ 4 - 1
public/js/config.js

@@ -2,7 +2,10 @@ const VUE_APP_URL = {
   baseUrl: "/modelreq", // 后台地址
   webRtcUrl: '/webRtc', // rtsp服务器IP地址
   wsUrl: 'http://182.92.126.35:9999', // WebSocket 地址
-  onlyOfficeUrl: '/documents' // [perf-base-v1] 卡1: OnlyOffice Document Server 反向代理前缀(api.js 按需加载时拼接)
+  onlyOfficeUrl: '/documents', // [perf-base-v1] 卡1: OnlyOffice Document Server 反向代理前缀(api.js 按需加载时拼接)
+  aiChatUrl: '/aiChat', // [perf-base-v1] 卡7: AI 会话服务反向代理前缀(原 http://182.92.126.35:6005)
+  difyUrl: '/dify', // [perf-base-v1] 卡7: Dify 服务反向代理前缀(原 http://39.97.59.228:8000,剥离前缀后路径自带 /v1)
+  difyApiKey: 'app-tSFRUnv0Qkbtik1dwtlhnpkd' // [perf-base-v1] 卡7: Dify 应用 Key,生产环境应由后端代理下发,勿硬编码在前端
 }
 const History_Type = {
   // type: 'remote', // remote、vent  (remote 代表的是历史查询走的装备院的接口,vent是走的咱们的,目前神东的项目都用remote, 其他矿用vent)

+ 13 - 10
src/components/AIChat/MiniChat.vue

@@ -268,6 +268,8 @@
   import type { EChartsType } from 'echarts';
   // [perf-base-v1] 卡6: v-html 消毒——marked/katex 输出经公共 DOMPurify 白名单消毒后再进 formatMessage
   import { sanitizeHtml } from '/@/utils/markdownSafe';
+  // [perf-base-v1] 卡7: Dify 接口地址配置化,原硬编码 http://39.97.59.228:8000/v1 改走 VUE_APP_URL.difyUrl(开发经 vite proxy /dify 转发)
+  import { difyBase, difyApiKey } from '/@/utils/aiChatApi';
   // [perf-base-v1] 卡2b: @vue-office 三组件改异步加载,仅 isShowDoc 预览时才会拉取 office-preview chunk
   const VueOfficePdf = defineAsyncComponent(() => import('@vue-office/pdf/lib/v3/vue-office-pdf.mjs'));
   const VueOfficeDocx = defineAsyncComponent(() => import('@vue-office/docx/lib/v3/vue-office-docx.mjs'));
@@ -288,7 +290,8 @@
   const isShowDoc = ref(false);
   const fileType = ref('');
   const fileUrl = ref('');
-  const APIKEY = ref('Bearer app-tSFRUnv0Qkbtik1dwtlhnpkd');
+  // [perf-base-v1] 卡7: API Key 改从 config.js(VUE_APP_URL.difyApiKey)读取,生产环境应由后端代理下发,勿依赖前端硬编码兜底值
+  const APIKEY = ref('Bearer ' + difyApiKey);
   interface ListItem {
     id: number;
     name?: string;
@@ -555,7 +558,7 @@
       message_files.value.push(files);
     }
     try {
-      const response = await fetch('http://39.97.59.228:8000/v1/chat-messages', {
+      const response = await fetch(`${difyBase}/v1/chat-messages`, {
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
@@ -751,7 +754,7 @@
     formData.append('user', userid);
 
     try {
-      const response = await fetch(`http://39.97.59.228:8000/v1/files/upload`, {
+      const response = await fetch(`${difyBase}/v1/files/upload`, {
         method: 'POST',
         headers: {
           Authorization: APIKEY.value,
@@ -784,7 +787,7 @@
   //停止响应
   async function stopReq() {
     try {
-      let response = await fetch(`http://39.97.59.228:8000/v1/chat-messages/${taskID.value}/stop`, {
+      let response = await fetch(`${difyBase}/v1/chat-messages/${taskID.value}/stop`, {
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
@@ -807,7 +810,7 @@
     // 最大重试次数
     const maxRetries = API_KEYS.length - 1;
     try {
-      let response = await fetch(`http://39.97.59.228:8000/v1/messages?conversation_id=${id}&user=${userid}`, {
+      let response = await fetch(`${difyBase}/v1/messages?conversation_id=${id}&user=${userid}`, {
         method: 'GET',
         headers: {
           'Content-Type': 'application/json',
@@ -901,7 +904,7 @@
   //停止响应
   async function getNextSuggest() {
     try {
-      let response = await fetch(`http://39.97.59.228:8000/v1/messages/${messageID.value}/suggested?user=${userid}`, {
+      let response = await fetch(`${difyBase}/v1/messages/${messageID.value}/suggested?user=${userid}`, {
         method: 'GET',
         headers: {
           'Content-Type': 'application/json',
@@ -925,7 +928,7 @@
   // 保存修改
   const handleSave = async (item: ListItem) => {
     try {
-      let response = await fetch(`http://39.97.59.228:8000/v1/conversations/${item.id}/name`, {
+      let response = await fetch(`${difyBase}/v1/conversations/${item.id}/name`, {
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
@@ -955,7 +958,7 @@
       onOk: async () => {
         // 原有删除逻辑不变
         try {
-          let response = await fetch(`http://39.97.59.228:8000/v1/conversations/${item.id}`, {
+          let response = await fetch(`${difyBase}/v1/conversations/${item.id}`, {
             method: 'DELETE',
             headers: {
               'Content-Type': 'application/json',
@@ -989,7 +992,7 @@
   async function getHistoryList() {
     sessionHistory.value = [];
     try {
-      const fetch1 = fetch(`http://39.97.59.228:8000/v1/conversations?user=${userid}`, {
+      const fetch1 = fetch(`${difyBase}/v1/conversations?user=${userid}`, {
         method: 'get',
         headers: {
           'Content-Type': 'application/json',
@@ -1004,7 +1007,7 @@
           return { data: [] }; // 失败时返回空数组
         });
 
-      const fetch2 = fetch(`http://39.97.59.228:8000/v1/conversations?user=${userid}`, {
+      const fetch2 = fetch(`${difyBase}/v1/conversations?user=${userid}`, {
         method: 'get',
         headers: {
           'Content-Type': 'application/json',

+ 8 - 6
src/store/modules/AIChat.ts

@@ -1,5 +1,7 @@
 import { defineStore } from 'pinia';
 import { useUserStore } from './user';
+// [perf-base-v1] 卡7: AI 接口地址配置化,原硬编码 http://182.92.126.35:6005 改走 VUE_APP_URL.aiChatUrl(开发经 vite proxy /aiChat 转发)
+import { aiFetch } from '/@/utils/aiChatApi';
 
 export const useAIChat = defineStore({
   id: 'ai-chat',
@@ -52,7 +54,7 @@ export const useAIChat = defineStore({
   actions: {
     /** 创建新会话 */
     async createSession() {
-      const response = await fetch('http://182.92.126.35:6005/sessions/create', {
+      const response = await aiFetch('/sessions/create', {
         method: 'post',
         headers: {
           'Content-Type': 'application/json',
@@ -78,7 +80,7 @@ export const useAIChat = defineStore({
 
       try {
         // 发送 POST 请求
-        const response = await fetch('http://182.92.126.35:6005/chat_stream', {
+        const response = await aiFetch('/chat_stream', {
           method: 'POST',
           headers: {
             'Content-Type': 'application/json',
@@ -157,7 +159,7 @@ export const useAIChat = defineStore({
     },
     /** 创建会话的标题 */
     async createSessionTitle(title: string) {
-      await fetch('http://182.92.126.35:6005/sessions/title', {
+      await aiFetch('/sessions/title', {
         method: 'post',
         headers: {
           'Content-Type': 'application/json',
@@ -171,7 +173,7 @@ export const useAIChat = defineStore({
     /** 修改会话的标题 */
     async changeSessionTitle(title: string, id: string) {
       try {
-        const response = await fetch('http://182.92.126.35:6005/sessions/change_title', {
+        const response = await aiFetch('/sessions/change_title', {
           method: 'POST',
           headers: {
             'Content-Type': 'application/json',
@@ -195,7 +197,7 @@ export const useAIChat = defineStore({
     },
     /** 获取会话历史 */
     async getSessionHistory() {
-      const response = await fetch(`http://182.92.126.35:6005/sessions`, {
+      const response = await aiFetch(`/sessions`, {
         method: 'post',
         headers: {
           'Content-Type': 'application/json',
@@ -209,7 +211,7 @@ export const useAIChat = defineStore({
     },
     /** 获取会话历史 */
     async getSessionHistoryByID(id: string) {
-      const response = await fetch(`http://182.92.126.35:6005/sessions/history_chat/?chat_session_id=${id}`, {
+      const response = await aiFetch(`/sessions/history_chat/?chat_session_id=${id}`, {
         method: 'get',
         headers: {
           'Content-Type': 'application/json',

+ 24 - 0
src/utils/aiChatApi.ts

@@ -0,0 +1,24 @@
+// [perf-base-v1] 卡7: AI 接口地址配置化
+// 原实现:AIChat store 硬编码 http://182.92.126.35:6005,MiniChat 硬编码 http://39.97.59.228:8000/v1,
+// 生产/开发环境切换需改源码。现实现:统一走 public/js/config.js 的 VUE_APP_URL 配置
+// (开发环境经 vite proxy 前缀 /aiChat、/dify 转发,生产由 nginx 等反代同名前缀)。
+// 注意:VUE_APP_URL 是 config.js 经典脚本顶层 const(全局词法绑定,不在 window 上),按现有代码惯例直接引用。
+
+/** AI 会话服务基础路径(原 http://182.92.126.35:6005) */
+export const aiBase: string = (typeof VUE_APP_URL !== 'undefined' && VUE_APP_URL.aiChatUrl) || '/aiChat';
+
+/** Dify 服务基础路径(原 http://39.97.59.228:8000,调用方需自行拼 /v1 前缀) */
+export const difyBase: string = (typeof VUE_APP_URL !== 'undefined' && VUE_APP_URL.difyUrl) || '/dify';
+
+/** Dify 应用 API Key(不含 'Bearer ' 前缀)。生产环境应由后端代理下发,勿依赖前端硬编码兜底值。 */
+export const difyApiKey: string =
+  (typeof VUE_APP_URL !== 'undefined' && VUE_APP_URL.difyApiKey) || 'app-tSFRUnv0Qkbtik1dwtlhnpkd';
+
+/**
+ * AI 会话服务 fetch 封装:拼接 aiBase + path 后调用原生 fetch
+ * @param path 以 / 开头的接口路径,如 '/sessions/create'
+ * @param init 原生 fetch 的 RequestInit
+ */
+export function aiFetch(path: string, init?: RequestInit): Promise<Response> {
+  return fetch(`${aiBase}${path}`, init);
+}

+ 3 - 0
types/global.d.ts

@@ -7,6 +7,9 @@ declare global {
     webRtcUrl: string; // rtsp 服务器地址前缀
     wsUrl: string; // WebSocket 地址
     onlyOfficeUrl?: string; // OnlyOffice Document Server 反向代理前缀
+    aiChatUrl?: string; // [perf-base-v1] 卡7: AI 会话服务反向代理前缀
+    difyUrl?: string; // [perf-base-v1] 卡7: Dify 服务反向代理前缀
+    difyApiKey?: string; // [perf-base-v1] 卡7: Dify 应用 Key(生产应由后端代理下发)
   };
 
   // [perf-base-v1] 卡1: OnlyOffice Documents API 全局(由 src/utils/onlyOffice.ts loadDocsApi 按需注入)