current-org.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * 当前登录用户所属机构(与 PC 端 src/utils/current-org.js 同一口径):
  3. * 「所属机构」下拉选项一律取登录缓存 loginChangeGroupVOList,
  4. * 不再调 /main/group/getGroupList 全量接口——普通账号不该看到其它公司。
  5. *
  6. * 移动端登录后把 /main/user/login 的返回整体存在 storage 的 userInfo 里,
  7. * 与 PC vuex 里 user.info 同源,故直接读 loginChangeGroupVOList。
  8. */
  9. import { pickLegalEntities } from "@/utils/hr/organization-tree";
  10. /** 是否有效机构 id(0 视为无效) */
  11. function isValidId(value) {
  12. return value !== undefined && value !== null && value !== "" && Number(value) !== 0;
  13. }
  14. /** 登录缓存中的可选机构列表 → [{ id, name }](即当前账号所属公司) */
  15. export function getLoginGroupOptions() {
  16. const info = uni.getStorageSync("userInfo") || {};
  17. const raw = info.raw || {};
  18. const list = info.loginChangeGroupVOList || raw.loginChangeGroupVOList || [];
  19. return (Array.isArray(list) ? list : [])
  20. .filter((item) => item && isValidId(item.groupId))
  21. .map((item) => ({
  22. id: item.groupId,
  23. name: item.groupName || item.name || item.organName || item.orgName || "",
  24. }));
  25. }
  26. /**
  27. * 当前登录用户所属机构 id:
  28. * 优先取登录缓存机构列表第一条(用户所属多个机构时默认第一个),
  29. * 缓存缺失时回落 userInfo.currentGroupId(登录/切换机构时写入)。
  30. */
  31. export function getCurrentOrgId() {
  32. const options = getLoginGroupOptions();
  33. if (options.length) return options[0].id;
  34. const info = uni.getStorageSync("userInfo") || {};
  35. const raw = info.raw || {};
  36. const current = info.currentGroupId != null ? info.currentGroupId : raw.currentGroupId;
  37. return isValidId(current) ? current : null;
  38. }
  39. /** 当前登录用户所属机构 { id, name } */
  40. export function getCurrentOrg() {
  41. const id = getCurrentOrgId();
  42. const matched = getLoginGroupOptions().find((item) => String(item.id) === String(id));
  43. return matched || { id, name: "" };
  44. }
  45. /**
  46. * 「所属机构」下拉选项(口径对齐 PC EmployeeArchiveForm.legalEntities):
  47. * ① 主源:登录缓存 loginChangeGroupVOList(当前账号所属公司),**不再用全量机构树**;
  48. * ② 缓存里只有 id 没有名称时(老缓存/接口缺字段),才回落到组织树里的集团/公司节点,
  49. * 保证下拉可读可选,而不是显示成一串 id;
  50. * ③ 当前值不在候选中时(编辑老档案,原机构已不在账号所属范围)补一条兜底项,
  51. * 避免下拉只显示 ID、且不会因此改掉已保存的值。
  52. * @param {Array} orgList 组织树全量列表(仅用于 ②③ 兜底,可传空)
  53. * @param {string|number} currentId 当前编辑中的机构 id
  54. * @param {string} currentName 当前编辑中的机构名称(组织树里也找不到时兜底显示)
  55. * @returns {Array<{id:string, name:string}>}
  56. */
  57. export function getOrgOptionsWithFallback(orgList = [], currentId = "", currentName = "") {
  58. const tree = Array.isArray(orgList) ? orgList : [];
  59. const companiesFromTree = () =>
  60. pickLegalEntities(tree).map((item) => ({ id: String(item.id), name: item.name || "" }));
  61. const nameFromTree = (id) => {
  62. const hit = companiesFromTree().find((item) => String(item.id) === String(id));
  63. return hit ? hit.name : "";
  64. };
  65. let list;
  66. const cached = getLoginGroupOptions();
  67. if (cached.length) {
  68. // 机构范围锁在登录缓存里;只是名称缺失(老缓存/接口少字段)时按 id 从组织树补名,
  69. // 不因为“没名称”就把候选扩大到全部公司(这点比 PC 更严一点)
  70. list = cached.map((item) => ({
  71. id: String(item.id),
  72. name: item.name || nameFromTree(item.id),
  73. }));
  74. } else {
  75. // 完全没有登录缓存(半登录态/老缓存)才回落组织树,否则下拉会是空的
  76. list = companiesFromTree();
  77. console.log(
  78. "[current-org] 登录缓存无机构列表,回落组织树:",
  79. list.map((item) => item.id).join(",") || "-"
  80. );
  81. }
  82. const id = currentId == null || currentId === "" ? "" : String(currentId);
  83. if (id && !list.some((item) => String(item.id) === id)) {
  84. // 编辑老档案:原机构已不在账号所属范围,补一条兜底项保证能显示、能保存原值
  85. list = [{ id, name: nameFromTree(id) || currentName || "" }].concat(list);
  86. }
  87. return list;
  88. }