一单四制优化及加入工作流

This commit is contained in:
王利强
2026-07-27 09:27:22 +08:00
parent 455fd4fc07
commit 7c3285ed5a
725 changed files with 19668 additions and 2921 deletions

142
utils/userInfo.js Normal file
View File

@@ -0,0 +1,142 @@
/** 将 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);
}
/** 解析用于权限判断的角色标识,优先当前身份 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;
}