refactor(settings): 重构用户管理和设备管理页面

- 调整用户管理页面角色数据获取方法,使用 getRolesOptions 替代 getAllRoles
- 更新用户表格组件接收的角色数据属性名及类型
- 修改设备管理页面路由路径,从 /device/list 调整为 /devices
- 移除调试用 console.log 输出语句
- 添加选项类型 Options 接口定义
- 优化侧边栏导航结构与交互逻辑,支持父级菜单带链接可点击
- 引入日志模块用于 API 请求与响应记录
- 升级依赖包配置,移除 peer 标记
- 微调样式类名增强布局效果和用户体验
This commit is contained in:
Chaos
2025-11-29 09:02:00 +08:00
parent 2caa8f26a3
commit 0a0e6df66b
16 changed files with 170 additions and 142 deletions

View File

@@ -2,6 +2,7 @@
import type { HttpMethod, JsonObject, JsonValue } from '$lib/types/http.ts';
import type { ApiResult } from '$lib/types/api.ts';
import { log } from '$lib/log.ts';
interface RequestOptions extends Omit<RequestInit, 'method' | 'body'> {
@@ -13,8 +14,6 @@ const API_BASE_URL = import.meta.env.VITE_PUBLIC_API_URL || 'http://localhost:18
const normalizeHeaders = (headers?: HeadersInit):Record<string, string> =>{
const result:Record<string,string> = {};
console.log('normalizeHeaders', headers);
if (!headers){
return result;
}
@@ -36,7 +35,7 @@ const normalizeHeaders = (headers?: HeadersInit):Record<string, string> =>{
})
}
console.log('normalizeHeaders result:', result);
return result;
}
export class HttpError extends Error {
@@ -48,7 +47,6 @@ export class HttpError extends Error {
this.name = 'HttpError';
this.status = status;
this.details = details;
// 保持正确的原型链
if (Error.captureStackTrace) {
Error.captureStackTrace(this, HttpError);
@@ -63,10 +61,10 @@ const httpRequest = async <T>(
options: RequestOptions = {}
): Promise<ApiResult<T>> => {
const fullUrl = `${API_BASE_URL}${url}`;
log.info('API Request:', method, fullUrl)
const { body , headers, ...rest } = options;
const requestHeaders: Record<string, string> = normalizeHeaders(headers);
let requestBody: BodyInit | undefined;
@@ -85,7 +83,7 @@ const httpRequest = async <T>(
try {
log.debug('API Request Body:', requestBody)
const response = await fetch(fullUrl, {
method,
headers: requestHeaders,
@@ -94,24 +92,27 @@ const httpRequest = async <T>(
body: canHaveBody ? requestBody : undefined,
...rest
});
console.log('response', response);
if (!response.ok) {
let errorDetail;
try {
errorDetail = await response.json();
} catch (e) {
console.error('Error parsing JSON:', e);
errorDetail = await response.text();
}
const message = `HTTP Error ${response.status} (${response.statusText})`;
log.warn(message)
throw new HttpError(message, response.status, errorDetail);
}
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
return (await response.json()) as ApiResult<T>;
const res = await response.json();
log.info('API Response:', res)
return (res) as ApiResult<T>;
}
return { code: 200, msg: 'OK', data: null } ; // 这里的 as any 是为了兼容 T 可能是 null 的情况