diff --git a/src/api/device.js b/src/api/device.js new file mode 100644 index 00000000..17ab3b13 --- /dev/null +++ b/src/api/device.js @@ -0,0 +1,53 @@ +import http from '../utils/http.js'; + +/** + * 设备管理API + */ +export class DeviceApi { + /** + * 获取设备列表 + * @returns {Promise} 设备列表 + */ + static list() { + return http.get('/devices'); + } + + /** + * 创建新设备 + * @param {Object} deviceData 设备数据 + * @returns {Promise} 创建结果 + */ + static create(deviceData) { + return http.post('/devices', deviceData); + } + + /** + * 获取设备详情 + * @param {string|number} id 设备ID + * @returns {Promise} 设备详情 + */ + static get(id) { + return http.get(`/devices/${id}`); + } + + /** + * 更新设备信息 + * @param {string|number} id 设备ID + * @param {Object} deviceData 设备数据 + * @returns {Promise} 更新结果 + */ + static update(id, deviceData) { + return http.put(`/devices/${id}`, deviceData); + } + + /** + * 删除设备 + * @param {string|number} id 设备ID + * @returns {Promise} 删除结果 + */ + static delete(id) { + return http.delete(`/devices/${id}`); + } +} + +export default DeviceApi; \ No newline at end of file diff --git a/src/api/index.js b/src/api/index.js new file mode 100644 index 00000000..f21ed065 --- /dev/null +++ b/src/api/index.js @@ -0,0 +1,17 @@ +import DeviceApi from './device.js'; +import PlanApi from './plan.js'; +import UserApi from './user.js'; + +/** + * API客户端 + */ +export class ApiClient { + constructor() { + this.devices = DeviceApi; + this.plans = PlanApi; + this.users = UserApi; + } +} + +// 导出API客户端实例 +export default new ApiClient(); \ No newline at end of file diff --git a/src/api/plan.js b/src/api/plan.js new file mode 100644 index 00000000..d1f7a4e7 --- /dev/null +++ b/src/api/plan.js @@ -0,0 +1,71 @@ +import http from '../utils/http.js'; + +/** + * 计划管理API + */ +export class PlanApi { + /** + * 获取计划列表 + * @returns {Promise} 计划列表 + */ + static list() { + return http.get('/plans'); + } + + /** + * 创建新计划 + * @param {Object} planData 计划数据 + * @returns {Promise} 创建结果 + */ + static create(planData) { + return http.post('/plans', planData); + } + + /** + * 获取计划详情 + * @param {string|number} id 计划ID + * @returns {Promise} 计划详情 + */ + static get(id) { + return http.get(`/plans/${id}`); + } + + /** + * 更新计划信息 + * @param {string|number} id 计划ID + * @param {Object} planData 计划数据 + * @returns {Promise} 更新结果 + */ + static update(id, planData) { + return http.put(`/plans/${id}`, planData); + } + + /** + * 删除计划 + * @param {string|number} id 计划ID + * @returns {Promise} 删除结果 + */ + static delete(id) { + return http.delete(`/plans/${id}`); + } + + /** + * 启动计划 + * @param {string|number} id 计划ID + * @returns {Promise} 启动结果 + */ + static start(id) { + return http.post(`/plans/${id}/start`); + } + + /** + * 停止计划 + * @param {string|number} id 计划ID + * @returns {Promise} 停止结果 + */ + static stop(id) { + return http.post(`/plans/${id}/stop`); + } +} + +export default PlanApi; \ No newline at end of file diff --git a/src/api/user.js b/src/api/user.js new file mode 100644 index 00000000..93d6e33d --- /dev/null +++ b/src/api/user.js @@ -0,0 +1,26 @@ +import http from '../utils/http.js'; + +/** + * 用户管理API + */ +export class UserApi { + /** + * 创建新用户 + * @param {Object} userData 用户数据 + * @returns {Promise} 创建结果 + */ + static create(userData) { + return http.post('/users', userData); + } + + /** + * 用户登录 + * @param {Object} credentials 登录凭证 {username, password} + * @returns {Promise} 登录结果 + */ + static login(credentials) { + return http.post('/users/login', credentials); + } +} + +export default UserApi; \ No newline at end of file diff --git a/src/services/deviceService.js b/src/services/deviceService.js index 94da5304..9195e519 100644 --- a/src/services/deviceService.js +++ b/src/services/deviceService.js @@ -1,5 +1,4 @@ -import http from '../utils/http.js'; -import API_CONFIG from '../config/api.js'; +import apiClient from '../api/index.js'; class DeviceService { /** @@ -8,7 +7,7 @@ class DeviceService { */ async getDevices() { try { - const response = await http.get(API_CONFIG.ENDPOINTS.DEVICES); + const response = await apiClient.devices.list(); return response.data || []; } catch (error) { console.error('获取设备列表失败:', error); @@ -23,7 +22,7 @@ class DeviceService { */ async createDevice(device) { try { - const response = await http.post(API_CONFIG.ENDPOINTS.DEVICES, device); + const response = await apiClient.devices.create(device); return response.data; } catch (error) { console.error('创建设备失败:', error); @@ -39,7 +38,7 @@ class DeviceService { */ async updateDevice(deviceId, device) { try { - const response = await http.put(`${API_CONFIG.ENDPOINTS.DEVICES}/${deviceId}`, device); + const response = await apiClient.devices.update(deviceId, device); return response.data; } catch (error) { console.error('更新设备失败:', error); @@ -54,7 +53,7 @@ class DeviceService { */ async deleteDevice(deviceId) { try { - await http.delete(`${API_CONFIG.ENDPOINTS.DEVICES}/${deviceId}`); + await apiClient.devices.delete(deviceId); } catch (error) { console.error('删除设备失败:', error); throw error;