safety-eval-service-frontend/src/utils/enterpriseInfo/adapter.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2026-07-07 17:14:09 +08:00
/** 企业信息管理:前后端字段与分页格式适配 */
import { asId, isIdFieldName, normalizeQueryIds } from "./idUtil";
import { withOrgId } from "./orgContext";
function parseAttachmentUrls(urls, defaultName = "附件.pdf") {
if (!urls) {
return [];
}
const raw = String(urls).trim();
// JSON 数组字符串:如 '[{"url":"...","uid":"...","name":"...","status":"done"}]'
if (raw.startsWith("[") && raw.endsWith("]")) {
try {
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) {
return parsed
.filter((item) => item?.url)
.map((item) => ({
name: item.name || `${defaultName.replace(".pdf", "")}1`,
2026-07-14 16:38:57 +08:00
fileName:
item.fileName ||
item.name ||
`${defaultName.replace(".pdf", "")}1`,
2026-07-07 17:14:09 +08:00
url: item.url,
uid: item.uid || undefined,
status: item.status || "done",
}));
}
} catch (_) {
// JSON 解析失败,回退到逗号分隔处理
}
}
// 旧格式:逗号分隔的 URL 字符串
return raw
.split(",")
.map((url) => url.trim())
.filter(Boolean)
.map((url, index) => ({
name: `${defaultName.replace(".pdf", "")}${index + 1}.pdf`,
fileName: `${defaultName.replace(".pdf", "")}${index + 1}.pdf`,
url,
}));
}
/** 分页查询参数:子表 org_id → org_info.id机构信息管理 getInfo 不走此方法 */
export function toPageQuery(params = {}, fieldMap = {}) {
const query = {
current: params.pageIndex ?? params.current ?? 1,
size: params.pageSize ?? params.size ?? 10,
};
Object.entries(fieldMap).forEach(([from, to]) => {
const value = params[from];
if (value !== undefined && value !== null && value !== "") {
query[to] = isIdFieldName(to) ? asId(value) : value;
}
});
return withOrgId(normalizeQueryIds(query));
}
export function fromPageResponse(res, mapItem) {
if (!res) {
return { success: false, data: [], totalCount: 0 };
}
const list = Array.isArray(res.data) ? res.data : [];
return {
...res,
data: mapItem ? list.map(mapItem) : list,
totalCount: res.totalCount ?? res.total ?? list.length,
};
}
export function fromSingleResponse(res, mapItem) {
if (!res) {
return { success: false, data: null };
}
return {
...res,
data: res.data && mapItem ? mapItem(res.data) : res.data,
};
}
/** 监管端机构账号:后端 state 0=启用1=禁用 */
export function isOrgAccountEnabled(record = {}) {
return Number(record.state) === 0;
}