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

96 lines
3.1 KiB
JavaScript
Raw Normal View History

2026-06-25 16:30:37 +08:00
/**
* 企业信息模块初始化与下拉数据拉取绕过 DVA避免 loading 状态导致页面重渲染死循环
*/
import {
fromPageResponse,
fromSingleResponse,
toDepartmentForm,
toOrgInfoForm,
toPageQuery,
toPositionForm,
} from "./adapter";
import { apiGet } from "./http";
2026-06-26 14:27:49 +08:00
import { clearOrgInfoId, getOrgInfoId, setOrgInfoId } from "./orgContext";
/** 机构信息管理页路径(无机构数据时唯一可访问页) */
export const ORG_INFO_PAGE_PATH = "/certificate/container/EnterpriseInfo/OrgInfo";
2026-06-25 16:30:37 +08:00
let ensureOrgPromise = null;
2026-06-26 14:27:49 +08:00
/** 最近一次 getInfo 转换结果,供机构信息页复用,避免重复请求 */
let lastOrgInfoDetail = null;
export function isOrgInfoPage(path = window.location.pathname) {
return path === ORG_INFO_PAGE_PATH || path.startsWith(`${ORG_INFO_PAGE_PATH}/`);
}
export function getOrgInfoDetail() {
return lastOrgInfoDetail;
}
export function setOrgInfoDetailCache(detail) {
lastOrgInfoDetail = detail;
}
2026-06-25 16:30:37 +08:00
2026-06-26 14:27:49 +08:00
function fetchOrgInfoContext() {
return apiGet("/safety-eval/org-info/getInfo", {}, {}, { includeOrgContext: false })
.then((res) => {
lastOrgInfoDetail = fromSingleResponse(res, toOrgInfoForm);
const id = lastOrgInfoDetail?.data?.id ?? res?.data?.id;
if (id) {
setOrgInfoId(id);
return { hasOrg: true, orgInfoId: String(id), detail: lastOrgInfoDetail };
}
clearOrgInfoId();
return { hasOrg: false, orgInfoId: null, detail: lastOrgInfoDetail };
})
.catch((err) => {
console.warn("[ensureOrgContext] getInfo failed:", err);
clearOrgInfoId();
lastOrgInfoDetail = { success: false, data: null };
return { hasOrg: false, orgInfoId: null, detail: lastOrgInfoDetail, networkError: true };
});
}
/**
* 确保已缓存机构 id子表分页/请求头 orgInfoId 依赖此值
* @param {{ force?: boolean }} options force=true 时忽略缓存并重新 getInfo进入企业信息模块时使用
* @returns {Promise<{ hasOrg: boolean, orgInfoId: string|null, detail?: object, networkError?: boolean }>}
*/
export function ensureOrgContext(options = {}) {
const { force = false } = options;
if (!force) {
const cached = getOrgInfoId();
if (cached) {
return Promise.resolve({ hasOrg: true, orgInfoId: cached, detail: lastOrgInfoDetail });
}
2026-06-25 16:30:37 +08:00
}
2026-06-26 14:27:49 +08:00
if (ensureOrgPromise) {
return ensureOrgPromise;
2026-06-25 16:30:37 +08:00
}
2026-06-26 14:27:49 +08:00
ensureOrgPromise = fetchOrgInfoContext().finally(() => {
ensureOrgPromise = null;
});
2026-06-25 16:30:37 +08:00
return ensureOrgPromise;
}
export async function fetchOrgDepartmentPage(params = {}) {
await ensureOrgContext();
const query = toPageQuery(params, { likeDeptName: "deptName" });
const res = await apiGet("/safety-eval/org-department/page", query);
return fromPageResponse(res, toDepartmentForm);
}
export async function fetchOrgPositionPage(params = {}) {
await ensureOrgContext();
const query = toPageQuery(params, {
eqDeptId: "deptId",
likePositionName: "positionName",
});
const res = await apiGet("/safety-eval/org-position/page", query);
return fromPageResponse(res, toPositionForm);
}