定义api
This commit is contained in:
53
src/api/device.js
Normal file
53
src/api/device.js
Normal file
@@ -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;
|
||||||
17
src/api/index.js
Normal file
17
src/api/index.js
Normal file
@@ -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();
|
||||||
71
src/api/plan.js
Normal file
71
src/api/plan.js
Normal file
@@ -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;
|
||||||
26
src/api/user.js
Normal file
26
src/api/user.js
Normal file
@@ -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;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import http from '../utils/http.js';
|
import apiClient from '../api/index.js';
|
||||||
import API_CONFIG from '../config/api.js';
|
|
||||||
|
|
||||||
class DeviceService {
|
class DeviceService {
|
||||||
/**
|
/**
|
||||||
@@ -8,7 +7,7 @@ class DeviceService {
|
|||||||
*/
|
*/
|
||||||
async getDevices() {
|
async getDevices() {
|
||||||
try {
|
try {
|
||||||
const response = await http.get(API_CONFIG.ENDPOINTS.DEVICES);
|
const response = await apiClient.devices.list();
|
||||||
return response.data || [];
|
return response.data || [];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取设备列表失败:', error);
|
console.error('获取设备列表失败:', error);
|
||||||
@@ -23,7 +22,7 @@ class DeviceService {
|
|||||||
*/
|
*/
|
||||||
async createDevice(device) {
|
async createDevice(device) {
|
||||||
try {
|
try {
|
||||||
const response = await http.post(API_CONFIG.ENDPOINTS.DEVICES, device);
|
const response = await apiClient.devices.create(device);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('创建设备失败:', error);
|
console.error('创建设备失败:', error);
|
||||||
@@ -39,7 +38,7 @@ class DeviceService {
|
|||||||
*/
|
*/
|
||||||
async updateDevice(deviceId, device) {
|
async updateDevice(deviceId, device) {
|
||||||
try {
|
try {
|
||||||
const response = await http.put(`${API_CONFIG.ENDPOINTS.DEVICES}/${deviceId}`, device);
|
const response = await apiClient.devices.update(deviceId, device);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('更新设备失败:', error);
|
console.error('更新设备失败:', error);
|
||||||
@@ -54,7 +53,7 @@ class DeviceService {
|
|||||||
*/
|
*/
|
||||||
async deleteDevice(deviceId) {
|
async deleteDevice(deviceId) {
|
||||||
try {
|
try {
|
||||||
await http.delete(`${API_CONFIG.ENDPOINTS.DEVICES}/${deviceId}`);
|
await apiClient.devices.delete(deviceId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('删除设备失败:', error);
|
console.error('删除设备失败:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user