From 2e79fcfeb0567dd5a63ff087cde52133b75496fb Mon Sep 17 00:00:00 2001 From: cloud370 Date: Tue, 23 Sep 2025 09:08:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AE=E7=94=9F=E4=BA=A7?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=90=8E=E7=AB=AF=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=9C=B0=E5=9D=80=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加环境变量支持,可通过 NEXT_PUBLIC_API_BASE_URL 配置后端地址 - 实现智能地址获取:无配置时自动使用当前域名+3006端口 - 添加 .env.template 模板文件,方便部署时配置 - 更新 package.json 脚本支持通过 PORT 环境变量配置前端端口 - 修改 .gitignore 允许模板文件提交但保护敏感环境文件 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .claude/settings.local.json | 7 ++++++- .env.template | 11 +++++++++++ .gitignore | 1 + lib/api.ts | 17 ++++++++++++++++- package.json | 5 +++-- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 .env.template 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",