GroupSettingViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #import "GroupBanListViewController.h"
  23. @interface GroupSettingViewController ()
  24. @property (nonatomic, strong) GroupListModel *sourceModel;
  25. @property (nonatomic, strong) GroupSettingBodyView *bodyView;
  26. @property (nonatomic, strong) NSMutableArray *sourceArray;
  27. @property (nonatomic, assign) NSInteger applyCount;
  28. @property (nonatomic, strong) KSPublicAlertView *alertView;
  29. @property (nonatomic, strong) NSString *groupRoleType;
  30. @property (nonatomic, assign) BOOL isMute;
  31. @end
  32. @implementation GroupSettingViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. [self allocTitle:@"群设置"];
  37. [self configUI];
  38. }
  39. - (void)configUI {
  40. self.scrollView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  41. self.bodyView = [GroupSettingBodyView shareInstance];
  42. CGFloat viewHeight = [self.bodyView getViewHeight:self.groupRoleType];
  43. CGFloat height = KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin > viewHeight ? KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin : viewHeight;
  44. self.bodyView.groupRoleType = @"";
  45. [self.scrollView addSubview:self.bodyView];
  46. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.right.mas_equalTo(self.view);
  48. make.top.mas_equalTo(self.scrollView.mas_top);
  49. make.bottom.mas_equalTo(self.scrollView.mas_bottom);
  50. make.height.mas_equalTo(height);
  51. }];
  52. }
  53. - (void)rightBtnClick {
  54. // 分享
  55. }
  56. - (void)viewWillAppear:(BOOL)animated {
  57. [super viewWillAppear:animated];
  58. [self requestData];
  59. [self getGroupMessageNotiferStatus];
  60. [self requestApplyMember];
  61. }
  62. - (void)requestApplyMember {
  63. [KSNetworkingManager imGroupMemberAuditListRequest:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  64. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  65. NSArray *sourceArray = [dic ks_arrayValueForKey:@"data"];
  66. self.applyCount = sourceArray.count;
  67. self.bodyView.applyMember = self.applyCount;
  68. }
  69. else {
  70. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  71. }
  72. } faliure:^(NSError * _Nonnull error) {
  73. }];
  74. }
  75. - (void)evaluateMessge {
  76. self.bodyView.groupRoleType = self.groupRoleType;
  77. CGFloat viewHeight = [self.bodyView getViewHeight:self.groupRoleType];
  78. CGFloat height = kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin > viewHeight ? kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin : viewHeight;
  79. [self.bodyView mas_updateConstraints:^(MASConstraintMaker *make) {
  80. make.height.mas_equalTo(height);
  81. }];
  82. NSMutableArray *memberArray = [NSMutableArray array];
  83. for (GroupMemberModel *model in self.sourceArray) {
  84. if (model.isAdmin) {
  85. [memberArray insertObject:model atIndex:0];
  86. }
  87. else {
  88. [memberArray addObject:model];
  89. }
  90. }
  91. self.sourceArray = [NSMutableArray arrayWithArray:memberArray];
  92. [self.bodyView evaluateStudentArray:self.sourceArray];
  93. }
  94. - (void)requestData {
  95. [KSNetworkingManager queryGroupDetail:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  96. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  97. NSDictionary *subDic = [dic ks_dictionaryValueForKey:@"data"];
  98. self.sourceModel = [[GroupListModel alloc] initWithDictionary:subDic];
  99. NSString *jsonString = [subDic ks_stringValueForKey:@"configJson"];
  100. if (![NSString isEmptyString:jsonString]) {
  101. NSData *jsonData = [jsonString mj_JSONData];
  102. NSDictionary *configJson = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
  103. self.isMute = [configJson ks_boolValueForKey:@"mute"];
  104. }
  105. else {
  106. self.isMute = NO;
  107. }
  108. [self refreshUI];
  109. }
  110. else if ([dic ks_integerValueForKey:@"code"] == 204) {
  111. [LOADING_MANAGER KSShowMsg:@"获取群信息失败" promptCompletion:^{
  112. [self backAction];
  113. }];
  114. }
  115. else {
  116. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  117. }
  118. [self getGroupStudent];
  119. } faliure:^(NSError * _Nonnull error) {
  120. [self getGroupStudent];
  121. }];
  122. }
  123. - (void)getGroupStudent {
  124. [self.sourceArray removeAllObjects];
  125. [KSNetworkingManager imGroupMemberAllRequest:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  126. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  127. NSArray *sourceArray = [dic ks_arrayValueForKey:@"data"];
  128. for (NSDictionary *parm in sourceArray) {
  129. GroupMemberModel *model = [[GroupMemberModel alloc] initWithDictionary:parm];
  130. if ([model.imUserId isEqualToString:UserDefault(IM_USERID)]) {
  131. self.groupRoleType = model.groupRoleType;
  132. }
  133. [self.sourceArray addObject:model];
  134. }
  135. [self evaluateMessge];
  136. }
  137. else {
  138. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  139. }
  140. } faliure:^(NSError * _Nonnull error) {
  141. }];
  142. }
  143. - (void)getGroupMessageNotiferStatus {
  144. [[V2TIMManager sharedInstance] getGroupsInfo:@[self.groupId] succ:^(NSArray<V2TIMGroupInfoResult *> * groupResultList) {
  145. for (V2TIMGroupInfoResult *result in groupResultList) {
  146. V2TIMGroupInfo *info = result.info;
  147. NSLog(@"recvOpt, %d", (int)info.recvOpt);
  148. if (info.recvOpt == V2TIM_RECEIVE_MESSAGE) {
  149. self.bodyView.isOn = NO;
  150. }
  151. else {
  152. self.bodyView.isOn = YES;
  153. }
  154. }
  155. } fail:^(int code, NSString *desc) {
  156. NSLog(@"failure, code:%d, desc:%@", code, desc);
  157. }];
  158. }
  159. - (void)refreshUI {
  160. self.bodyView.isMute = self.isMute;
  161. MJWeakSelf;
  162. [self.bodyView configWithSource:self.sourceModel callback:^(GROUPSETTING type) {
  163. [weakSelf operationWithType:type];
  164. }];
  165. }
  166. - (void)operationWithType:(GROUPSETTING)type {
  167. switch (type) {
  168. case GROUPSETTING_PERSON: // 群成员列表
  169. {
  170. GroupMemberViewController *memberVC = [[GroupMemberViewController alloc] init];
  171. memberVC.groupId = self.groupId;
  172. [self.navigationController pushViewController:memberVC animated:YES];
  173. }
  174. break;
  175. case GROUPSETTING_APPLY: // 入群申请列表
  176. {
  177. GroupApplyViewController *applyCtrl = [[GroupApplyViewController alloc] init];
  178. applyCtrl.groupId = self.groupId;
  179. [self.navigationController pushViewController:applyCtrl animated:YES];
  180. }
  181. break;
  182. case GROUPSETTING_GROUP: // 修改群名称
  183. {
  184. ModifyNameViewController *modifyCtrl = [[ModifyNameViewController alloc] init];
  185. modifyCtrl.groupId = self.groupId;
  186. modifyCtrl.preNickName = self.sourceModel.name;
  187. [self.navigationController pushViewController:modifyCtrl animated:YES];
  188. }
  189. break;
  190. case GROUPSETTING_MESSAGESEARCH:
  191. {
  192. KSSearchViewController *vc = [[KSSearchViewController alloc] init];
  193. vc.groupId = self.groupId;
  194. CustomNavViewController *nav = [[CustomNavViewController alloc] initWithRootViewController:(UIViewController *)vc];
  195. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  196. UIViewController *parentVC = self.navigationController;
  197. if (parentVC) {
  198. [parentVC presentViewController:nav animated:NO completion:nil];
  199. }
  200. }
  201. break;
  202. case GROUPSETTING_SETTING: // 消息提醒
  203. {
  204. [self messageSetting];
  205. }
  206. break;
  207. case GROUPSETTING_NOTICE: // 群公告
  208. {
  209. GroupNoticeViewController *noticeVC = [[GroupNoticeViewController alloc] init];
  210. noticeVC.groupId = self.groupId;
  211. noticeVC.canEdit = YES;
  212. [self.navigationController pushViewController:noticeVC animated:YES];
  213. }
  214. break;
  215. case GROUPSETTING_FEEDBACK: // 反馈
  216. {
  217. KSChatComplainController *ctrl = [[KSChatComplainController alloc] init];
  218. ctrl.fromGroup = YES;
  219. ctrl.targetId = self.groupId;
  220. [self.navigationController pushViewController:ctrl animated:YES];
  221. }
  222. break;
  223. case GROUPSETTING_DISMISS: // 解散群聊
  224. {
  225. [self showDismissAlert];
  226. }
  227. break;
  228. case GROUPSETTING_ADDMENBER: // 添加群成员
  229. {
  230. NSMutableArray *memberArray = [NSMutableArray array];
  231. for (GroupMemberModel *model in self.sourceArray) {
  232. [memberArray addObject:model.imUserId];
  233. }
  234. TenantChooseMemberViewController *ctrl = [[TenantChooseMemberViewController alloc] init];
  235. ctrl.groupMemberIdArray = memberArray;
  236. MJWeakSelf;
  237. [ctrl chooseMemberCallback:^(NSMutableArray * _Nullable memberArray) {
  238. [weakSelf addMember:memberArray];
  239. }];
  240. [self.navigationController pushViewController:ctrl animated:YES];
  241. }
  242. break;
  243. case GROUPSETTING_MUTE: // 禁言操作
  244. {
  245. if (self.sourceModel) {
  246. [self muteAction];
  247. }
  248. else {
  249. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您已不在当前群聊"];
  250. }
  251. }
  252. break;
  253. case GROUPSETTING_BANLIST: // 禁言列表
  254. {
  255. GroupBanListViewController *ctrl = [[GroupBanListViewController alloc] init];
  256. ctrl.groupId = self.groupId;
  257. [self.navigationController pushViewController:ctrl animated:YES];
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. - (void)muteAction {
  265. BOOL isMute = !self.isMute;
  266. [KSNetworkingManager imGroupMuteAllRequest:KS_POST groupId:self.groupId muteAll:isMute success:^(NSDictionary * _Nonnull dic) {
  267. if ([dic ks_integerValueForKey:@"code"] == 200) {
  268. [LOADING_MANAGER KSShowMsg:@"设置成功" promptCompletion:^{
  269. [self requestData];
  270. }];
  271. }
  272. else {
  273. self.bodyView.isMute = self.isMute;
  274. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  275. }
  276. } faliure:^(NSError * _Nonnull error) {
  277. self.bodyView.isMute = self.isMute;
  278. }];
  279. }
  280. - (void)addMember:(NSMutableArray *)array {
  281. if (array.count == 0) {
  282. return;
  283. }
  284. NSMutableArray *stuIdArray = [NSMutableArray array];
  285. for (TenantStuModel *model in array) {
  286. [stuIdArray addObject:model.imUserId];
  287. }
  288. [LOADING_MANAGER showHUD];
  289. [KSNetworkingManager tenantImGroupAddMemberRequest:KS_POST groupId:self.groupId studentIdArray:stuIdArray success:^(NSDictionary * _Nonnull dic) {
  290. [LOADING_MANAGER removeHUD];
  291. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  292. [self requestData];
  293. }
  294. else {
  295. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  296. }
  297. } faliure:^(NSError * _Nonnull error) {
  298. [LOADING_MANAGER removeHUD];
  299. }];
  300. }
  301. - (void)showDismissAlert {
  302. MJWeakSelf;
  303. self.alertView = [KSPublicAlertView shareInstanceWithTitle:@"提示" descMessage:@"确认解散群聊吗" leftTitle:@"取消" rightTitle:@"确定" cancelAction:^{
  304. } sureAction:^{
  305. [weakSelf dismissGroup];
  306. }];
  307. }
  308. - (void)dismissGroup {
  309. [KSNetworkingManager imGroupDismiss:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  310. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  311. [LOADING_MANAGER KSShowMsg:@"群已解散" promptCompletion:^{
  312. [self removeCurrentConversation];
  313. [self.navigationController popToRootViewControllerAnimated:YES];
  314. }];
  315. }
  316. else {
  317. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  318. }
  319. } faliure:^(NSError * _Nonnull error) {
  320. }];
  321. }
  322. - (void)removeCurrentConversation {
  323. MJWeakSelf;
  324. NSString *conversationID = [NSString stringWithFormat:@"group_%@", self.groupId];
  325. [[V2TIMManager sharedInstance] deleteConversation:conversationID
  326. succ:^{
  327. [weakSelf.navigationController popViewControllerAnimated:YES];
  328. }
  329. fail:nil];
  330. }
  331. - (void)messageSetting {
  332. V2TIMReceiveMessageOpt type = self.bodyView.isOn ? V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE : V2TIM_RECEIVE_MESSAGE;
  333. [[V2TIMManager sharedInstance] setGroupReceiveMessageOpt:self.groupId opt:type succ:^{
  334. NSLog(@"success");
  335. } fail:^(int code, NSString *desc) {
  336. dispatch_main_async_safe(^{
  337. [LOADING_MANAGER KSShowMsg:@"设置失败" promptCompletion:^{
  338. self.bodyView.isOn = !self.bodyView.isOn;
  339. }];
  340. });
  341. }];
  342. }
  343. - (NSMutableArray *)sourceArray {
  344. if (!_sourceArray) {
  345. _sourceArray = [NSMutableArray array];
  346. }
  347. return _sourceArray;
  348. }
  349. /*
  350. #pragma mark - Navigation
  351. // In a storyboard-based application, you will often want to do a little preparation before navigation
  352. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  353. // Get the new view controller using [segue destinationViewController].
  354. // Pass the selected object to the new view controller.
  355. }
  356. */
  357. @end