controller.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'package:get/get.dart';
  2. import '../../../common/api/index.dart';
  3. import '../../../common/models/index.dart';
  4. import '../../../common/routers/index.dart';
  5. import '../../../common/utils/index.dart';
  6. import 'index.dart';
  7. import 'org_person_list/index.dart';
  8. import 'person/index.dart';
  9. class ContactController extends GetxController {
  10. ContactController();
  11. final state = ContactState();
  12. /// 在 widget 内存中分配后立即调用。
  13. @override
  14. void onInit() {
  15. super.onInit();
  16. }
  17. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  18. @override
  19. void onReady() {
  20. loadContact();
  21. super.onReady();
  22. }
  23. /// 在 [onDelete] 方法之前调用。
  24. @override
  25. void onClose() {
  26. super.onClose();
  27. }
  28. /// dispose 释放内存
  29. @override
  30. void dispose() {
  31. super.dispose();
  32. }
  33. Future<void> loadContact() async {
  34. if (O2ContactPermissionManager.instance.isCurrentPersonCannotQueryAll()) {
  35. OLogger.i('当前用户没有权限查询通讯录');
  36. return ;
  37. }
  38. var dn = O2ApiManager.instance.o2User?.distinguishedName;
  39. if (dn != null && dn.isNotEmpty) {
  40. var myIdentities =
  41. await OrganizationControlService.to.identityListByPerson(dn);
  42. if (myIdentities != null && myIdentities.isNotEmpty) {
  43. loadMyDepartments(myIdentities);
  44. if (!O2ContactPermissionManager.instance.isCurrentPersonCannotQueryOuter()) {
  45. loadTopUnit(myIdentities);
  46. } else {
  47. OLogger.i('当前用户没有权限查询外部门!');
  48. }
  49. }
  50. }
  51. }
  52. Future<void> loadMyDepartments(List<O2Identity> identities) async {
  53. var myDepartments = await OrganizationAssembleExpressService.to
  54. .unitListWithIdentities(
  55. identities.map((e) => e.distinguishedName!).toList());
  56. if (myDepartments != null && myDepartments.isNotEmpty) {
  57. // 查询排除
  58. List<O2Unit> newList = [];
  59. for (var element in myDepartments) {
  60. if (!O2ContactPermissionManager.instance.isExcludeUnit(element.distinguishedName??'')) {
  61. newList.add(element);
  62. }
  63. }
  64. state.myDepartments.addAll(newList);
  65. }
  66. }
  67. Future<void> loadTopUnit(List<O2Identity> identities) async {
  68. state.topUnit.clear();
  69. List<O2Unit> tops = [];
  70. // 全部顶级列示
  71. for (var i = 0; i < identities.length; i++) {
  72. final idDn = identities[i].distinguishedName;
  73. if (idDn?.isEmpty == true) {
  74. continue;
  75. }
  76. final unitTop = await OrganizationAssembleExpressService.to.unitByIdAndLevel(idDn!, 1);
  77. if (unitTop != null) {
  78. if (!O2ContactPermissionManager.instance.isExcludeUnit(unitTop.distinguishedName??'')) {
  79. if (!tops.any((uniqueObj) => uniqueObj.distinguishedName == unitTop.distinguishedName)) { // 去重复
  80. tops.add(await loadFirstLevelUnitAndPersonList(unitTop));
  81. }
  82. }
  83. }
  84. }
  85. state.topUnit.addAll(tops);
  86. }
  87. /// 获取第一层组织人员列表
  88. Future<O2Unit> loadFirstLevelUnitAndPersonList(O2Unit parent) async {
  89. final parentId = parent.distinguishedName;
  90. if (parentId == null) {
  91. OLogger.e('顶级组织的 ${parent.name} distinguishedName is null');
  92. return parent;
  93. }
  94. // 加载组织列表
  95. List<O2Unit>? orgs = await OrganizationControlService.to.unitListWithParent(parentId);
  96. if (orgs != null && orgs.isNotEmpty) { // 排除检查
  97. List<O2Unit> newUnitList = [];
  98. for (var element in orgs) {
  99. if (!O2ContactPermissionManager.instance.isExcludeUnit(element.distinguishedName??'')) {
  100. newUnitList.add(element);
  101. }
  102. }
  103. orgs = newUnitList;
  104. }
  105. parent.subUnitList = orgs;
  106. // 加载人员列表
  107. List<O2Identity>? identits = await OrganizationControlService.to.identityListWithUnit(parentId);
  108. List<O2Person>? persons;
  109. if (identits != null && identits.isNotEmpty) {
  110. persons = await OrganizationAssembleExpressService.to.listPersonWithIdentities(identits.map((e) => e.distinguishedName!).toList());
  111. if (persons != null && persons.isNotEmpty) {
  112. List<O2Person> newlist = [];
  113. for (var element in persons) { // 排除检查
  114. if (!O2ContactPermissionManager.instance.isExcludePerson(element.distinguishedName ?? '')) {
  115. newlist.add(element);
  116. }
  117. }
  118. persons = newlist;
  119. }
  120. }
  121. parent.personList = persons;
  122. return parent;
  123. }
  124. void openSearchContact() {
  125. if (O2ContactPermissionManager.instance.isCurrentPersonCannotQueryAll() || O2ContactPermissionManager.instance.isCurrentPersonCannotQueryOuter()) {
  126. OLogger.i('当前用户没有权限搜索通讯录');
  127. Loading.toast('contact_no_permission'.tr);
  128. return ;
  129. }
  130. Get.toNamed(O2OARoutes.homeContactSearch);
  131. }
  132. /// 打开部门
  133. void openDept(O2Unit o2unit) {
  134. OrgPersonListPage.open(top: o2unit);
  135. }
  136. ///
  137. /// 打开个人信息
  138. ///
  139. void clickEnterPerson(O2Person person) {
  140. PersonPage.openPersonInfo(person);
  141. }
  142. }