优化ui接口地址的部署问题

This commit is contained in:
三丙
2025-09-13 23:24:16 +08:00
parent 4fc8b3acbd
commit 7a03cc98a7
4 changed files with 30 additions and 8 deletions

View File

@@ -21,7 +21,8 @@
}, },
"scripts": { "scripts": {
"start": "GENERATE_SOURCEMAP=false react-scripts start", "start": "GENERATE_SOURCEMAP=false react-scripts start",
"build": "GENERATE_SOURCEMAP=false react-scripts build", "build": "GENERATE_SOURCEMAP=false REACT_APP_ENV=development react-scripts build",
"build:prod": "GENERATE_SOURCEMAP=false REACT_APP_ENV=production react-scripts build",
"eject": "react-scripts eject" "eject": "react-scripts eject"
}, },
"eslintConfig": { "eslintConfig": {

View File

@@ -115,7 +115,7 @@
<goal>npm</goal> <goal>npm</goal>
</goals> </goals>
<configuration> <configuration>
<arguments>run build</arguments> <arguments>run build:prod</arguments>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

View File

@@ -334,7 +334,7 @@ const Dashboard: React.FC = () => {
border: '1px solid #f0f0f0', border: '1px solid #f0f0f0',
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)' boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)'
}} }}
bodyStyle={{ padding: '20px' }} styles={{ body: { padding: '20px' } }}
> >
<Statistic <Statistic
title="充电站数量" title="充电站数量"
@@ -351,7 +351,7 @@ const Dashboard: React.FC = () => {
border: '1px solid #f0f0f0', border: '1px solid #f0f0f0',
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)' boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)'
}} }}
bodyStyle={{ padding: '20px' }} styles={{ body: { padding: '20px' } }}
> >
<Statistic <Statistic
title="充电桩数量" title="充电桩数量"
@@ -368,7 +368,7 @@ const Dashboard: React.FC = () => {
border: '1px solid #f0f0f0', border: '1px solid #f0f0f0',
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)' boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)'
}} }}
bodyStyle={{ padding: '20px' }} styles={{ body: { padding: '20px' } }}
> >
<Statistic <Statistic
title="充电枪数量" title="充电枪数量"
@@ -391,7 +391,7 @@ const Dashboard: React.FC = () => {
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)', boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)',
height: '380px' height: '380px'
}} }}
bodyStyle={{ padding: '16px', height: '100%' }} styles={{ body: { padding: '16px', height: '100%' } }}
loading={loading} loading={loading}
> >
<div <div
@@ -412,7 +412,7 @@ const Dashboard: React.FC = () => {
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)', boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)',
height: '380px' height: '380px'
}} }}
bodyStyle={{ padding: '16px', height: '100%' }} styles={{ body: { padding: '16px', height: '100%' } }}
loading={loading} loading={loading}
> >
<div <div

View File

@@ -7,9 +7,30 @@
import axios, {AxiosError, AxiosResponse} from 'axios'; import axios, {AxiosError, AxiosResponse} from 'axios';
import {message} from 'antd'; import {message} from 'antd';
// 获取API基础URL的函数
const getApiBaseUrl = (): string => {
// 如果设置了环境变量,优先使用环境变量
if (process.env.REACT_APP_API_BASE_URL) {
return process.env.REACT_APP_API_BASE_URL;
}
// 根据构建环境决定API基础URL
const env = process.env.REACT_APP_ENV || 'development';
if (env === 'production') {
// 生产环境:使用当前页面的协议和域名
const protocol = window.location.protocol;
const host = window.location.host;
return `${protocol}//${host}`;
} else {
// 开发环境使用localhost:8080
return 'http://localhost:8080';
}
};
// 创建axios实例 // 创建axios实例
const api = axios.create({ const api = axios.create({
baseURL: process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080', baseURL: getApiBaseUrl(),
timeout: 10000, timeout: 10000,
headers: { headers: {
'Content-Type': 'application/json;charset=UTF-8', 'Content-Type': 'application/json;charset=UTF-8',