Spaces:
Sleeping
Sleeping
feat:让打包的 App 可以访问线上数据
Browse files- src/components/PaymentModal.tsx +2 -2
- src/hooks/useSocket.ts +3 -1
- src/lib/api.ts +45 -0
- src/main.tsx +4 -0
- src/pages/dashboard/Chat.tsx +2 -2
src/components/PaymentModal.tsx
CHANGED
|
@@ -93,9 +93,9 @@ export default function PaymentModal() {
|
|
| 93 |
method: 'POST',
|
| 94 |
headers: {
|
| 95 |
'Content-Type': 'application/json',
|
| 96 |
-
'Authorization': `Bearer ${token}`
|
| 97 |
},
|
| 98 |
-
body: JSON.stringify({ orderId: currentOrderId,
|
| 99 |
});
|
| 100 |
const qrData = await qrRes.json();
|
| 101 |
if (qrData.success && qrData.qrCode) {
|
|
|
|
| 93 |
method: 'POST',
|
| 94 |
headers: {
|
| 95 |
'Content-Type': 'application/json',
|
| 96 |
+
'Authorization': `Bearer ${token}`
|
| 97 |
},
|
| 98 |
+
body: JSON.stringify({ orderId: currentOrderId, method })
|
| 99 |
});
|
| 100 |
const qrData = await qrRes.json();
|
| 101 |
if (qrData.success && qrData.qrCode) {
|
src/hooks/useSocket.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { useEffect, useRef } from 'react';
|
| 2 |
import { io, Socket } from 'socket.io-client';
|
| 3 |
import { useStore } from '@/store/useStore';
|
|
|
|
| 4 |
|
| 5 |
export const useSocket = () => {
|
| 6 |
const { user, token } = useStore();
|
|
@@ -10,7 +11,8 @@ export const useSocket = () => {
|
|
| 10 |
if (!token || !user) return;
|
| 11 |
|
| 12 |
// 初始化连接,增加鉴权
|
| 13 |
-
const
|
|
|
|
| 14 |
auth: { token }
|
| 15 |
});
|
| 16 |
socketRef.current = socket;
|
|
|
|
| 1 |
import { useEffect, useRef } from 'react';
|
| 2 |
import { io, Socket } from 'socket.io-client';
|
| 3 |
import { useStore } from '@/store/useStore';
|
| 4 |
+
import { getBaseUrl } from '@/lib/api';
|
| 5 |
|
| 6 |
export const useSocket = () => {
|
| 7 |
const { user, token } = useStore();
|
|
|
|
| 11 |
if (!token || !user) return;
|
| 12 |
|
| 13 |
// 初始化连接,增加鉴权
|
| 14 |
+
const baseUrl = getBaseUrl();
|
| 15 |
+
const socket = io(baseUrl || window.location.origin, {
|
| 16 |
auth: { token }
|
| 17 |
});
|
| 18 |
socketRef.current = socket;
|
src/lib/api.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* 全局 API 环境配置
|
| 3 |
+
* 自动识别开发环境、生产环境、Tauri 桌面端、Capacitor 移动端
|
| 4 |
+
*/
|
| 5 |
+
export const getBaseUrl = () => {
|
| 6 |
+
// 1. 优先使用环境变量中配置的地址
|
| 7 |
+
const envBaseUrl = import.meta.env.VITE_API_BASE_URL;
|
| 8 |
+
if (envBaseUrl) return envBaseUrl;
|
| 9 |
+
|
| 10 |
+
// 2. 如果是打包环境 (Tauri 或 Capacitor),必须使用线上公网地址
|
| 11 |
+
if (
|
| 12 |
+
typeof window !== 'undefined' &&
|
| 13 |
+
(window.location.protocol === 'tauri:' ||
|
| 14 |
+
window.location.protocol === 'capacitor:' ||
|
| 15 |
+
window.location.hostname === 'localhost' === false)
|
| 16 |
+
) {
|
| 17 |
+
return 'https://duqing2026-codex-ai-platform.hf.space';
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// 3. 本地开发环境且是浏览器访问,返回空字符串(走 Vite Proxy 代理)
|
| 21 |
+
return '';
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
/**
|
| 25 |
+
* 全局 Fetch 拦截器安装函数
|
| 26 |
+
*/
|
| 27 |
+
export const setupGlobalFetch = () => {
|
| 28 |
+
const { fetch: originalFetch } = window;
|
| 29 |
+
const baseUrl = getBaseUrl();
|
| 30 |
+
|
| 31 |
+
if (!baseUrl) return; // 如果没有基准地址(本地开发),不进行拦截
|
| 32 |
+
|
| 33 |
+
window.fetch = async (...args) => {
|
| 34 |
+
let [resource, config] = args;
|
| 35 |
+
|
| 36 |
+
// 如果是字符串路径且以 /api 开头,则补齐基准地址
|
| 37 |
+
if (typeof resource === 'string' && resource.startsWith('/api')) {
|
| 38 |
+
resource = `${baseUrl}${resource}`;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
return originalFetch(resource, config);
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
console.log(`[API] 全局拦截器已启动,基准地址: ${baseUrl}`);
|
| 45 |
+
};
|
src/main.tsx
CHANGED
|
@@ -4,6 +4,10 @@ import { HelmetProvider } from 'react-helmet-async';
|
|
| 4 |
import App from './App';
|
| 5 |
import './index.css';
|
| 6 |
import './i18n'; // 导入 i18n 配置
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
ReactDOM.createRoot(document.getElementById('root')!).render(
|
| 9 |
<React.StrictMode>
|
|
|
|
| 4 |
import App from './App';
|
| 5 |
import './index.css';
|
| 6 |
import './i18n'; // 导入 i18n 配置
|
| 7 |
+
import { setupGlobalFetch } from './lib/api';
|
| 8 |
+
|
| 9 |
+
// 在应用启动前初始化全局 API 拦截器
|
| 10 |
+
setupGlobalFetch();
|
| 11 |
|
| 12 |
ReactDOM.createRoot(document.getElementById('root')!).render(
|
| 13 |
<React.StrictMode>
|
src/pages/dashboard/Chat.tsx
CHANGED
|
@@ -200,7 +200,7 @@ export default function ChatPage() {
|
|
| 200 |
} else {
|
| 201 |
formDataToSend.append('content', formData.content);
|
| 202 |
}
|
| 203 |
-
|
| 204 |
const response = await fetch('/api/knowledge/create', {
|
| 205 |
method: 'POST',
|
| 206 |
headers: { 'Authorization': `Bearer ${token}` },
|
|
@@ -237,7 +237,7 @@ export default function ChatPage() {
|
|
| 237 |
|
| 238 |
addChatMessage({ content: userQuery, role: 'user' });
|
| 239 |
addChatMessage({ content: '', role: 'assistant' });
|
| 240 |
-
|
| 241 |
try {
|
| 242 |
const response = await fetch('/api/ai/chat', {
|
| 243 |
method: 'POST',
|
|
|
|
| 200 |
} else {
|
| 201 |
formDataToSend.append('content', formData.content);
|
| 202 |
}
|
| 203 |
+
|
| 204 |
const response = await fetch('/api/knowledge/create', {
|
| 205 |
method: 'POST',
|
| 206 |
headers: { 'Authorization': `Bearer ${token}` },
|
|
|
|
| 237 |
|
| 238 |
addChatMessage({ content: userQuery, role: 'user' });
|
| 239 |
addChatMessage({ content: '', role: 'assistant' });
|
| 240 |
+
|
| 241 |
try {
|
| 242 |
const response = await fetch('/api/ai/chat', {
|
| 243 |
method: 'POST',
|