紧急修复:客户端改为调用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:
@@ -7,6 +7,8 @@
|
||||
interface ApiConfig {
|
||||
// 主API基础URL
|
||||
baseUrl: string;
|
||||
// Dify对话服务URL(独立配置,使用外网地址)
|
||||
difyBaseUrl: string;
|
||||
// 文档服务URL
|
||||
documentUrl: string;
|
||||
// 文档上传API URL
|
||||
@@ -123,6 +125,7 @@ const configs: Record<string, ApiConfig> = {
|
||||
// 开发环境
|
||||
development: {
|
||||
baseUrl: 'http://172.16.0.55:8000',
|
||||
difyBaseUrl: 'http://nas.7bm.co:8000', // Dify对话服务使用外网地址
|
||||
documentUrl: 'http://172.16.0.55:8000/docauditai/',
|
||||
uploadUrl: 'http://172.16.0.55:8000/admin/documents',
|
||||
oauth: {
|
||||
@@ -137,6 +140,7 @@ const configs: Record<string, ApiConfig> = {
|
||||
// 测试环境
|
||||
testing: {
|
||||
baseUrl: 'http://nas.7bm.co:8873',
|
||||
difyBaseUrl: 'http://nas.7bm.co:8000', // Dify对话服务使用外网地址
|
||||
documentUrl: 'http://nas.7bm.co:8873/docauditai/',
|
||||
uploadUrl: 'http://nas.7bm.co:8873/admin/documents',
|
||||
oauth: {
|
||||
@@ -152,6 +156,8 @@ const configs: Record<string, ApiConfig> = {
|
||||
production: {
|
||||
// postgrest
|
||||
baseUrl: 'http://10.79.97.17:8000',
|
||||
// Dify对话服务使用外网地址
|
||||
difyBaseUrl: 'http://nas.7bm.co:8000',
|
||||
// minio
|
||||
documentUrl: 'http://10.76.244.156:9000/docauditai/',
|
||||
// 文件上传
|
||||
@@ -171,6 +177,7 @@ const configs: Record<string, ApiConfig> = {
|
||||
// 备用配置 (可以根据需要添加更多环境)
|
||||
staging: {
|
||||
baseUrl: 'http://172.16.0.119:9000/admin',
|
||||
difyBaseUrl: 'http://nas.7bm.co:8000', // Dify对话服务使用外网地址
|
||||
documentUrl: 'http://nas.7bm.co:9000/docauditai/',
|
||||
uploadUrl: 'http://172.16.0.119:8000/admin/documents/upload',
|
||||
oauth: {
|
||||
@@ -344,7 +351,8 @@ export const apiConfig = getCurrentConfig();
|
||||
// 导出具体的配置项,方便使用
|
||||
export const {
|
||||
baseUrl: API_BASE_URL,
|
||||
documentUrl: DOCUMENT_URL,
|
||||
difyBaseUrl: DIFY_BASE_URL,
|
||||
documentUrl: DOCUMENT_URL,
|
||||
uploadUrl: UPLOAD_URL,
|
||||
oauth: OAUTH_CONFIG
|
||||
} = apiConfig;
|
||||
|
||||
+9
-37
@@ -28,47 +28,19 @@ const extractAppId = (appUrl: string): string => {
|
||||
return appUrl;
|
||||
};
|
||||
|
||||
// 获取Dify API配置
|
||||
const getDifyApiUrl = () => {
|
||||
return getEnvVar('NEXT_PUBLIC_API_URL', 'https://api.dify.ai/v1');
|
||||
};
|
||||
|
||||
const getAppId = () => {
|
||||
const rawAppId = getEnvVar('NEXT_PUBLIC_APP_ID', '');
|
||||
const extractedAppId = extractAppId(rawAppId);
|
||||
// console.log('🔧 Chat Config Debug:', {
|
||||
// rawAppId,
|
||||
// extractedAppId,
|
||||
// difyApiUrl: getDifyApiUrl(),
|
||||
// hasApiKey: !!getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
|
||||
// });
|
||||
return extractedAppId;
|
||||
};
|
||||
|
||||
// 生成用户ID (模拟服务端逻辑)
|
||||
const generateUserId = () => {
|
||||
const appId = getAppId();
|
||||
// 生成或获取会话ID (可以使用localStorage或其他方式)
|
||||
let sessionId = '';
|
||||
if (typeof window !== 'undefined') {
|
||||
sessionId = localStorage.getItem('dify_session_id') || '';
|
||||
if (!sessionId) {
|
||||
sessionId = 'sess_' + Math.random().toString(36).substring(2, 15);
|
||||
localStorage.setItem('dify_session_id', sessionId);
|
||||
}
|
||||
}
|
||||
return `user_${appId}:${sessionId}`;
|
||||
// 获取Remix API路由的基础URL(客户端调用服务端API routes)
|
||||
// 注意:客户端不再直接调用Dify API,而是调用Remix的API routes
|
||||
// 服务端的API routes会通过dify-client.server.ts代理到FastAPI,再到Dify
|
||||
const getApiBaseUrl = () => {
|
||||
// 客户端使用相对路径调用Remix API routes
|
||||
return '/api';
|
||||
};
|
||||
|
||||
// 聊天应用配置
|
||||
export const CHAT_CONFIG = {
|
||||
// API相关配置 - 直接使用Dify API
|
||||
API_URL: getDifyApiUrl(),
|
||||
APP_ID: getAppId(),
|
||||
API_KEY: getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
|
||||
|
||||
// 用户生成函数
|
||||
generateUserId,
|
||||
// API相关配置 - 调用Remix API routes(不再直接调用Dify)
|
||||
// 客户端 → /api/* routes → dify-client.server.ts → FastAPI /dify → Dify
|
||||
API_URL: getApiBaseUrl(),
|
||||
|
||||
// 应用信息
|
||||
APP_INFO: {
|
||||
|
||||
Reference in New Issue
Block a user