diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 392a9c5..192bcb2 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -3,7 +3,12 @@ "allow": [ "Bash(pnpm dlx:*)", "Bash(pnpm dev:*)", - "Bash(mkdir:*)" + "Bash(mkdir:*)", + "Bash(git remote add:*)", + "Bash(git push:*)", + "Bash(git add:*)", + "Bash(npm run build:*)", + "Bash(git add:*)" ], "deny": [] } diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..7391eef --- /dev/null +++ b/.env.template @@ -0,0 +1,11 @@ +# 环境配置模板 +# 部署时复制此文件为 .env.production 并修改相应配置 + +# 前端服务端口(默认 3000) +PORT=3000 + +# 后端 API 基础地址 +# NEXT_PUBLIC_API_BASE_URL=http://当前域名:3006 + +# 环境 +NODE_ENV=production \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5ef6a52..328e2df 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ yarn-error.log* # env files (can opt-in for committing if needed) .env* +!.env.template # vercel .vercel diff --git a/lib/api.ts b/lib/api.ts index a639849..7e42ac5 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -8,13 +8,28 @@ import { ScriptUploadItem } from './types'; -const API_BASE_URL = 'http://localhost:3006'; +const getApiBaseUrl = () => { + // 检查是否有配置的环境变量(构建时注入) + const envUrl = process.env.NEXT_PUBLIC_API_BASE_URL; + if (envUrl && envUrl !== 'undefined') { + return envUrl; + } + + // 在浏览器环境中,没有配置则使用当前域名 + if (typeof window !== 'undefined') { + return `${window.location.protocol}//${window.location.hostname}:3006`; + } + + // 服务端渲染时的默认值 + return 'http://localhost:3006'; +}; class ApiClient { private async request( endpoint: string, options: RequestInit = {} ): Promise> { + const API_BASE_URL = getApiBaseUrl(); // 每次请求时动态获取 const url = `${API_BASE_URL}${endpoint}`; const response = await fetch(url, { diff --git a/package.json b/package.json index 215a631..97a3d90 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "private": true, "scripts": { "dev": "next dev --turbopack", - "build": "next build --turbopack", - "start": "next start" + "build": "next build --turbopack", + "start": "next start -p ${PORT:-3000}", + "start:prod": "next start -p ${PORT:-3000}" }, "dependencies": { "@radix-ui/react-checkbox": "^1.3.3",