126 lines
3.5 KiB
JavaScript
126 lines
3.5 KiB
JavaScript
/**
|
|
* API 统一入口文件
|
|
* 根据不同环境配置不同的API域名和端口
|
|
* 统一暴露所有模块的API接口
|
|
*/
|
|
|
|
import { createAPI } from './interceptor.js';
|
|
|
|
// 环境配置
|
|
const ENV_CONFIG = {
|
|
development: {
|
|
baseURL: 'http://localhost:8080',
|
|
timeout: 10000,
|
|
enableLogging: true
|
|
},
|
|
test: {
|
|
baseURL: 'http://test-api.smart-crop.com',
|
|
timeout: 15000,
|
|
enableLogging: true
|
|
},
|
|
production: {
|
|
baseURL: 'https://api.smart-crop.com',
|
|
timeout: 20000,
|
|
enableLogging: false
|
|
}
|
|
};
|
|
|
|
// 获取当前环境
|
|
const getEnvironment = () => {
|
|
const env = import.meta.env?.MODE || 'development';
|
|
|
|
// 可以根据其他条件判断环境
|
|
if (env === 'production') return 'production';
|
|
if (env === 'test') return 'test';
|
|
return 'development';
|
|
};
|
|
|
|
// 创建API实例
|
|
const currentEnv = getEnvironment();
|
|
const envConfig = ENV_CONFIG[currentEnv];
|
|
|
|
console.log(`🚀 API Environment: ${currentEnv}`);
|
|
console.log(`🌐 API Base URL: ${envConfig.baseURL}`);
|
|
|
|
// 创建API实例
|
|
export const api = createAPI({
|
|
baseURL: envConfig.baseURL,
|
|
timeout: envConfig.timeout,
|
|
enableLogging: envConfig.enableLogging
|
|
});
|
|
|
|
// 导入各模块API
|
|
import { agriculturalMachineryAPI } from './subModules/agriculturalMachinery.js';
|
|
import { landInformationAPI } from './subModules/landInformation.js';
|
|
import { farmingOperationAPI } from './subModules/farmingOperation.js';
|
|
import { agriculturalAssetAPI } from './subModules/agriculturalAsset.js';
|
|
import { aiCropModelAPI } from './subModules/aiCropModel.js';
|
|
import { waterFertilizerControlAPI } from './subModules/waterFertilizerControl.js';
|
|
import { centralConfigAPI } from './subModules/centralConfig.js';
|
|
|
|
// 统一导出所有API
|
|
export const API = {
|
|
// 农机管理模块
|
|
agriculturalMachinery: agriculturalMachineryAPI(api),
|
|
|
|
// 地块信息模块
|
|
landInformation: landInformationAPI(api),
|
|
|
|
// 农事操作模块
|
|
farmingOperation: farmingOperationAPI(api),
|
|
|
|
// 农业资产模块
|
|
agriculturalAsset: agriculturalAssetAPI(api),
|
|
|
|
// AI作物模型模块
|
|
aiCropModel: aiCropModelAPI(api),
|
|
|
|
// 水肥控制模块
|
|
waterFertilizerControl: waterFertilizerControlAPI(api),
|
|
|
|
// 中心配置模块
|
|
centralConfig: centralConfigAPI(api),
|
|
|
|
// 认证相关API (直接暴露,不需要模块化)
|
|
auth: {
|
|
login: (credentials) => api.post('/auth/login', credentials),
|
|
register: (userInfo) => api.post('/auth/register', userInfo),
|
|
logout: () => api.post('/auth/logout'),
|
|
refreshToken: (refreshToken) => api.post('/auth/refresh-token', { refreshToken }),
|
|
getCurrentUser: () => api.get('/auth/me'),
|
|
updateProfile: (profileData) => api.put('/auth/profile', profileData),
|
|
changePassword: (passwordData) => api.put('/auth/change-password', passwordData)
|
|
},
|
|
|
|
// 通用API
|
|
common: {
|
|
uploadFile: (fileData) => api.post('/common/upload', fileData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
}),
|
|
downloadFile: (fileId) => api.get(`/common/download/${fileId}`, {
|
|
responseType: 'blob'
|
|
}),
|
|
getSystemConfig: () => api.get('/common/system-config'),
|
|
healthCheck: () => api.get('/common/health')
|
|
}
|
|
};
|
|
|
|
// 导出默认API实例和配置
|
|
export default API;
|
|
|
|
// 导出环境信息(供调试使用)
|
|
export { currentEnv, envConfig };
|
|
|
|
// API使用示例
|
|
/*
|
|
import { API } from '@/apis';
|
|
|
|
// 使用农机管理API
|
|
const machineryList = await API.agriculturalMachinery.getMachineryList();
|
|
|
|
// 使用认证API
|
|
const loginResult = await API.auth.login({ username, password });
|
|
|
|
// 使用通用API
|
|
const uploadResult = await API.common.uploadFile(formData);
|
|
*/ |