safety-eval-service-frontend/src/api/enterpriseInfo/http.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2026-06-23 18:07:30 +08:00
import { Get, Post } from "@cqsjjb/jjb-common-lib/http";
function parseHttpError(err) {
const data = err?.response?.data;
if (data && typeof data === "object") {
return {
success: false,
code: data.code ?? data.errCode,
message: data.message || data.errMessage || "操作失败",
data: data.data ?? null,
};
}
return { success: false, message: err?.message || "请求失败" };
}
/** 底层 HTTP供 adapter 层组合调用(不可嵌套 declareRequest */
export async function apiGet(path, params = {}) {
try {
return await Get(path, params);
}
catch (err) {
return parseHttpError(err);
}
}
/** @ 前缀表示 Post 请求体不包裹 request 字段 */
export async function apiPost(path, data = {}) {
const url = path.startsWith("@") ? path : `@${path}`;
try {
return await Post(url, data);
}
catch (err) {
return parseHttpError(err);
}
}
export async function apiPostDelete(path, id) {
const url = `${path}?id=${encodeURIComponent(id)}`;
try {
return await Post(url, {});
}
catch (err) {
return parseHttpError(err);
}
}
export function safePageResult(mapper) {
return async (params) => {
try {
const res = await mapper(params);
return res;
}
catch (err) {
console.warn("[enterpriseInfo/safePageResult]", err);
return { success: false, data: [], totalCount: 0 };
}
};
}
export function safeAction(mapper) {
return async (params) => {
try {
const res = await mapper(params);
if (res && res.success === false) {
return {
success: false,
message: res.message || res.msg || "操作失败",
code: res.code,
};
}
return res;
}
catch (err) {
console.warn("[enterpriseInfo/safeAction]", err);
return parseHttpError(err);
}
};
}