Files
registry/public/js/api.js
T
jif2.zhang dc4c67e386
build-and-deploy / build-and-deploy (push) Successful in 43s
feat: 访问记录后台分页,支持时间范围/操作/键过滤
2026-09-21 10:49:55 +08:00

161 lines
4.4 KiB
JavaScript

/**
* API 调用封装
* 统一处理与后端 REST 接口的交互,返回解析后的数据或抛出错误
*/
const API = {
/**
* 通用请求方法
* @param {string} url - 请求路径
* @param {Object} options - fetch 选项
* @returns {Promise<any>} 响应数据
*/
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 });
},
updateProject(id, data) {
return this.request(`/api/projects/${id}`, { method: "PUT", body: data });
},
// ========== 配置 ==========
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" });
},
getAudit(projectId, filters = {}) {
const qs = new URLSearchParams();
for (const k of ["key", "action", "start", "end", "page", "pageSize"]) {
if (filters[k] !== undefined && filters[k] !== null && filters[k] !== "") {
qs.append(k, filters[k]);
}
}
const str = qs.toString();
return this.request(`/api/projects/${projectId}/configs/audit${str ? "?" + str : ""}`);
},
// ========== 模式 ==========
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" });
},
};