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

104 lines
3.7 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";
export const MATERIAL_REPORT_PAGE_PATH = "/certificate/container/EnterpriseInfo/MaterialReport";
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}/`) || path === MATERIAL_REPORT_PAGE_PATH || path.startsWith(`${MATERIAL_REPORT_PAGE_PATH}/`);
}
export function getOrgInfoDetail() {
return lastOrgInfoDetail;
}
export function setOrgInfoDetailCache(detail) {
lastOrgInfoDetail = detail;
}
function fetchOrgInfoContext() {
const cachedBefore = getOrgInfoId();
return apiGet("/safetyEval/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 };
}
// 本地联调getInfo 无 SSO 关联时,保留 session 中已有 orgInfoId
if (cachedBefore) {
return { hasOrg: true, orgInfoId: cachedBefore, 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("/safetyEval/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("/safetyEval/org-position/page", query);
return fromPageResponse(res, toPositionForm);
}