"use strict"; const common_vendor = require("../common/vendor.js"); function normalizeUserIdentity(raw) { if (!raw) return null; if (typeof raw === "string") { try { return JSON.parse(raw); } catch (e) { return null; } } return raw; } function getCurrentIdentityFromStorage() { try { const raw = common_vendor.index.getStorageSync("currentIdentity"); if (!raw) return null; return normalizeUserIdentity(raw); } catch (e) { return null; } } function normalizeRoleKey(value) { if (value == null || value === "") return ""; return String(value).trim(); } function resolveRoleKey(roles, fallback = "") { if (!(roles == null ? void 0 : roles.length)) return normalizeRoleKey(fallback); const first = roles[0]; if (typeof first === "string") return normalizeRoleKey(first); return normalizeRoleKey(first.roleKey || first.role || fallback); } function resolveUserIdentityRoleKey(userInfo) { if (!userInfo) return ""; const identity = normalizeUserIdentity(userInfo.userIdentity); return normalizeRoleKey(identity == null ? void 0 : identity.roleKey); } function resolveUserRoleKey(userInfo) { if (!userInfo) return ""; const identity = normalizeUserIdentity(userInfo.userIdentity); const currentIdentity = getCurrentIdentityFromStorage(); const identityRoleKey = normalizeRoleKey(identity == null ? void 0 : identity.roleKey); if (identityRoleKey) return identityRoleKey; const currentRoleKey = normalizeRoleKey(currentIdentity == null ? void 0 : currentIdentity.roleKey); if (currentRoleKey) return currentRoleKey; const storedRole = normalizeRoleKey(userInfo.role); if (storedRole) return storedRole; const nameHint = [ identity == null ? void 0 : identity.roleName, identity == null ? void 0 : identity.identityName, userInfo.identityName, currentIdentity == null ? void 0 : currentIdentity.roleName, currentIdentity == null ? void 0 : currentIdentity.identityName ].filter(Boolean).join(" "); if (/审批|approval/i.test(nameHint)) return "approval"; return ""; } function resolveIdentityName(profile, userIdentity) { const identity = userIdentity || normalizeUserIdentity(profile == null ? void 0 : profile.userIdentity); if (identity == null ? void 0 : identity.identityName) { return identity.identityName; } if (profile == null ? void 0 : profile.identityName) { return profile.identityName; } if (identity) { const parts = [identity.deptName, identity.roleName, identity.postName].filter(Boolean); if (parts.length) return parts.join("-"); } return ""; } function mapProfileToUserInfo(profile) { if (!profile) return {}; const userIdentity = normalizeUserIdentity(profile.userIdentity); const currentIdentity = getCurrentIdentityFromStorage(); const roleKey = normalizeRoleKey( (userIdentity == null ? void 0 : userIdentity.roleKey) || (currentIdentity == null ? void 0 : currentIdentity.roleKey) || resolveRoleKey(profile.roles, "") ); return { userId: profile.userId || "", username: profile.userName || "", nickName: profile.nickName || "", deptId: (userIdentity == null ? void 0 : userIdentity.deptId) ?? (currentIdentity == null ? void 0 : currentIdentity.deptId) ?? profile.deptId ?? "", deptName: (userIdentity == null ? void 0 : userIdentity.deptName) ?? (currentIdentity == null ? void 0 : currentIdentity.deptName) ?? profile.deptName ?? "", role: roleKey, avatar: profile.avatar || "", phone: profile.phonenumber || profile.phone || "", identityName: resolveIdentityName(profile, userIdentity), userIdentity: userIdentity || currentIdentity }; } function saveUserInfoToStorage(profile) { const userInfo = mapProfileToUserInfo(profile); common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo)); return userInfo; } function applyProfileToUserInfo(target, profile) { const mapped = mapProfileToUserInfo(profile); Object.keys(mapped).forEach((key) => { target[key] = mapped[key]; }); common_vendor.index.setStorageSync("userInfo", JSON.stringify(mapped)); return mapped; } function applyStoredUserInfo(target, stored) { if (!stored) return; const userIdentity = normalizeUserIdentity(stored.userIdentity); const currentIdentity = getCurrentIdentityFromStorage(); const mergedIdentity = userIdentity || currentIdentity; const mapped = { userId: stored.userId || "", username: stored.username || "", nickName: stored.nickName || "", deptId: stored.deptId || (mergedIdentity == null ? void 0 : mergedIdentity.deptId) || "", deptName: stored.deptName || (mergedIdentity == null ? void 0 : mergedIdentity.deptName) || "", role: resolveUserRoleKey({ role: stored.role, userIdentity: mergedIdentity, identityName: stored.identityName }), avatar: stored.avatar || "", phone: stored.phone || "", userIdentity: mergedIdentity, identityName: stored.identityName || resolveIdentityName(stored, mergedIdentity) }; Object.keys(mapped).forEach((key) => { target[key] = mapped[key]; }); return mapped; } exports.applyProfileToUserInfo = applyProfileToUserInfo; exports.applyStoredUserInfo = applyStoredUserInfo; exports.getCurrentIdentityFromStorage = getCurrentIdentityFromStorage; exports.resolveIdentityName = resolveIdentityName; exports.resolveUserIdentityRoleKey = resolveUserIdentityRoleKey; exports.saveUserInfoToStorage = saveUserInfoToStorage; //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/userInfo.js.map