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

96 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 企业信息模块初始化与下拉数据拉取(绕过 DVA避免 loading 状态导致页面重渲染死循环)
*/
import {
fromPageResponse,
fromSingleResponse,
toDepartmentForm,
toOrgInfoForm,
toPageQuery,
toPositionForm,
} from "./adapter";
import { apiGet } from "./http";
import { clearOrgInfoId, getOrgInfoId, setOrgInfoId } from "./orgContext";
/** 机构信息管理页路径(无机构数据时唯一可访问页) */
export const ORG_INFO_PAGE_PATH = "/certificate/container/EnterpriseInfo/OrgInfo";
let ensureOrgPromise = null;
/** 最近一次 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;
}
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 });
}
}
if (ensureOrgPromise) {
return ensureOrgPromise;
}
ensureOrgPromise = fetchOrgInfoContext().finally(() => {
ensureOrgPromise = null;
});
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);
}