controller.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import 'package:get/get.dart';
  2. import '../../../common/api/index.dart';
  3. import '../../../common/models/index.dart';
  4. import '../../../common/utils/index.dart';
  5. import '../../../common/values/index.dart';
  6. import '../contact/contact_picker/index.dart';
  7. import 'im_chat/index.dart';
  8. import 'index.dart';
  9. import 'instant_chat/index.dart';
  10. import 'speech_assistant_chat/index.dart';
  11. class ImController extends GetxController {
  12. ImController();
  13. final state = ImState();
  14. Map<String, String> headers = {};
  15. EventBus eventBus = EventBus();
  16. final eventId = 'IM';
  17. List<InstantMsg> instantMsgList = [];
  18. InstantMsg? instantMsg;
  19. /// 在 widget 内存中分配后立即调用。
  20. @override
  21. void onInit() {
  22. super.onInit();
  23. }
  24. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  25. @override
  26. void onReady() {
  27. //监听websocket消息
  28. eventBus.on(EventBus.websocketCreateImMsg, eventId, (arg) {
  29. OLogger.d("收到websocket im 新消息!刷新会话列表");
  30. if (arg is IMMessage) {
  31. loadConversationList();
  32. }
  33. });
  34. eventBus.on(EventBus.websocketImConversationDeleteMsg, eventId, (arg) {
  35. OLogger.d("收到websocket 删除会话消息!刷新会话列表");
  36. loadConversationList();
  37. });
  38. eventBus.on(EventBus.websocketImConversationUpdateMsg, eventId, (arg) {
  39. OLogger.d("收到websocket 更新会话消息!刷新会话列表");
  40. loadConversationList();
  41. });
  42. headers[O2ApiManager.instance.tokenName] =
  43. O2ApiManager.instance.o2User?.token ?? '';
  44. final extend = ProgramCenterService.to.extendParam();
  45. final isShow = extend['showIMSpeechButton'] ?? false;
  46. state.showSpeechAssistant = isShow;
  47. loadConversationList();
  48. super.onReady();
  49. }
  50. /// 在 [onDelete] 方法之前调用。
  51. @override
  52. void onClose() {
  53. eventBus.off(EventBus.websocketCreateImMsg, eventId);
  54. eventBus.off(EventBus.websocketImConversationDeleteMsg, eventId);
  55. eventBus.off(EventBus.websocketImConversationUpdateMsg, eventId);
  56. super.onClose();
  57. }
  58. /// dispose 释放内存
  59. @override
  60. void dispose() {
  61. super.dispose();
  62. }
  63. // 语音助手
  64. void startSpeechAssistant() {
  65. SpeechAssistantChatPage.open();
  66. }
  67. Future<void> startSingleChat() async {
  68. var result = await ContactPickerPage.startPicker([ContactPickMode.personPicker]);
  69. if (result is ContactPickerResult) {
  70. if (result.users != null && result.users!.isNotEmpty) {
  71. if (result.users![0].distinguishedName! == O2ApiManager.instance.o2User?.distinguishedName) {
  72. Loading.toast('im_msg_create_conversation_not_with_self'.tr);
  73. return;
  74. }
  75. createConversation(O2.imConversationTypeSingle, [result.users![0].distinguishedName!]);
  76. }
  77. }
  78. }
  79. Future<void> startGroupChat() async {
  80. var result = await ContactPickerPage.startPicker([ContactPickMode.personPicker], multiple: true);
  81. if (result is ContactPickerResult) {
  82. List<String> personList = [];
  83. result.users?.forEach((element) {
  84. personList.add(element.distinguishedName!);
  85. });
  86. createConversation(O2.imConversationTypeGroup, personList);
  87. }
  88. }
  89. Future<void> createConversation(String type, List<String> personList) async {
  90. if (type.isEmpty || personList.isEmpty) {
  91. return;
  92. }
  93. final conv = await MessageCommunicationService.to.createConversation(type, personList);
  94. if (conv != null) {
  95. tapOpenConversation(conv);
  96. }
  97. }
  98. /// 获取会话列表
  99. Future<void> loadConversationList() async {
  100. if (ProgramCenterService.to.isShowSystemMessage()) {
  101. loadInstantMsgList();
  102. } else {
  103. state.showInstantMsg = false;
  104. }
  105. var list = await MessageCommunicationService.to.myConversationList();
  106. if (list != null && list.isNotEmpty) {
  107. state.conversationList.clear();
  108. state.conversationList.addAll(list);
  109. }
  110. int unReadNumber = 0;
  111. for (var element in state.conversationList) {
  112. unReadNumber += element.unreadNumber??0;
  113. }
  114. eventBus.emit(EventBus.imUnReadNumberMsg, unReadNumber);
  115. }
  116. Future<void> loadInstantMsgList() async {
  117. final list = await MessageCommunicationService.to.myInstantMsgList(15);
  118. if (list == null || list.isEmpty) {
  119. state.showInstantMsg = false;
  120. return;
  121. }
  122. final newList = list.where((element) => element.type?.startsWith('im_') == false).toList(); // im 消息不显示
  123. instantMsgList.clear();
  124. instantMsgList.addAll(newList);
  125. instantMsg = instantMsgList.first;
  126. state.showInstantMsg = true;
  127. }
  128. /// 单聊 人员头像
  129. String? personIconUrl(IMConversationInfo info) {
  130. // 单聊
  131. if (info.type == O2.imConversationTypeSingle) {
  132. var otherParty = info.personList?.firstWhereOrNull((element) => element != O2ApiManager.instance.o2User?.distinguishedName);
  133. if (otherParty != null && otherParty.isNotEmpty) {
  134. return OrganizationControlService.to.iconUrl(otherParty);
  135. }
  136. }
  137. return null;
  138. }
  139. /// 聊天会话
  140. Future<void> tapOpenConversation(IMConversationInfo info) async {
  141. OLogger.d("点击会话。。。。");
  142. await ImChatPage.open(info.id!);
  143. loadConversationList();
  144. }
  145. /// 系统消息
  146. Future<void> clickInstant() async {
  147. OLogger.d('点击了 系统消息');
  148. await InstantChatPage.open();
  149. loadConversationList();
  150. }
  151. }