controller.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:o2oa_all_platform/common/index.dart';
  5. import '../../../home/contact/contact_picker/index.dart';
  6. import '../meeting_room_picker/index.dart';
  7. import 'index.dart';
  8. class MeetingFormController extends GetxController {
  9. MeetingFormController();
  10. final state = MeetingFormState();
  11. var isEdit = false; // 修改还是新建
  12. // 会议标题
  13. final TextEditingController subjectController = TextEditingController();
  14. // 在线会议打开的连接
  15. final TextEditingController onlineLinkController = TextEditingController();
  16. // 在线会议房间号
  17. final TextEditingController onlineRoomController = TextEditingController();
  18. // 会议描述
  19. final TextEditingController summaryController = TextEditingController();
  20. /// 在 widget 内存中分配后立即调用。
  21. @override
  22. void onInit() {
  23. super.onInit();
  24. }
  25. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  26. @override
  27. void onReady() {
  28. final meetingInfo = Get.arguments;
  29. if (meetingInfo != null && meetingInfo is MeetingInfoModel) {
  30. isEdit = true;
  31. state.meetingInfo.value = meetingInfo;
  32. }
  33. initData();
  34. super.onReady();
  35. }
  36. /// 在 [onDelete] 方法之前调用。
  37. @override
  38. void onClose() {
  39. super.onClose();
  40. }
  41. /// dispose 释放内存
  42. @override
  43. void dispose() {
  44. super.dispose();
  45. }
  46. void initData() {
  47. String? config = SharedPreferenceService.to
  48. .getString(SharedPreferenceService.meetingConfigKey);
  49. if (config.isNotEmpty) {
  50. MeetingConfigModel cModel =
  51. MeetingConfigModel.fromJson(O2Utils.parseStringToJson(config));
  52. state.typeList.clear();
  53. state.typeList.addAll(cModel.typeList ?? []);
  54. state.showMode = cModel.enableOnline == true;
  55. state.showOnline = cModel.enableOnline == true && cModel.onlineProduct != '好视通'; // 这里目前是写死的名字,只有这个接入了,好视通不需要输入链接和会议号,其他需要写入。
  56. }
  57. subjectController.text = state.meetingInfo.value?.subject ?? '';
  58. onlineLinkController.text = state.meetingInfo.value?.roomLink ?? '';
  59. onlineRoomController.text = state.meetingInfo.value?.roomId ?? '';
  60. summaryController.text = state.meetingInfo.value?.summary ?? '';
  61. if (!isEdit) {
  62. // 新建
  63. final now = DateTime.now();
  64. var startDate = now.addHours(1);
  65. final completeDate = now.addHours(2);
  66. state.meetingInfo.value = MeetingInfoModel(
  67. startTime: startDate.zeroedMinutes().ymdhms(),
  68. completedTime: completeDate.zeroedMinutes().ymdhms());
  69. }
  70. }
  71. ///
  72. /// 保存会议
  73. ///
  74. Future<void> saveMeeting({bool closePage = false}) async {
  75. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  76. if (meetingInfo != null) {
  77. final subject = subjectController.text;
  78. if (subject.isEmpty) {
  79. Loading.toast('meeting_form_subject_not_empty'.tr);
  80. return;
  81. }
  82. meetingInfo.subject = subject;
  83. meetingInfo.summary = summaryController.text;
  84. final startTime = meetingInfo.startTime;
  85. final completedTime = meetingInfo.completedTime;
  86. if (startTime == null ||
  87. startTime.isEmpty ||
  88. completedTime == null ||
  89. completedTime.isEmpty) {
  90. Loading.toast('meeting_form_time_not_empty'.tr);
  91. return;
  92. }
  93. final room = meetingInfo.room;
  94. if (room == null || room.isEmpty) {
  95. Loading.toast('meeting_form_room_not_empty'.tr);
  96. return;
  97. }
  98. final inviteMemberList = meetingInfo.inviteMemberList;
  99. if (inviteMemberList == null || inviteMemberList.isEmpty) {
  100. Loading.toast('meeting_form_member_not_empty'.tr);
  101. return;
  102. }
  103. if (state.showOnline && meetingInfo.mode == MeetingMode.online.getKey()) {
  104. final link = onlineLinkController.text;
  105. if (link.isEmpty) {
  106. Loading.toast('meeting_form_online_link_hint'.tr);
  107. return;
  108. }
  109. final roomId = onlineRoomController.text;
  110. if (roomId.isEmpty) {
  111. Loading.toast('meeting_form_online_room_hint'.tr);
  112. return;
  113. }
  114. meetingInfo.roomLink = link;
  115. meetingInfo.roomId = roomId;
  116. }
  117. if (meetingInfo.id == null) {
  118. // 新增
  119. // 当前申请人
  120. meetingInfo.applicant = O2ApiManager.instance.o2User?.distinguishedName;
  121. final id = await MeetingAssembleService.to.saveMeeting(meetingInfo);
  122. if (id != null) {
  123. meetingInfo.id = id.id;
  124. state.meetingInfo.value = meetingInfo;
  125. if (closePage) {
  126. Get.back();
  127. }
  128. }
  129. } else {
  130. // 更新
  131. final id = await MeetingAssembleService.to
  132. .updateMeeting(meetingInfo.id!, meetingInfo);
  133. if (id != null) {
  134. meetingInfo.id = id.id;
  135. state.meetingInfo.value = meetingInfo;
  136. if (closePage) {
  137. Get.back();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. // 添加主持人
  144. void clickPickHostPerson() async {
  145. var result = await ContactPickerPage.startPicker([ContactPickMode.personPicker]);
  146. if (result is ContactPickerResult) {
  147. if (result.users != null && result.users!.isNotEmpty) {
  148. String hostPerson = result.users![0].distinguishedName!;
  149. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  150. if (meetingInfo != null) {
  151. meetingInfo.hostPerson = hostPerson;
  152. state.meetingInfo.value = meetingInfo;
  153. OLogger.d('meetingInfo: ${meetingInfo.toJson()}');
  154. }
  155. }
  156. }
  157. }
  158. // 添加会议室
  159. void clickPickMeetingRoom() async {
  160. final room = await MeetingRoomPickerPage.pickMeetingRoom(state.meetingInfo.value?.startTime, state.meetingInfo.value?.completedTime);
  161. if (room != null && room is MeetingRoom) {
  162. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  163. meetingInfo?.woRoom = room;
  164. meetingInfo?.room = room.id;
  165. state.meetingInfo.value = meetingInfo;
  166. }
  167. }
  168. // 添加承办部门
  169. void clickPickHostUnit() async {
  170. var result = await ContactPickerPage.startPicker([ContactPickMode.departmentPicker]);
  171. if (result is ContactPickerResult) {
  172. if (result.departments != null && result.departments!.isNotEmpty) {
  173. String hostUnit = result.departments![0].distinguishedName!;
  174. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  175. if (meetingInfo != null) {
  176. meetingInfo.hostUnit = hostUnit;
  177. state.meetingInfo.value = meetingInfo;
  178. OLogger.d('meetingInfo: ${meetingInfo.toJson()}');
  179. }
  180. }
  181. }
  182. }
  183. // 添加参会人员、组织
  184. void clickAddMembers() async {
  185. final inviteMemberList = state.meetingInfo.value?.inviteMemberList ?? [];
  186. final orgList = inviteMemberList.where((element) => element.endsWith('@U')).toList();
  187. final personList = inviteMemberList.where((element) => element.endsWith('@P')).toList();
  188. var result = await ContactPickerPage.startPicker([ContactPickMode.personPicker, ContactPickMode.departmentPicker], multiple: true, initUserList: personList, initDeptList: orgList);
  189. if (result is ContactPickerResult) {
  190. List<String> persons = [];
  191. List<String> deptList = [];
  192. if (result.users != null && result.users!.isNotEmpty) {
  193. persons = result.users!.map((e) => e.distinguishedName!).toList();
  194. }
  195. if (result.departments != null && result.departments!.isNotEmpty) {
  196. deptList = result.departments!.map((e) => e.distinguishedName!).toList();
  197. }
  198. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  199. if (meetingInfo != null) {
  200. List<String> inviteMembers = [];
  201. inviteMembers.addAll(persons);
  202. inviteMembers.addAll(deptList);
  203. meetingInfo.inviteMemberList = inviteMembers;
  204. state.meetingInfo.value = meetingInfo;
  205. OLogger.d('meetingInfo: ${meetingInfo.toJson()}');
  206. }
  207. }
  208. }
  209. // 删除参会人员、组织
  210. void removeMember(String person) {
  211. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  212. if (meetingInfo != null) {
  213. var inviteMemberList = meetingInfo.inviteMemberList ?? [];
  214. inviteMemberList.remove(person);
  215. meetingInfo.inviteMemberList = inviteMemberList;
  216. state.meetingInfo.value = meetingInfo;
  217. OLogger.d('meetingInfo: ${meetingInfo.toJson()}');
  218. }
  219. }
  220. ///
  221. /// 上传附件
  222. ///
  223. Future<void> uploadMeetingAttachment() async {
  224. final id = state.meetingInfo.value?.id;
  225. if (id == null || id.isEmpty) {
  226. // 先保存会议数据 获取会议id
  227. await saveMeeting();
  228. }
  229. final meetingId = state.meetingInfo.value?.id;
  230. if (meetingId == null || meetingId.isEmpty) {
  231. OLogger.e('无法上传会议材料,会议ID为空!!!');
  232. return;
  233. }
  234. O2Utils.pickerFileOrImage((paths) {
  235. _uploadMeetingAttaBegin(paths, meetingId);
  236. });
  237. }
  238. Future<void> _uploadMeetingAttaBegin(List<String?> paths, String meetingId) async {
  239. if (paths.isEmpty) {
  240. return;
  241. }
  242. String path = paths[0] ?? '';
  243. if (path.isEmpty) {
  244. return;
  245. }
  246. OLogger.d('开始上传文件: $path');
  247. Loading.show();
  248. final aId = await MeetingAssembleService.to
  249. .uploadMeetingAttachment(meetingId, File(path));
  250. if (aId != null) {
  251. final meetingAttachment =
  252. await MeetingAssembleService.to.getMeetingAttachment(aId.id!);
  253. if (meetingAttachment != null) {
  254. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  255. if (meetingInfo != null) {
  256. final attaList = meetingInfo.attachmentList ?? [];
  257. attaList.add(meetingAttachment);
  258. meetingInfo.attachmentList = attaList;
  259. state.meetingInfo.value = meetingInfo;
  260. OLogger.d('meetingInfo: ${meetingInfo.toJson()}');
  261. }
  262. Loading.dismiss();
  263. }
  264. }
  265. }
  266. ///
  267. /// 删除附件
  268. ///
  269. void removeAttachment(MeetingAttachmentModel atta) async {
  270. final aId = await MeetingAssembleService.to.deleteMeetingAttachment(atta.id!);
  271. if (aId != null) {
  272. final meetingInfo = state.meetingInfo.value?.newInstanceCopyValue();
  273. if (meetingInfo != null) {
  274. final attaList = meetingInfo.attachmentList ?? [];
  275. attaList.remove(atta);
  276. meetingInfo.attachmentList = attaList;
  277. state.meetingInfo.value = meetingInfo;
  278. }
  279. }
  280. }
  281. }