Files
threeonecheck_web/utils/userInfo.js

170 lines
5.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 将 profile/detail 接口数据映射为本地 userInfo 结构 */
function normalizeUserIdentity(raw) {
if (!raw) return null;
if (typeof raw === 'string') {
try {
return JSON.parse(raw);
} catch (e) {
return null;
}
}
return raw;
}
/** 从本地读取切换身份时缓存的 currentIdentity */
export function getCurrentIdentityFromStorage() {
try {
const raw = uni.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();
}
/** 从 roles 数组解析 roleKey */
function resolveRoleKey(roles, fallback = '') {
if (!roles?.length) return normalizeRoleKey(fallback);
const first = roles[0];
if (typeof first === 'string') return normalizeRoleKey(first);
return normalizeRoleKey(first.roleKey || first.role || fallback);
}
/** 从 userInfo 中读取当前身份 roleKey仅取 userIdentity.roleKey */
export function resolveUserIdentityRoleKey(userInfo) {
if (!userInfo) return '';
const identity = normalizeUserIdentity(userInfo.userIdentity);
return normalizeRoleKey(identity?.roleKey);
}
/** 从 userInfo 中读取组织实体类型0/1=政府2/3=企业及企业下部门 */
export function resolveOrganizationEntityType(userInfo) {
if (!userInfo) return null;
const identity = normalizeUserIdentity(userInfo.userIdentity);
const currentIdentity = getCurrentIdentityFromStorage();
const merged = identity || currentIdentity;
if (merged?.organizationEntityType == null || merged?.organizationEntityType === '') return null;
const entityType = Number(merged.organizationEntityType);
return Number.isNaN(entityType) ? null : entityType;
}
const MANAGER_ROLE_KEYS = ['admin', 'manage'];
/** 是否为政府类型组织的管理员organizationEntityType=0/1 且 roleKey 为 admin/manage */
export function isGovernmentEntityAdmin(userInfo) {
const entityType = resolveOrganizationEntityType(userInfo);
if (entityType !== 0 && entityType !== 1) return false;
return MANAGER_ROLE_KEYS.includes(resolveUserIdentityRoleKey(userInfo));
}
/** 解析用于权限判断的角色标识,优先当前身份 roleKey */
export function resolveUserRoleKey(userInfo) {
if (!userInfo) return '';
const identity = normalizeUserIdentity(userInfo.userIdentity);
const currentIdentity = getCurrentIdentityFromStorage();
const identityRoleKey = normalizeRoleKey(identity?.roleKey);
if (identityRoleKey) return identityRoleKey;
const currentRoleKey = normalizeRoleKey(currentIdentity?.roleKey);
if (currentRoleKey) return currentRoleKey;
const storedRole = normalizeRoleKey(userInfo.role);
if (storedRole) return storedRole;
const nameHint = [
identity?.roleName,
identity?.identityName,
userInfo.identityName,
currentIdentity?.roleName,
currentIdentity?.identityName
].filter(Boolean).join(' ');
if (/审批|approval/i.test(nameHint)) return 'approval';
return '';
}
/** 解析用于展示的身份名称 */
export function resolveIdentityName(profile, userIdentity) {
const identity = userIdentity || normalizeUserIdentity(profile?.userIdentity);
if (identity?.identityName) {
return identity.identityName;
}
if (profile?.identityName) {
return profile.identityName;
}
if (identity) {
const parts = [identity.deptName, identity.roleName, identity.postName].filter(Boolean);
if (parts.length) return parts.join('-');
}
return '';
}
export function mapProfileToUserInfo(profile) {
if (!profile) return {};
const userIdentity = normalizeUserIdentity(profile.userIdentity);
const currentIdentity = getCurrentIdentityFromStorage();
const roleKey = normalizeRoleKey(
userIdentity?.roleKey || currentIdentity?.roleKey || resolveRoleKey(profile.roles, '')
);
return {
userId: profile.userId || '',
username: profile.userName || '',
nickName: profile.nickName || '',
deptId: userIdentity?.deptId ?? currentIdentity?.deptId ?? profile.deptId ?? '',
deptName: userIdentity?.deptName ?? currentIdentity?.deptName ?? profile.deptName ?? '',
role: roleKey,
avatar: profile.avatar || '',
phone: profile.phonenumber || profile.phone || '',
identityName: resolveIdentityName(profile, userIdentity),
userIdentity: userIdentity || currentIdentity
};
}
/** 写入本地 userInfo 缓存 */
export function saveUserInfoToStorage(profile) {
const userInfo = mapProfileToUserInfo(profile);
uni.setStorageSync('userInfo', JSON.stringify(userInfo));
return userInfo;
}
/** 将 profile 数据应用到页面 reactive 对象(逐项赋值,确保小程序视图更新) */
export function applyProfileToUserInfo(target, profile) {
const mapped = mapProfileToUserInfo(profile);
Object.keys(mapped).forEach((key) => {
target[key] = mapped[key];
});
uni.setStorageSync('userInfo', JSON.stringify(mapped));
return mapped;
}
/** 从本地缓存应用到页面 reactive 对象 */
export 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?.deptId || '',
deptName: stored.deptName || 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;
}