首次提交
This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
// API服务层
|
||||
import axios from 'axios';
|
||||
import { API_BASE_URL, API_ENDPOINTS } from '../utils/constants.js';
|
||||
|
||||
// 获取设备MAC地址
|
||||
export function getDeviceMacAddress() {
|
||||
const os = require('os');
|
||||
const networkInterfaces = os.networkInterfaces();
|
||||
for (const name of Object.keys(networkInterfaces)) {
|
||||
for (const net of networkInterfaces[name]) {
|
||||
if (net.mac && net.mac !== '00:00:00:00:00:00') {
|
||||
return net.mac;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
// 创建axios实例
|
||||
const http = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
http.interceptors.request.use(
|
||||
config => {
|
||||
// 可以在这里添加token等认证信息
|
||||
return config;
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器
|
||||
http.interceptors.response.use(
|
||||
response => {
|
||||
return response;
|
||||
},
|
||||
error => {
|
||||
console.error('API请求错误:', error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 认证相关API
|
||||
export const authAPI = {
|
||||
// 账号密码登录
|
||||
login: (data) => {
|
||||
const mac = getDeviceMacAddress();
|
||||
return http.post(API_ENDPOINTS.AUTH.LOGIN, {
|
||||
...data,
|
||||
mac
|
||||
});
|
||||
},
|
||||
|
||||
// 手机号验证码登录
|
||||
phoneLogin: (data) => {
|
||||
const mac = getDeviceMacAddress();
|
||||
return http.post(API_ENDPOINTS.AUTH.PHONE_LOGIN, {
|
||||
...data,
|
||||
mac
|
||||
});
|
||||
},
|
||||
|
||||
// 企业登录
|
||||
companyLogin: (data) => {
|
||||
return http.post(API_ENDPOINTS.AUTH.COMPANY_LOGIN, data);
|
||||
},
|
||||
|
||||
// 设置登录缓存
|
||||
setLoginCache: (data) => {
|
||||
const mac = getDeviceMacAddress();
|
||||
return http.post(API_ENDPOINTS.AUTH.SET_LOGIN_CACHE, {
|
||||
...data,
|
||||
mac
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 用户相关API
|
||||
export const userAPI = {
|
||||
// 获取用户信息
|
||||
getUserInfo: () => {
|
||||
return http.put(API_ENDPOINTS.USER.INFO, {});
|
||||
},
|
||||
|
||||
// 获取公司列表
|
||||
getCompanyList: () => {
|
||||
return http.get(API_ENDPOINTS.USER.COMPANY_LIST, {
|
||||
params: {},
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 根据登录获取用户ID
|
||||
getUserIdByLogin: (data) => {
|
||||
const mac = getDeviceMacAddress();
|
||||
return http.post(API_ENDPOINTS.USER.GET_USER_ID, {
|
||||
...data,
|
||||
mac,
|
||||
verify_type: 3
|
||||
});
|
||||
},
|
||||
|
||||
// 修改密码
|
||||
changePassword: (data) => {
|
||||
return http.post(API_ENDPOINTS.USER.CHANGE_PASSWORD, data, {
|
||||
loading: true
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 公司相关API
|
||||
export const companyAPI = {
|
||||
// 获取公司用户信息
|
||||
getCompanyUserInfo: () => {
|
||||
return http.post(API_ENDPOINTS.COMPANY.INFO, {}, {
|
||||
loading: true
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 设备相关API
|
||||
export const deviceAPI = {
|
||||
// 获取所有设备列表
|
||||
getDeviceList: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.LIST, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取设备类型列表
|
||||
getDeviceTypes: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.TYPES, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 连接设备
|
||||
connectDevice: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.CONNECT, {
|
||||
params,
|
||||
loading: params.loading || false,
|
||||
isErrorMessage: false
|
||||
});
|
||||
},
|
||||
|
||||
// 获取设备使用日志
|
||||
getDeviceLogs: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.LOGS, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 添加使用申请
|
||||
addUseApprove: (params) => {
|
||||
return http.post(API_ENDPOINTS.DEVICE.APPROVE, null, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 撤销使用申请
|
||||
revokeUseApprove: (params) => {
|
||||
return http.post(API_ENDPOINTS.DEVICE.REVOKE, null, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取用途列表
|
||||
getPurposeList: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.PURPOSE_LIST, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取设备使用时间列表
|
||||
getDeviceTimeList: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.TIME_LIST, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取端口列表
|
||||
getPortList: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.PORT_LIST, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取端口状态
|
||||
getPortStatus: (params) => {
|
||||
return http.get(API_ENDPOINTS.DEVICE.PORT_STATUS, {
|
||||
params,
|
||||
loading: true
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 通用API
|
||||
export const commonAPI = {
|
||||
// 获取验证码
|
||||
getVerificationCode: (data) => {
|
||||
return http.post(API_ENDPOINTS.COMMON.VERIFICATION_CODE, data, {
|
||||
loading: true
|
||||
});
|
||||
},
|
||||
|
||||
// 获取微信二维码
|
||||
getWechatQRCode: () => {
|
||||
return http.get(API_ENDPOINTS.COMMON.WECHAT_QRCODE, {
|
||||
params: {}
|
||||
});
|
||||
},
|
||||
|
||||
// 检查扫码状态
|
||||
checkScanStatus: (params) => {
|
||||
return http.get(API_ENDPOINTS.COMMON.CHECK_SCAN, {
|
||||
params
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 统一的API响应处理
|
||||
export function handleApiResponse(response, successMessage = '操作成功') {
|
||||
if (response.data && response.data.code === 200) {
|
||||
return {
|
||||
success: true,
|
||||
data: response.data.data,
|
||||
message: response.data.message || successMessage
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: response.data?.message || '操作失败',
|
||||
code: response.data?.code
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 统一的错误处理
|
||||
export function handleApiError(error) {
|
||||
console.error('API调用失败:', error);
|
||||
|
||||
let message = '网络请求失败';
|
||||
|
||||
if (error.response) {
|
||||
// 服务器返回了错误状态码
|
||||
message = error.response.data?.message || `服务器错误: ${error.response.status}`;
|
||||
} else if (error.request) {
|
||||
// 请求发出但没有收到响应
|
||||
message = '网络连接失败,请检查网络';
|
||||
} else {
|
||||
// 其他错误
|
||||
message = error.message || '请求失败';
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user