GroupSettingViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // GroupSettingViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/24.
  6. //
  7. #import "GroupSettingViewController.h"
  8. #import "GroupSettingBodyView.h"
  9. #import "KSSearchViewController.h"
  10. #import "KSChatComplainController.h"
  11. #import "GroupListModel.h"
  12. #import "GroupMemberModel.h"
  13. #import "GroupMemberViewController.h"
  14. #import "GroupNoticeViewController.h"
  15. #import "GroupApplyViewController.h"
  16. #import "ModifyNameViewController.h"
  17. #import "KSPublicAlertView.h"
  18. #import <ImSDK_Plus/V2TIMManager+Group.h>
  19. #import "CustomNavViewController.h"
  20. #import "TenantChooseMemberViewController.h"
  21. #import "TenantStuModel.h"
  22. @interface GroupSettingViewController ()
  23. @property (nonatomic, strong) GroupListModel *sourceModel;
  24. @property (nonatomic, strong) GroupSettingBodyView *bodyView;
  25. @property (nonatomic, strong) NSMutableArray *sourceArray;
  26. @property (nonatomic, assign) NSInteger applyCount;
  27. @property (nonatomic, strong) KSPublicAlertView *alertView;
  28. @end
  29. @implementation GroupSettingViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. [self allocTitle:@"群设置"];
  34. [self configUI];
  35. }
  36. - (void)configUI {
  37. self.scrollView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  38. self.bodyView = [GroupSettingBodyView shareInstance];
  39. CGFloat height = kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin > 700 ? kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin : 700;
  40. self.bodyView.frame = CGRectMake(0, 0, kScreenWidth, height);
  41. [self.scrollView addSubview:self.bodyView];
  42. [self.scrollView setContentSize:CGSizeMake(kScreenWidth, height)];
  43. }
  44. - (void)rightBtnClick {
  45. // 分享
  46. }
  47. - (void)viewWillAppear:(BOOL)animated {
  48. [super viewWillAppear:animated];
  49. [self requestData];
  50. [self getGroupMessageNotiferStatus];
  51. [self requestApplyMember];
  52. }
  53. - (void)requestApplyMember {
  54. [KSNetworkingManager imGroupMemberAuditListRequest:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  55. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  56. NSArray *sourceArray = [dic ks_arrayValueForKey:@"data"];
  57. self.applyCount = sourceArray.count;
  58. self.bodyView.applyMember = self.applyCount;
  59. }
  60. else {
  61. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  62. }
  63. } faliure:^(NSError * _Nonnull error) {
  64. }];
  65. }
  66. - (void)evaluateMessge {
  67. NSMutableArray *memberArray = [NSMutableArray array];
  68. for (GroupMemberModel *model in self.sourceArray) {
  69. if (model.isAdmin) {
  70. [memberArray insertObject:model atIndex:0];
  71. }
  72. else {
  73. [memberArray addObject:model];
  74. }
  75. }
  76. self.sourceArray = [NSMutableArray arrayWithArray:memberArray];
  77. [self.bodyView evaluateStudentArray:self.sourceArray];
  78. }
  79. - (void)requestData {
  80. [KSNetworkingManager queryGroupDetail:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  81. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  82. NSDictionary *subDic = [dic ks_dictionaryValueForKey:@"data"];
  83. self.sourceModel = [[GroupListModel alloc] initWithDictionary:subDic];
  84. [self refreshUI];
  85. }
  86. else if ([dic ks_integerValueForKey:@"code"] == 204) {
  87. [LOADING_MANAGER KSShowMsg:@"获取群信息失败" promptCompletion:^{
  88. [self backAction];
  89. }];
  90. }
  91. else {
  92. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  93. }
  94. [self getGroupStudent];
  95. } faliure:^(NSError * _Nonnull error) {
  96. [self getGroupStudent];
  97. }];
  98. }
  99. - (void)getGroupStudent {
  100. [self.sourceArray removeAllObjects];
  101. [KSNetworkingManager imGroupMemberAllRequest:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  102. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  103. NSArray *sourceArray = [dic ks_arrayValueForKey:@"data"];
  104. for (NSDictionary *parm in sourceArray) {
  105. GroupMemberModel *model = [[GroupMemberModel alloc] initWithDictionary:parm];
  106. [self.sourceArray addObject:model];
  107. }
  108. [self evaluateMessge];
  109. }
  110. else {
  111. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  112. }
  113. } faliure:^(NSError * _Nonnull error) {
  114. }];
  115. }
  116. - (void)getGroupMessageNotiferStatus {
  117. [[V2TIMManager sharedInstance] getGroupsInfo:@[self.groupId] succ:^(NSArray<V2TIMGroupInfoResult *> * groupResultList) {
  118. for (V2TIMGroupInfoResult *result in groupResultList) {
  119. V2TIMGroupInfo *info = result.info;
  120. NSLog(@"recvOpt, %d", (int)info.recvOpt);
  121. if (info.recvOpt == V2TIM_RECEIVE_MESSAGE) {
  122. self.bodyView.isOn = NO;
  123. }
  124. else {
  125. self.bodyView.isOn = YES;
  126. }
  127. }
  128. } fail:^(int code, NSString *desc) {
  129. NSLog(@"failure, code:%d, desc:%@", code, desc);
  130. }];
  131. }
  132. - (void)refreshUI {
  133. MJWeakSelf;
  134. [self.bodyView configWithSource:self.sourceModel callback:^(GROUPSETTING type) {
  135. [weakSelf operationWithType:type];
  136. }];
  137. }
  138. - (void)operationWithType:(GROUPSETTING)type {
  139. switch (type) {
  140. case GROUPSETTING_PERSON: // 群成员列表
  141. {
  142. GroupMemberViewController *memberVC = [[GroupMemberViewController alloc] init];
  143. memberVC.groupId = self.groupId;
  144. [self.navigationController pushViewController:memberVC animated:YES];
  145. }
  146. break;
  147. case GROUPSETTING_APPLY: // 入群申请列表
  148. {
  149. GroupApplyViewController *applyCtrl = [[GroupApplyViewController alloc] init];
  150. applyCtrl.groupId = self.groupId;
  151. [self.navigationController pushViewController:applyCtrl animated:YES];
  152. }
  153. break;
  154. case GROUPSETTING_GROUP: // 修改群名称
  155. {
  156. ModifyNameViewController *modifyCtrl = [[ModifyNameViewController alloc] init];
  157. modifyCtrl.groupId = self.groupId;
  158. modifyCtrl.preNickName = self.sourceModel.name;
  159. [self.navigationController pushViewController:modifyCtrl animated:YES];
  160. }
  161. break;
  162. case GROUPSETTING_MESSAGESEARCH:
  163. {
  164. KSSearchViewController *vc = [[KSSearchViewController alloc] init];
  165. vc.groupId = self.groupId;
  166. CustomNavViewController *nav = [[CustomNavViewController alloc] initWithRootViewController:(UIViewController *)vc];
  167. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  168. UIViewController *parentVC = self.navigationController;
  169. if (parentVC) {
  170. [parentVC presentViewController:nav animated:NO completion:nil];
  171. }
  172. }
  173. break;
  174. case GROUPSETTING_SETTING: // 消息提醒
  175. {
  176. [self messageSetting];
  177. }
  178. break;
  179. case GROUPSETTING_NOTICE: // 群公告
  180. {
  181. GroupNoticeViewController *noticeVC = [[GroupNoticeViewController alloc] init];
  182. noticeVC.groupId = self.groupId;
  183. noticeVC.canEdit = YES;
  184. [self.navigationController pushViewController:noticeVC animated:YES];
  185. }
  186. break;
  187. case GROUPSETTING_FEEDBACK: // 反馈
  188. {
  189. KSChatComplainController *ctrl = [[KSChatComplainController alloc] init];
  190. ctrl.fromGroup = YES;
  191. ctrl.targetId = self.groupId;
  192. [self.navigationController pushViewController:ctrl animated:YES];
  193. }
  194. break;
  195. case GROUPSETTING_DISMISS: // 解散群聊
  196. {
  197. [self showDismissAlert];
  198. }
  199. break;
  200. case GROUPSETTING_ADDMENBER: // 添加群成员
  201. {
  202. NSMutableArray *memberArray = [NSMutableArray array];
  203. for (GroupMemberModel *model in self.sourceArray) {
  204. [memberArray addObject:model.imUserId];
  205. }
  206. TenantChooseMemberViewController *ctrl = [[TenantChooseMemberViewController alloc] init];
  207. ctrl.groupMemberIdArray = memberArray;
  208. MJWeakSelf;
  209. [ctrl chooseMemberCallback:^(NSMutableArray * _Nullable memberArray) {
  210. [weakSelf addMember:memberArray];
  211. }];
  212. [self.navigationController pushViewController:ctrl animated:YES];
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. - (void)addMember:(NSMutableArray *)array {
  220. if (array.count == 0) {
  221. return;
  222. }
  223. NSMutableArray *stuIdArray = [NSMutableArray array];
  224. for (TenantStuModel *model in array) {
  225. [stuIdArray addObject:model.imUserId];
  226. }
  227. [LOADING_MANAGER showHUD];
  228. [KSNetworkingManager tenantImGroupAddMemberRequest:KS_POST groupId:self.groupId studentIdArray:stuIdArray success:^(NSDictionary * _Nonnull dic) {
  229. [LOADING_MANAGER removeHUD];
  230. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  231. [self requestData];
  232. }
  233. else {
  234. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  235. }
  236. } faliure:^(NSError * _Nonnull error) {
  237. [LOADING_MANAGER removeHUD];
  238. }];
  239. }
  240. - (void)showDismissAlert {
  241. MJWeakSelf;
  242. self.alertView = [KSPublicAlertView shareInstanceWithTitle:@"提示" descMessage:@"确认解散群聊吗" leftTitle:@"取消" rightTitle:@"确定" cancelAction:^{
  243. } sureAction:^{
  244. [weakSelf dismissGroup];
  245. }];
  246. }
  247. - (void)dismissGroup {
  248. [KSNetworkingManager imGroupDismiss:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  249. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  250. [LOADING_MANAGER KSShowMsg:@"群已解散" promptCompletion:^{
  251. [self removeCurrentConversation];
  252. [self.navigationController popToRootViewControllerAnimated:YES];
  253. }];
  254. }
  255. else {
  256. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  257. }
  258. } faliure:^(NSError * _Nonnull error) {
  259. }];
  260. }
  261. - (void)removeCurrentConversation {
  262. MJWeakSelf;
  263. NSString *conversationID = [NSString stringWithFormat:@"group_%@", self.groupId];
  264. [[V2TIMManager sharedInstance] deleteConversation:conversationID
  265. succ:^{
  266. [weakSelf.navigationController popViewControllerAnimated:YES];
  267. }
  268. fail:nil];
  269. }
  270. - (void)messageSetting {
  271. V2TIMReceiveMessageOpt type = self.bodyView.isOn ? V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE : V2TIM_RECEIVE_MESSAGE;
  272. [[V2TIMManager sharedInstance] setGroupReceiveMessageOpt:self.groupId opt:type succ:^{
  273. NSLog(@"success");
  274. } fail:^(int code, NSString *desc) {
  275. dispatch_main_async_safe(^{
  276. [LOADING_MANAGER KSShowMsg:@"设置失败" promptCompletion:^{
  277. self.bodyView.isOn = !self.bodyView.isOn;
  278. }];
  279. });
  280. }];
  281. }
  282. - (NSMutableArray *)sourceArray {
  283. if (!_sourceArray) {
  284. _sourceArray = [NSMutableArray array];
  285. }
  286. return _sourceArray;
  287. }
  288. /*
  289. #pragma mark - Navigation
  290. // In a storyboard-based application, you will often want to do a little preparation before navigation
  291. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  292. // Get the new view controller using [segue destinationViewController].
  293. // Pass the selected object to the new view controller.
  294. }
  295. */
  296. @end