| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /**
- * 当前登录用户所属机构(与 PC 端 src/utils/current-org.js 同一口径):
- * 「所属机构」下拉选项一律取登录缓存 loginChangeGroupVOList,
- * 不再调 /main/group/getGroupList 全量接口——普通账号不该看到其它公司。
- *
- * 移动端登录后把 /main/user/login 的返回整体存在 storage 的 userInfo 里,
- * 与 PC vuex 里 user.info 同源,故直接读 loginChangeGroupVOList。
- */
- import { pickLegalEntities } from "@/utils/hr/organization-tree";
- /** 是否有效机构 id(0 视为无效) */
- function isValidId(value) {
- return value !== undefined && value !== null && value !== "" && Number(value) !== 0;
- }
- /** 登录缓存中的可选机构列表 → [{ id, name }](即当前账号所属公司) */
- export function getLoginGroupOptions() {
- const info = uni.getStorageSync("userInfo") || {};
- const raw = info.raw || {};
- const list = info.loginChangeGroupVOList || raw.loginChangeGroupVOList || [];
- return (Array.isArray(list) ? list : [])
- .filter((item) => item && isValidId(item.groupId))
- .map((item) => ({
- id: item.groupId,
- name: item.groupName || item.name || item.organName || item.orgName || "",
- }));
- }
- /**
- * 当前登录用户所属机构 id:
- * 优先取登录缓存机构列表第一条(用户所属多个机构时默认第一个),
- * 缓存缺失时回落 userInfo.currentGroupId(登录/切换机构时写入)。
- */
- export function getCurrentOrgId() {
- const options = getLoginGroupOptions();
- if (options.length) return options[0].id;
- const info = uni.getStorageSync("userInfo") || {};
- const raw = info.raw || {};
- const current = info.currentGroupId != null ? info.currentGroupId : raw.currentGroupId;
- return isValidId(current) ? current : null;
- }
- /** 当前登录用户所属机构 { id, name } */
- export function getCurrentOrg() {
- const id = getCurrentOrgId();
- const matched = getLoginGroupOptions().find((item) => String(item.id) === String(id));
- return matched || { id, name: "" };
- }
- /**
- * 「所属机构」下拉选项(口径对齐 PC EmployeeArchiveForm.legalEntities):
- * ① 主源:登录缓存 loginChangeGroupVOList(当前账号所属公司),**不再用全量机构树**;
- * ② 缓存里只有 id 没有名称时(老缓存/接口缺字段),才回落到组织树里的集团/公司节点,
- * 保证下拉可读可选,而不是显示成一串 id;
- * ③ 当前值不在候选中时(编辑老档案,原机构已不在账号所属范围)补一条兜底项,
- * 避免下拉只显示 ID、且不会因此改掉已保存的值。
- * @param {Array} orgList 组织树全量列表(仅用于 ②③ 兜底,可传空)
- * @param {string|number} currentId 当前编辑中的机构 id
- * @param {string} currentName 当前编辑中的机构名称(组织树里也找不到时兜底显示)
- * @returns {Array<{id:string, name:string}>}
- */
- export function getOrgOptionsWithFallback(orgList = [], currentId = "", currentName = "") {
- const tree = Array.isArray(orgList) ? orgList : [];
- const companiesFromTree = () =>
- pickLegalEntities(tree).map((item) => ({ id: String(item.id), name: item.name || "" }));
- const nameFromTree = (id) => {
- const hit = companiesFromTree().find((item) => String(item.id) === String(id));
- return hit ? hit.name : "";
- };
- let list;
- const cached = getLoginGroupOptions();
- if (cached.length) {
- // 机构范围锁在登录缓存里;只是名称缺失(老缓存/接口少字段)时按 id 从组织树补名,
- // 不因为“没名称”就把候选扩大到全部公司(这点比 PC 更严一点)
- list = cached.map((item) => ({
- id: String(item.id),
- name: item.name || nameFromTree(item.id),
- }));
- } else {
- // 完全没有登录缓存(半登录态/老缓存)才回落组织树,否则下拉会是空的
- list = companiesFromTree();
- console.log(
- "[current-org] 登录缓存无机构列表,回落组织树:",
- list.map((item) => item.id).join(",") || "-"
- );
- }
- const id = currentId == null || currentId === "" ? "" : String(currentId);
- if (id && !list.some((item) => String(item.id) === id)) {
- // 编辑老档案:原机构已不在账号所属范围,补一条兜底项保证能显示、能保存原值
- list = [{ id, name: nameFromTree(id) || currentName || "" }].concat(list);
- }
- return list;
- }
|