Files
erp_sb/electron-vue-template/src/renderer/api/http.ts
zhangzijienbplus 17f03c3ade refactor(auth):重构认证服务并移除冗余代码
- 移除了 AuthServiceImpl 中的登录、注册、token 验证等方法,仅保留错误上报和客户端信息功能
- 删除了设备注册和离线通知相关逻辑
- 移除了 IAuthService 接口中的登录、注册、验证 token 等方法定义
- 清理了 AccountManager.vue 中的无关注释文字-优化了阿里巴巴1688 服务中的图片上传处理逻辑- 移除了 AmazonScrapingServiceImpl 中未使用的日志导入和空行
- 统一了 Vue 组件中的同步导入方式,替换异步组件定义
- 更新了应用配置文件中的服务器地址和懒加载设置
- 新增缓存管理服务用于统一清理各类缓存数据
- 优化了设备 IP 地址获取逻辑并在注册时传递给后端- 调整了构建配置以减小安装包体积并支持多语言
- 修改了主进程窗口加载逻辑以适配开发与生产环境- 添加了全局样式限制图片预览器尺寸
- 移除了设备 ID 测试类和部分无用的正则表达式导入
2025-10-20 18:01:40 +08:00

125 lines
3.6 KiB
TypeScript

// HTTP 工具:统一管理后端服务配置和请求
export type HttpMethod = 'GET' | 'POST' | 'DELETE';
// 集中管理所有后端服务配置
export const CONFIG = {
CLIENT_BASE: 'http://localhost:8081',
// RUOYI_BASE: 'http://192.168.1.89:8085',
RUOYI_BASE: 'http://8.138.23.49:8085',
SSE_URL: 'http://8.138.23.49:8085/monitor/account/events'
} as const;
function resolveBase(path: string): string {
// RuoYi 后端路径:鉴权、设备、反馈、版本、工具
if (path.startsWith('/monitor/') || path.startsWith('/system/') || path.startsWith('/tool/banma')) {
return CONFIG.RUOYI_BASE;
}
return CONFIG.CLIENT_BASE;
}
function buildQuery(params?: Record<string, unknown>): string {
if (!params) return '';
const query = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value != null) query.append(key, String(value));
});
return query.toString() ? `?${query}` : '';
}
async function request<T>(path: string, options: RequestInit): Promise<T> {
// 获取token
let token = '';
try {
const tokenModule = await import('../utils/token');
token = tokenModule.getToken() || '';
} catch (e) {
console.warn('获取token失败:', e);
}
const res = await fetch(`${resolveBase(path)}${path}`, {
credentials: 'omit',
cache: 'no-store',
...options,
headers: {
'Content-Type': 'application/json',
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...options.headers
}
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(text || `HTTP ${res.status}`);
}
const contentType = res.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
const json: any = await res.json();
// 业务状态码判断:支持两种格式
// - erp_client_sb (本地服务): code=0 表示成功
// - RuoYi 后端: code=200 表示成功
if (json.code !== undefined && json.code !== 0 && json.code !== 200) {
throw new Error(json.msg || '请求失败');
}
return json as T;
}
return (await res.text()) as unknown as T;
}
export const http = {
get<T>(path: string, params?: Record<string, unknown>) {
return request<T>(`${path}${buildQuery(params)}`, { method: 'GET' });
},
post<T>(path: string, body?: unknown) {
return request<T>(path, {
method: 'POST',
body: body ? JSON.stringify(body) : undefined
});
},
delete<T>(path: string) {
return request<T>(path, { method: 'DELETE' });
},
async upload<T>(path: string, form: FormData) {
// 获取token
let token = '';
try {
const tokenModule = await import('../utils/token');
token = tokenModule.getToken() || '';
} catch (e) {
console.warn('获取token失败:', e);
}
const headers: Record<string, string> = {};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return fetch(`${resolveBase(path)}${path}`, {
method: 'POST',
body: form,
credentials: 'omit',
cache: 'no-store',
headers
}).then(async res => {
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(text || `HTTP ${res.status}`);
}
const contentType = res.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
const json: any = await res.json();
if (json.code !== undefined && json.code !== 0 && json.code !== 200) {
throw new Error(json.msg || '请求失败');
}
return json as T;
}
return (await res.text()) as unknown as T;
});
}
};