/** * API 调用封装 * 统一处理与后端 REST 接口的交互,返回解析后的数据或抛出错误 */ const API = { /** * 通用请求方法 * @param {string} url - 请求路径 * @param {Object} options - fetch 选项 * @returns {Promise} 响应数据 */ async request(url, options = {}) { const headers = { "Content-Type": "application/json" }; // 自动携带认证 token const token = localStorage.getItem("registry_token"); if (token) { headers["Authorization"] = `Bearer ${token}`; } const opts = { headers, ...options }; if (opts.body && typeof opts.body !== "string") { opts.body = JSON.stringify(opts.body); } const res = await fetch(url, opts); // 401 未登录,跳转登录页 if (res.status === 401 && !url.includes("/api/auth/")) { localStorage.removeItem("registry_token"); localStorage.removeItem("registry_user"); window.location.href = "/login.html"; throw new Error("登录已过期,请重新登录"); } const json = await res.json(); if (!json.success) { throw new Error(json.message || "请求失败"); } return json.data; }, // ========== 认证 ========== login(username, password) { return this.request("/api/auth/login", { method: "POST", body: { username, password } }); }, logout() { return this.request("/api/auth/logout", { method: "POST" }); }, getMe() { return this.request("/api/auth/me"); }, // ========== 项目 ========== getProjects() { return this.request("/api/projects"); }, getProject(id) { return this.request(`/api/projects/${id}`); }, createProject(data) { return this.request("/api/projects", { method: "POST", body: data }); }, deleteProject(id) { return this.request(`/api/projects/${id}`, { method: "DELETE" }); }, updateMappings(id, mappings) { return this.request(`/api/projects/${id}/mappings`, { method: "PUT", body: mappings }); }, // ========== 配置 ========== getConfigs(projectId) { return this.request(`/api/projects/${projectId}/configs`); }, createConfig(projectId, data) { return this.request(`/api/projects/${projectId}/configs`, { method: "POST", body: data }); }, updateConfig(projectId, configId, data) { return this.request(`/api/projects/${projectId}/configs/${configId}`, { method: "PUT", body: data }); }, deleteConfig(projectId, configId) { return this.request(`/api/projects/${projectId}/configs/${configId}`, { method: "DELETE" }); }, // ========== 模式 ========== getMode() { return this.request("/api/mode"); }, setMode(mode) { return this.request("/api/mode", { method: "PUT", body: { mode } }); }, // ========== 用户管理 ========== getUsers() { return this.request("/api/users"); }, createUser(data) { return this.request("/api/users", { method: "POST", body: data }); }, updateUser(id, data) { return this.request(`/api/users/${id}`, { method: "PUT", body: data }); }, resetPassword(id, password) { return this.request(`/api/users/${id}/password`, { method: "PUT", body: { password } }); }, deleteUser(id) { return this.request(`/api/users/${id}`, { method: "DELETE" }); }, // ========== Token 管理 ========== getTokens() { return this.request("/api/tokens"); }, createToken(data) { return this.request("/api/tokens", { method: "POST", body: data }); }, updateToken(id, data) { return this.request(`/api/tokens/${id}`, { method: "PUT", body: data }); }, revokeToken(id) { return this.request(`/api/tokens/${id}/revoke`, { method: "PUT" }); }, deleteToken(id) { return this.request(`/api/tokens/${id}`, { method: "DELETE" }); }, };