紧急修复:客户端改为调用Remix API routes,不再直接调用Dify API

根本问题:客户端代码直接调用Dify API(12980端口),绕过了服务端代理

修改内容:
1. app/config/api-config.ts
   - 添加独立的 difyBaseUrl 配置(指向外网 nas.7bm.co:8000)
   - 导出 DIFY_BASE_URL 供服务端使用

2. app/config/chat.ts
   - 移除直接Dify API配置(NEXT_PUBLIC_API_URL, APP_ID, API_KEY)
   - 移除 generateUserId 函数
   - API_URL 改为 '/api'(指向Remix API routes)

3. app/services/api.client.ts
   - 所有fetch调用改为相对路径 /api/*
   - 移除所有 Authorization 头(服务端自动处理JWT)
   - 移除所有 user 参数传递(服务端从JWT提取)
   - credentials 改为 'include' 以携带cookie

4. app/services/dify-client.server.ts
   - 使用 DIFY_BASE_URL 替代 API_BASE_URL

5. app/utils/dify-test.client.ts
   - 测试函数改为调用Remix API routes

调用链路:
客户端 → /api/* → Remix API routes → dify-client.server.ts → FastAPI /dify → Dify

解决问题:
-  不再直接调用 nas.7bm.co:12980(Dify端口)
-  统一通过 nas.7bm.co:8000/dify(FastAPI代理)
-  所有请求都经过JWT认证
-  user字段由后端自动管理

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-30 11:25:37 +08:00
parent 63acabccc9
commit cf6e9c2421
5 changed files with 62 additions and 122 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
import { API_BASE_URL } from '~/config/api-config';
import { DIFY_BASE_URL } from '~/config/api-config';
// 获取环境变量的服务端函数
const getServerEnvVar = (name: string, defaultValue: string = '') => {
@@ -14,8 +14,8 @@ const getServerEnvVar = (name: string, defaultValue: string = '') => {
// Dify API 客户端配置
// 注意:现在通过 FastAPI 后端的 /dify 路由代理访问 Dify,使用 JWT 认证
const DIFY_CONFIG = {
// API_URL 指向 FastAPI 后端的 /dify 路由
API_URL: `${API_BASE_URL}/dify`,
// API_URL 指向 FastAPI 后端的 /dify 路由(使用外网地址 nas.7bm.co:8000
API_URL: `${DIFY_BASE_URL}/dify`,
// API_KEY 保留用于配置验证(实际不再使用,改用JWT)
API_KEY: getServerEnvVar('NEXT_PUBLIC_APP_KEY', ''),
APP_ID: (() => {