ForwardViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // ForwardViewController.m
  3. // AIIM
  4. //
  5. // Created by qitewei on 2025/5/10.
  6. //
  7. #import "ForwardViewController.h"
  8. #import "FriendListCell.h"
  9. #import "FriendNetApi.h"
  10. #import "GroupNetApi.h"
  11. #import "ChatListStore.h"
  12. #import "GWebSocket.h"
  13. #import "UDManager.h"
  14. @interface ForwardViewController ()<UITableViewDelegate,UITableViewDataSource>
  15. @property (nonatomic, strong) UIImageView * bgImageView;
  16. @property (nonatomic, strong) UIButton * leftBtn;
  17. @property (nonatomic, strong) UITableView * tableView;
  18. @property (nonatomic, strong) NSMutableArray * dataArray;
  19. @property (nonatomic, strong) NSMutableArray * groupArray;
  20. @end
  21. @implementation ForwardViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self configUI];
  25. [self getGroupList];
  26. [self getFriendlist];
  27. }
  28. - (void)configUI{
  29. [self setNavigationTitle:@"转发"];
  30. [self setNavigationBarTransparent:YES];
  31. [self setNavigationBarBackgroundColor:UIColor.clearColor];
  32. [self setNavigationTitleColor:UIColor.whiteColor font:SYSBFONT(18)];
  33. [self addBarButtonWithImage:kImageMake(@"fanhui") position:BarButtonItemPositionLeft action:@selector(backToChat)];
  34. [self.view addSubview:self.bgImageView];
  35. [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.left.right.top.bottom.mas_equalTo(0);
  37. }];
  38. [self.view addSubview:self.tableView];
  39. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.right.bottom.mas_equalTo(0);
  41. make.top.mas_equalTo(SCREEN_TOP);
  42. }];
  43. }
  44. #pragma mark api
  45. - (void)getGroupList{
  46. [GroupNetApi getGroupList:^(int code, NSDictionary * _Nullable result) {
  47. NSLog(@"result:%@",result);
  48. [self.groupArray addObjectsFromArray:result[@"data"]];
  49. [self.tableView reloadData];
  50. } fail:^(NSError * _Nonnull error) {
  51. }];
  52. }
  53. -(void)getFriendlist{
  54. [FriendNetApi getfriends:nil succ:^(int code, NSDictionary * res) {
  55. [self.dataArray addObjectsFromArray:res[@"data"]];
  56. //NSLog(@"self.friendlist:%@",self.friendlist);
  57. [self.tableView reloadData];
  58. } fail:^(NSError * _Nonnull error) {
  59. }];
  60. }
  61. #pragma mark event
  62. - (void)backToChat{
  63. NSLog(@"backToChat");
  64. [self dismissViewControllerAnimated:YES completion:nil];
  65. }
  66. #pragma mark private
  67. - (void)sendForwardMessageWithFriend:(NSDictionary *)friend Type:(NSInteger)type{
  68. NSLog(@"44444");
  69. NSDate *now = [NSDate date];
  70. NSTimeInterval trt = [now timeIntervalSince1970];
  71. NSInteger time = trt*1000;
  72. NSDictionary * userInfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  73. NSString * typeString = type == 0 ? @"1" : @"0";
  74. if (!self.isMultiple) {//逐条转发
  75. for (NSDictionary * msg in self.msgArray) {
  76. time= time+100;
  77. NSString *strtime = [NSString stringWithFormat:@"%ld",(long)time];
  78. NSDictionary *Smsgd=@{
  79. @"messageType":msg[@"messageType"],
  80. @"chatId":friend[@"id"],
  81. @"fromId":userInfo[@"id"],
  82. @"fromName":self.userName,
  83. @"fromAvatar":self.userAvatar,
  84. @"content":msg[@"content"],
  85. @"type":typeString,
  86. @"reSend":[NSNumber numberWithBool:false],
  87. @"extend": [msg jk_hasKey:@"extend"] == YES ? msg[@"extend"] : @{
  88. @"atAll":@"false",
  89. @"atUserIds":@[]
  90. },
  91. @"localtime":strtime,
  92. @"timestamp":strtime,
  93. };
  94. NSDictionary *sendInfo = @{
  95. @"code":@"2",
  96. @"message":Smsgd,
  97. };
  98. NSError *error;
  99. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sendInfo options:0 error:&error];
  100. if (!jsonData) {
  101. NSLog(@"Got an error: %@", error);
  102. } else {
  103. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  104. [GWebSocket.shareInstance sendMsg:jsonString];
  105. }
  106. }
  107. [MBProgressHUD showWithText:@"操作成功"];
  108. [self backToChat];
  109. }else{//合并转发
  110. NSString *strtime = [NSString stringWithFormat:@"%ld",(long)time];
  111. NSDictionary *Smsgd=@{
  112. @"messageType":@"6",
  113. @"chatId":friend[@"id"],
  114. @"fromId":userInfo[@"id"],
  115. @"fromName":self.userName,
  116. @"fromAvatar":self.userAvatar,
  117. @"content":@"聊天记录",
  118. @"type":typeString,
  119. @"reSend":[NSNumber numberWithBool:false],
  120. @"extend": @{@"messageList":self.msgArray},
  121. @"localtime":strtime,
  122. @"timestamp":strtime,
  123. };
  124. NSDictionary *sendInfo = @{
  125. @"code":@"2",
  126. @"message":Smsgd,
  127. };
  128. NSError *error;
  129. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sendInfo options:0 error:&error];
  130. if (!jsonData) {
  131. NSLog(@"Got an error: %@", error);
  132. } else {
  133. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  134. [GWebSocket.shareInstance sendMsg:jsonString];
  135. [MBProgressHUD showWithText:@"操作成功"];
  136. [self backToChat];
  137. }
  138. }
  139. }
  140. #pragma mark tableview delegate
  141. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  142. return 2;
  143. }
  144. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  145. if (section == 0) {
  146. return self.groupArray.count;
  147. }
  148. return self.dataArray.count;
  149. }
  150. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  151. return 20.f;
  152. }
  153. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  154. return 0;
  155. }
  156. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  157. UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
  158. UILabel * titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 200, 16)];
  159. titleLbl.text = section == 0 ? @"群聊" : @"好友";
  160. titleLbl.textColor = UIColor.whiteColor;
  161. titleLbl.font = SYSFONT(16);
  162. [header addSubview:titleLbl];
  163. return header;
  164. }
  165. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  166. return nil;
  167. }
  168. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  169. return 70;
  170. }
  171. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  172. if (indexPath.section == 0) {
  173. FriendListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(FriendListCell.class) forIndexPath:indexPath];
  174. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  175. cell.backgroundColor = [UIColor clearColor];
  176. [cell fillWithData:self.groupArray[indexPath.row]];
  177. return cell;
  178. }
  179. FriendListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(FriendListCell.class) forIndexPath:indexPath];
  180. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  181. cell.backgroundColor = [UIColor clearColor];
  182. [cell fillWithData:self.dataArray[indexPath.row]];
  183. return cell;
  184. }
  185. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  186. NSLog(@"1111");
  187. if (indexPath.section == 0) {
  188. NSDictionary * friendDict = self.groupArray[indexPath.row];
  189. NSLog(@"222");
  190. if ([friendDict[@"id"] isEqualToString:self.fromChatId]) {
  191. [self backToChat];
  192. return;
  193. }
  194. [self sendForwardMessageWithFriend:friendDict Type:0];
  195. }else{
  196. NSDictionary * friendDict = self.dataArray[indexPath.row];
  197. NSLog(@"33333:%@",self.fromChatId);
  198. NSLog(@"friendDict33333:%@",friendDict);
  199. if ([friendDict[@"id"] isEqualToString:self.fromChatId]) {
  200. [self backToChat];
  201. return;
  202. }
  203. [self sendForwardMessageWithFriend:friendDict Type:1];
  204. }
  205. }
  206. #pragma mark lazy
  207. - (UIImageView *)bgImageView{
  208. if (!_bgImageView) {
  209. _bgImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
  210. }
  211. return _bgImageView;
  212. }
  213. - (UIButton *)leftBtn{
  214. if (!_leftBtn) {
  215. _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  216. [_leftBtn setImage:kImageMake(@"fanhui") forState:UIControlStateNormal];
  217. }
  218. return _leftBtn;
  219. }
  220. - (UITableView *)tableView{
  221. if (!_tableView) {
  222. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  223. _tableView.backgroundColor = UIColor.clearColor;
  224. _tableView.delegate = self;
  225. _tableView.dataSource = self;
  226. [_tableView registerClass:FriendListCell.class forCellReuseIdentifier:NSStringFromClass(FriendListCell.class)];
  227. }
  228. return _tableView;
  229. }
  230. - (NSMutableArray *)dataArray{
  231. if (!_dataArray) {
  232. _dataArray = [NSMutableArray array];
  233. }
  234. return _dataArray;
  235. }
  236. - (NSMutableArray *)groupArray{
  237. if (!_groupArray) {
  238. _groupArray = [NSMutableArray array];
  239. }
  240. return _groupArray;
  241. }
  242. #pragma mark statusBar
  243. - (UIStatusBarStyle)preferredStatusBarStyle{
  244. return UIStatusBarStyleLightContent;
  245. }
  246. @end