pref: 去除开发环境配置

This commit is contained in:
zdl
2025-10-16 15:54:52 +08:00
parent ca51252fce
commit 081eb3c5c3
6 changed files with 42 additions and 57 deletions

View File

@@ -37,7 +37,8 @@ import WechatRegister from './WechatRegister';
// API配置 // API配置
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : "http://49.232.185.254:5001"; // 使用空字符串让请求通过 webpack proxy避免跨域 cookie 问题
const API_BASE_URL = "";
// 统一配置对象 // 统一配置对象
const AUTH_CONFIG = { const AUTH_CONFIG = {
@@ -154,12 +155,12 @@ export default function AuthFormContent() {
try { try {
setSendingCode(true); setSendingCode(true);
const response = await fetch(`${API_BASE_URL}/api/auth/send-verification-code`, { const response = await fetch('/api/auth/send-verification-code', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({ body: JSON.stringify({
credential, credential,
type: 'phone', type: 'phone',
@@ -243,12 +244,12 @@ export default function AuthFormContent() {
}; };
// 调用API根据模式选择不同的endpoint // 调用API根据模式选择不同的endpoint
const response = await fetch(`${API_BASE_URL}/api/auth/login-with-code`, { const response = await fetch('/api/auth/login-with-code', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify(requestBody), body: JSON.stringify(requestBody),
}); });

View File

@@ -3,10 +3,6 @@ import React, { createContext, useContext, useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useToast } from '@chakra-ui/react'; import { useToast } from '@chakra-ui/react';
// API基础URL配置
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL || "http://49.232.185.254:5001";
// 创建认证上下文 // 创建认证上下文
const AuthContext = createContext(); const AuthContext = createContext();
@@ -32,7 +28,7 @@ export const AuthProvider = ({ children }) => {
try { try {
console.log('🔍 检查Session状态...'); console.log('🔍 检查Session状态...');
const response = await fetch(`${API_BASE_URL}/api/auth/session`, { const response = await fetch(`/api/auth/session`, {
method: 'GET', method: 'GET',
credentials: 'include', // 重要包含cookie credentials: 'include', // 重要包含cookie
headers: { headers: {
@@ -103,14 +99,14 @@ export const AuthProvider = ({ children }) => {
formData.append('username', credential); formData.append('username', credential);
} }
console.log('📤 发送登录请求到:', `${API_BASE_URL}/api/auth/login`); console.log('📤 发送登录请求到:', `/api/auth/login`);
console.log('📝 请求数据:', { console.log('📝 请求数据:', {
credential, credential,
loginType, loginType,
formData: formData.toString() formData: formData.toString()
}); });
const response = await fetch(`${API_BASE_URL}/api/auth/login`, { const response = await fetch(`/api/auth/login`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@@ -181,7 +177,7 @@ export const AuthProvider = ({ children }) => {
formData.append('email', email); formData.append('email', email);
formData.append('password', password); formData.append('password', password);
const response = await fetch(`${API_BASE_URL}/api/auth/register`, { const response = await fetch(`/api/auth/register`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@@ -232,7 +228,7 @@ export const AuthProvider = ({ children }) => {
try { try {
setIsLoading(true); setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/api/auth/register/phone`, { const response = await fetch(`/api/auth/register/phone`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -288,7 +284,7 @@ export const AuthProvider = ({ children }) => {
try { try {
setIsLoading(true); setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/api/auth/register/email`, { const response = await fetch(`/api/auth/register/email`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -342,11 +338,12 @@ export const AuthProvider = ({ children }) => {
// 发送手机验证码 // 发送手机验证码
const sendSmsCode = async (phone) => { const sendSmsCode = async (phone) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/auth/send-sms-code`, { const response = await fetch(`/api/auth/send-sms-code`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({ phone }) body: JSON.stringify({ phone })
}); });
@@ -384,11 +381,12 @@ export const AuthProvider = ({ children }) => {
// 发送邮箱验证码 // 发送邮箱验证码
const sendEmailCode = async (email) => { const sendEmailCode = async (email) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/auth/send-email-code`, { const response = await fetch(`/api/auth/send-email-code`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({ email }) body: JSON.stringify({ email })
}); });
@@ -427,7 +425,7 @@ export const AuthProvider = ({ children }) => {
const logout = async () => { const logout = async () => {
try { try {
// 调用后端登出API // 调用后端登出API
await fetch(`${API_BASE_URL}/api/auth/logout`, { await fetch(`/api/auth/logout`, {
method: 'POST', method: 'POST',
credentials: 'include' credentials: 'include'
}); });
@@ -476,7 +474,7 @@ export const AuthProvider = ({ children }) => {
} }
// 获取用户权限信息 // 获取用户权限信息
const response = await fetch(`${API_BASE_URL}/api/subscription/permissions`, { const response = await fetch(`/api/subscription/permissions`, {
method: 'GET', method: 'GET',
credentials: 'include', credentials: 'include',
headers: { headers: {

View File

@@ -3,10 +3,6 @@ import React, { createContext, useContext, useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useToast } from '@chakra-ui/react'; import { useToast } from '@chakra-ui/react';
// API基础URL配置
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL || "http://49.232.185.254:5001";
// 创建认证上下文 // 创建认证上下文
const AuthContext = createContext(); const AuthContext = createContext();
@@ -36,7 +32,7 @@ export const AuthProvider = ({ children }) => {
const controller = new AbortController(); const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时 const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时
const response = await fetch(`${API_BASE_URL}/api/auth/session`, { const response = await fetch(`/api/auth/session`, {
method: 'GET', method: 'GET',
credentials: 'include', // 重要包含cookie credentials: 'include', // 重要包含cookie
headers: { headers: {
@@ -114,14 +110,14 @@ export const AuthProvider = ({ children }) => {
formData.append('username', credential); formData.append('username', credential);
} }
console.log('📤 发送登录请求到:', `${API_BASE_URL}/api/auth/login`); console.log('📤 发送登录请求到:', `/api/auth/login`);
console.log('📝 请求数据:', { console.log('📝 请求数据:', {
credential, credential,
loginType, loginType,
formData: formData.toString() formData: formData.toString()
}); });
const response = await fetch(`${API_BASE_URL}/api/auth/login`, { const response = await fetch(`/api/auth/login`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@@ -194,7 +190,7 @@ export const AuthProvider = ({ children }) => {
formData.append('email', email); formData.append('email', email);
formData.append('password', password); formData.append('password', password);
const response = await fetch(`${API_BASE_URL}/api/auth/register`, { const response = await fetch(`/api/auth/register`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@@ -245,7 +241,7 @@ export const AuthProvider = ({ children }) => {
try { try {
setIsLoading(true); setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/api/auth/register/phone`, { const response = await fetch(`/api/auth/register/phone`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -301,7 +297,7 @@ export const AuthProvider = ({ children }) => {
try { try {
setIsLoading(true); setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/api/auth/register/email`, { const response = await fetch(`/api/auth/register/email`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -355,11 +351,12 @@ export const AuthProvider = ({ children }) => {
// 发送手机验证码 // 发送手机验证码
const sendSmsCode = async (phone) => { const sendSmsCode = async (phone) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/auth/send-sms-code`, { const response = await fetch(`/api/auth/send-sms-code`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({ phone }) body: JSON.stringify({ phone })
}); });
@@ -397,11 +394,12 @@ export const AuthProvider = ({ children }) => {
// 发送邮箱验证码 // 发送邮箱验证码
const sendEmailCode = async (email) => { const sendEmailCode = async (email) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/auth/send-email-code`, { const response = await fetch(`/api/auth/send-email-code`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({ email }) body: JSON.stringify({ email })
}); });
@@ -440,7 +438,7 @@ export const AuthProvider = ({ children }) => {
const logout = async () => { const logout = async () => {
try { try {
// 调用后端登出API // 调用后端登出API
await fetch(`${API_BASE_URL}/api/auth/logout`, { await fetch(`/api/auth/logout`, {
method: 'POST', method: 'POST',
credentials: 'include' credentials: 'include'
}); });

View File

@@ -1,15 +1,10 @@
// src/services/eventService.js // src/services/eventService.js
// 判断当前是否是生产环境
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL;
//const API_BASE_URL = process.env.REACT_APP_API_URL || "http://49.232.185.254:5001";
const apiRequest = async (url, options = {}) => { const apiRequest = async (url, options = {}) => {
try { try {
console.log(`Making API request to: ${API_BASE_URL}${url}`); console.log(`Making API request to: ${url}`);
const response = await fetch(`${API_BASE_URL}${url}`, { const response = await fetch(url, {
...options, ...options,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -322,7 +317,7 @@ export const stockService = {
} }
const url = `/api/stock/${stockCode}/kline?${params.toString()}`; const url = `/api/stock/${stockCode}/kline?${params.toString()}`;
console.log(`获取K线数据: ${API_BASE_URL}${url}`); console.log(`获取K线数据: ${url}`);
const response = await apiRequest(url); const response = await apiRequest(url);
@@ -387,7 +382,7 @@ export const indexService = {
} }
const url = `/api/index/${indexCode}/kline?${params.toString()}`; const url = `/api/index/${indexCode}/kline?${params.toString()}`;
console.log(`获取指数K线数据: ${API_BASE_URL}${url}`); console.log(`获取指数K线数据: ${url}`);
const response = await apiRequest(url); const response = await apiRequest(url);
return response; return response;
} catch (error) { } catch (error) {

View File

@@ -32,10 +32,6 @@ import AuthFooter from "../../../components/Auth/AuthFooter";
import VerificationCodeInput from "../../../components/Auth/VerificationCodeInput"; import VerificationCodeInput from "../../../components/Auth/VerificationCodeInput";
import WechatRegister from "../../../components/Auth/WechatRegister"; import WechatRegister from "../../../components/Auth/WechatRegister";
// API配置
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : "http://49.232.185.254:5001";
export default function SignInIllustration() { export default function SignInIllustration() {
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
@@ -174,7 +170,7 @@ export default function SignInIllustration() {
try { try {
setSendingCode(true); setSendingCode(true);
const response = await fetch(`${API_BASE_URL}/api/auth/send-verification-code`, { const response = await fetch('/api/auth/send-verification-code', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -234,7 +230,7 @@ export default function SignInIllustration() {
// 验证码登录函数 // 验证码登录函数
const loginWithVerificationCode = async (credential, verificationCode, authLoginType) => { const loginWithVerificationCode = async (credential, verificationCode, authLoginType) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/auth/login-with-code`, { const response = await fetch('/api/auth/login-with-code', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@@ -42,9 +42,6 @@ import { format } from 'date-fns';
import { zhCN } from 'date-fns/locale'; import { zhCN } from 'date-fns/locale';
import { eventService } from '../../../services/eventService'; import { eventService } from '../../../services/eventService';
// 获取 API 基础地址
const API_BASE_URL = process.env.NODE_ENV === 'production' ? '' : (process.env.REACT_APP_API_URL || 'http://49.232.185.254:5001');
const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussionType = '事件讨论' }) => { const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussionType = '事件讨论' }) => {
const [posts, setPosts] = useState([]); const [posts, setPosts] = useState([]);
const [newPostContent, setNewPostContent] = useState(''); const [newPostContent, setNewPostContent] = useState('');
@@ -67,7 +64,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
setLoading(true); setLoading(true);
try { try {
const response = await fetch(`${API_BASE_URL}/api/events/${eventId}/posts?sort=latest&page=1&per_page=20`, { const response = await fetch(`/api/events/${eventId}/posts?sort=latest&page=1&per_page=20`, {
method: 'GET', method: 'GET',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include' credentials: 'include'
@@ -101,7 +98,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
const loadPostComments = async (postId) => { const loadPostComments = async (postId) => {
setLoadingComments(prev => ({ ...prev, [postId]: true })); setLoadingComments(prev => ({ ...prev, [postId]: true }));
try { try {
const response = await fetch(`${API_BASE_URL}/api/posts/${postId}/comments?sort=latest`, { const response = await fetch(`/api/posts/${postId}/comments?sort=latest`, {
method: 'GET', method: 'GET',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include' credentials: 'include'
@@ -134,7 +131,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
setSubmitting(true); setSubmitting(true);
try { try {
const response = await fetch(`${API_BASE_URL}/api/events/${eventId}/posts`, { const response = await fetch(`/api/events/${eventId}/posts`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include', credentials: 'include',
@@ -182,7 +179,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
if (!window.confirm('确定要删除这个帖子吗?')) return; if (!window.confirm('确定要删除这个帖子吗?')) return;
try { try {
const response = await fetch(`${API_BASE_URL}/api/posts/${postId}`, { const response = await fetch(`/api/posts/${postId}`, {
method: 'DELETE', method: 'DELETE',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include' credentials: 'include'
@@ -219,7 +216,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
// 点赞帖子 // 点赞帖子
const handleLikePost = async (postId) => { const handleLikePost = async (postId) => {
try { try {
const response = await fetch(`${API_BASE_URL}/api/posts/${postId}/like`, { const response = await fetch(`/api/posts/${postId}/like`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include' credentials: 'include'
@@ -251,7 +248,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
if (!content?.trim()) return; if (!content?.trim()) return;
try { try {
const response = await fetch(`${API_BASE_URL}/api/posts/${postId}/comments`, { const response = await fetch(`/api/posts/${postId}/comments`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include', credentials: 'include',
@@ -294,7 +291,7 @@ const EventDiscussionModal = ({ isOpen, onClose, eventId, eventTitle, discussion
if (!window.confirm('确定要删除这条评论吗?')) return; if (!window.confirm('确定要删除这条评论吗?')) return;
try { try {
const response = await fetch(`${API_BASE_URL}/api/comments/${commentId}`, { const response = await fetch(`/api/comments/${commentId}`, {
method: 'DELETE', method: 'DELETE',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include' credentials: 'include'