UserInfoViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // UserInfoViewController.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/14.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "UserInfoViewController.h"
  9. #import "UIView+NTES.h"
  10. #import "NIMAvatarImageView.h"
  11. #import "UIImageView+WebCache.h"
  12. #import "AppDelegate.h"
  13. #import "User.h"
  14. #import "NTESSessionViewController.h"
  15. #import "UserGiftViewController.h"
  16. #import "HttpRequest.h"
  17. #import "UIView+Toast.h"
  18. #import "UserConfigViewController.h"
  19. #import "AddPhotoViewController.h"
  20. #import "ImagePagerViewController.h"
  21. #import "SocialTableViewController.h"
  22. #import "SVProgressHUD.h"
  23. #import "UIActionSheet+NTESBlock.h"
  24. @interface UserInfoViewController ()
  25. @property (weak, nonatomic) IBOutlet UIView *topView;
  26. @property (weak, nonatomic) IBOutlet UIImageView *bannerImage;
  27. @property (weak, nonatomic) IBOutlet UIView *avatarContainer;
  28. @property (weak, nonatomic) IBOutlet UIImageView *genderImage;
  29. @property (weak, nonatomic) IBOutlet UILabel *nickLabel;
  30. @property (weak, nonatomic) IBOutlet UILabel *idLabel;
  31. @property (weak, nonatomic) IBOutlet UIButton *followBtn;
  32. @property (weak, nonatomic) IBOutlet UIButton *msgBtn;
  33. @property (weak, nonatomic) IBOutlet UIButton *giftBtn;
  34. @property (weak, nonatomic) IBOutlet UIView *photoContainer;
  35. @property (weak, nonatomic) IBOutlet UIView *socialContainer;
  36. @property (weak, nonatomic) IBOutlet UILabel *picTitle;
  37. @property (weak, nonatomic) IBOutlet UIView *picContainer;
  38. @property (weak, nonatomic) IBOutlet UILabel *morePicLabel;
  39. @property (weak, nonatomic) IBOutlet UILabel *ageLabel;
  40. @property (weak, nonatomic) IBOutlet UILabel *hobbyLabel;
  41. @property (weak, nonatomic) IBOutlet UILabel *addressLabel;
  42. @property (weak, nonatomic) IBOutlet UIButton *kickBtn;
  43. @property (nonatomic, strong) NIMAvatarImageView *avatar;
  44. @property (nonatomic, strong) NSString *userId;
  45. @property (nonatomic, strong) NIMUser *nimUser;
  46. @property (nonatomic, strong) UserInfo *userInfo;
  47. @property (nonatomic, assign) int followState;
  48. @property (nonatomic, strong) NSMutableArray *photoPicArr;
  49. @property (nonatomic, strong) NSMutableArray *photoThumbArr;
  50. @property (nonatomic, strong) NSMutableArray *socialPicArr;
  51. @property (nonatomic, strong) NSMutableArray *socialThumbArr;
  52. @end
  53. @implementation UserInfoViewController
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. // Do any additional setup after loading the view.
  57. self.tableView.contentInset = UIEdgeInsetsMake(-69, 0, 0, 0);
  58. _bannerImage.clipsToBounds = YES;
  59. _followBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  60. _followBtn.layer.borderWidth = 1;
  61. _followBtn.layer.cornerRadius = _followBtn.height/2;
  62. _msgBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  63. _msgBtn.layer.borderWidth = 1;
  64. _msgBtn.layer.cornerRadius = _msgBtn.height/2;
  65. _giftBtn.layer.cornerRadius = _giftBtn.height/2;
  66. _kickBtn.layer.cornerRadius = _kickBtn.height/2;
  67. if(_userInfo)
  68. {
  69. [self followState:_userInfo.follow];
  70. }
  71. else if(self.title && self.title.intValue > 0)
  72. {
  73. [self setUserId:self.title];
  74. }
  75. [self fetchGift];
  76. [_picContainer addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchMorePic:)]];
  77. }
  78. - (void)didReceiveMemoryWarning {
  79. [super didReceiveMemoryWarning];
  80. // Dispose of any resources that can be recreated.
  81. }
  82. - (void)viewWillAppear:(BOOL)animated{
  83. [super viewWillAppear:animated];
  84. [self.navigationItem setTitle:@""];
  85. UIButton *configBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  86. [configBtn setImage:[UIImage imageNamed:@"群成员信息_03"] forState:UIControlStateNormal];
  87. [configBtn addTarget:self action:@selector(onTouchConfig:) forControlEvents:UIControlEventTouchUpInside];
  88. [configBtn sizeToFit];
  89. UIBarButtonItem *configItem = [[UIBarButtonItem alloc] initWithCustomView:configBtn];
  90. [self.navigationItem setRightBarButtonItems:@[configItem]];
  91. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  92. [appDelegate transparentNavBar:self];
  93. }
  94. - (void)viewWillDisappear:(BOOL)animated{
  95. [super viewWillDisappear:animated];
  96. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  97. [appDelegate orangeNavBar:self];
  98. }
  99. - (void)setUserId:(NSString *)userId{
  100. __weak typeof(self) wself = self;
  101. [[HttpRequest shared] userInfo:userId search:NO success:^(UserInfo *userInfo) {
  102. [wself setUser:userInfo];
  103. } failure:^{
  104. }];
  105. }
  106. - (void)setUser:(UserInfo *)userInfo{
  107. _userId = userInfo.userId;
  108. self.userInfo = userInfo;
  109. [self followState:userInfo.follow];
  110. [self fetchNIMUser];
  111. [self fetchSocial];
  112. [self fetchGift];
  113. [self checkTeamKick];
  114. }
  115. - (void)checkTeamKick
  116. {
  117. if(_team == nil ||
  118. ![_team.owner isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount] ||
  119. [_team.owner isEqualToString:_userId])
  120. {
  121. _kickBtn.hidden = YES;
  122. return;
  123. }
  124. _kickBtn.hidden = NO;
  125. }
  126. - (void)fetchGift
  127. {
  128. __weak typeof(self) wself = self;
  129. [[User sharedInfo] requestGiftList:^(NSMutableArray * _Nullable list) {
  130. if(list.count == 0)
  131. {
  132. float centerX = wself.msgBtn.superview.width/2;
  133. wself.followBtn.right = centerX - 20;
  134. wself.msgBtn.left = centerX + 20;
  135. wself.giftBtn.hidden = YES;
  136. }
  137. } failure:^(NSError * _Nullable error) {
  138. }];
  139. }
  140. - (void)fetchNIMUser{
  141. __weak typeof(self) wself = self;
  142. [[NIMSDK sharedSDK].userManager fetchUserInfos:@[_userId] completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  143. _nimUser = [users objectAtIndex:0];
  144. [wself fillInfo];
  145. }];
  146. }
  147. - (void)fetchSocial{
  148. __weak typeof(self) wself = self;
  149. [[HttpRequest shared] getSocialDataList:nil targetId:_userId success:^(NSMutableArray * _Nullable dataList) {
  150. _socialPicArr = [[NSMutableArray alloc] init];
  151. _socialThumbArr = [[NSMutableArray alloc] init];
  152. for(int i=0; i<dataList.count; i++)
  153. {
  154. SocialItemData *data = [dataList objectAtIndex:i];
  155. [_socialPicArr addObjectsFromArray:data.picArr];
  156. [_socialThumbArr addObjectsFromArray:data.thumbArr];
  157. }
  158. [wself fillSocial];
  159. } failure:^{
  160. }];
  161. }
  162. - (void)fillSocial{
  163. float space = 5;
  164. float size = _picContainer.height;
  165. for(int i=0; i<_socialThumbArr.count; i++)
  166. {
  167. float x = i*(size + space);
  168. if(x+size > _picContainer.width)
  169. {
  170. break;
  171. }
  172. NSString *thumb = [_socialThumbArr objectAtIndex:i];
  173. NSURL *url = [NSURL URLWithString:thumb];
  174. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, size, size)];
  175. imgView.tag = i;
  176. [imgView setUserInteractionEnabled:YES];
  177. [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchSocialImage:)]];
  178. [imgView sd_setImageWithURL:url placeholderImage:User.defaultPlaceHolderImage];
  179. [_picContainer addSubview:imgView];
  180. }
  181. }
  182. - (void)fillInfo
  183. {
  184. if(!_avatar)
  185. {
  186. _avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, _avatarContainer.width, _avatarContainer.height)];
  187. [_avatar setUserInteractionEnabled:YES];
  188. [_avatar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  189. [_avatarContainer addSubview:_avatar];
  190. }
  191. NSURL *url = nil;
  192. if(_nimUser)
  193. {
  194. url = _nimUser.userInfo.avatarUrl ? [NSURL URLWithString:_nimUser.userInfo.avatarUrl] : nil;
  195. [_avatar nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  196. [_genderImage setImage:[[User sharedInfo] getGenderImg:_nimUser]];
  197. _nickLabel.text = [[User sharedInfo] getUserName:_nimUser];
  198. _idLabel.text = [NSString stringWithFormat:@"ID:%@", _userInfo.ssid];
  199. }
  200. if(_userInfo.showAge > 0)
  201. {
  202. if(_userInfo.age > 0)
  203. {
  204. _ageLabel.text = [@(_userInfo.age) stringValue];
  205. }
  206. else
  207. {
  208. _ageLabel.text = @"未知";
  209. }
  210. }
  211. else
  212. {
  213. _ageLabel.text = @"保密";
  214. }
  215. NSString *hobby = _userInfo.hobbyStr;
  216. if(hobby && hobby.length > 0)
  217. {
  218. _hobbyLabel.text = hobby;
  219. }
  220. else
  221. {
  222. _hobbyLabel.text = @"";
  223. }
  224. if(_userInfo.address && _userInfo.address.length > 0)
  225. {
  226. _addressLabel.text = _userInfo.address;
  227. }
  228. else
  229. {
  230. _addressLabel.text = @"未知";
  231. }
  232. NSString *imgURL = _userInfo.socialBanner;
  233. url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
  234. [_bannerImage sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner];
  235. int maxPhoto = 8;
  236. int numPhotos = _userInfo.photeArr.count;
  237. int columns = 4;
  238. int space = 2;
  239. float size = (self.view.width - space * (columns-1)) / columns;
  240. _photoPicArr = [[NSMutableArray alloc] init];
  241. _photoThumbArr = [[NSMutableArray alloc] init];
  242. for(int i=0; i<numPhotos; i++)
  243. {
  244. float x = (i % columns) * (size + space);
  245. float y = (i / columns) * (size + space);
  246. NSDictionary *photo = [_userInfo.photeArr objectAtIndex:i];
  247. NSString *thumb = [photo objectForKey:@"thumb"];
  248. NSString *pic = [photo objectForKey:@"pic"];
  249. NSURL *url = [NSURL URLWithString:thumb];
  250. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, size, size)];
  251. [imgView sd_setImageWithURL:url placeholderImage:User.defaultPlaceHolderImage];
  252. imgView.tag = i;
  253. [imgView setUserInteractionEnabled:YES];
  254. [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchImage:)]];
  255. [_photoContainer addSubview:imgView];
  256. [_photoThumbArr addObject:thumb];
  257. [_photoPicArr addObject:pic];
  258. if(i >= maxPhoto)
  259. break;
  260. }
  261. int row = (int)ceilf((float)numPhotos / (float)columns);
  262. row = MIN(row, 2);
  263. if(row > 0)
  264. _photoContainer.height = row * size + (row - 1) * space;
  265. _socialContainer.top = _photoContainer.bottom;
  266. _topView.height = _socialContainer.bottom;
  267. [self.tableView reloadData];
  268. }
  269. - (void)followState:(int)state{
  270. _followState = state;
  271. if(state == 1)
  272. {
  273. [_followBtn setTitle:@"已关注" forState:UIControlStateNormal];
  274. }
  275. else if(state == 0)
  276. {
  277. [_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
  278. }
  279. }
  280. - (IBAction)onTouchFollow:(id)sender {
  281. __weak typeof(self) wself = self;
  282. [[HttpRequest shared] follow:_userId success:^(int state) {
  283. [wself followState:state];
  284. if(state == 1)
  285. [wself.view makeToast:@"关注成功" duration:2.0 position:CSToastPositionCenter];
  286. else
  287. [wself.view makeToast:@"关注取消" duration:2.0 position:CSToastPositionCenter];
  288. } failure:^{
  289. [wself.view makeToast:@"操作失败" duration:2.0 position:CSToastPositionCenter];
  290. }];
  291. }
  292. - (IBAction)onTouchMsg:(id)sender {
  293. NIMSession *session = [NIMSession session:_userId type:NIMSessionTypeP2P];
  294. NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:session];
  295. [self.navigationController pushViewController:vc animated:YES];
  296. }
  297. - (IBAction)onTouchGift:(id)sender {
  298. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  299. UserGiftViewController *userGiftVC = [board instantiateViewControllerWithIdentifier:@"UserGift"];
  300. [userGiftVC setUser:_nimUser];
  301. [self.navigationController pushViewController:userGiftVC animated:YES];
  302. }
  303. - (IBAction)onTouchMorePic:(id)sender {
  304. NSLog(@"onTouchMorePic");
  305. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  306. SocialTableViewController *vc = [board instantiateViewControllerWithIdentifier:@"TargetSocial"];
  307. vc.userInfo = _userInfo;
  308. [self.navigationController pushViewController:vc animated:YES];
  309. }
  310. - (void)onTouchConfig:(id)sender{
  311. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  312. UserConfigViewController *vc = [board instantiateViewControllerWithIdentifier:@"UserConfig"];
  313. [vc setUserId:_userId];
  314. [self.navigationController pushViewController:vc animated:YES];
  315. }
  316. - (void)onTouchImage:(UITapGestureRecognizer *)gestureRecognizer{
  317. UIView *viewClicked = [gestureRecognizer view];
  318. NSLog(@"show image %ld", (long)viewClicked.tag);
  319. int index = (int)viewClicked.tag;
  320. ImagePagerViewController *imagePageVC = [[ImagePagerViewController alloc] init];
  321. [imagePageVC setImages:_photoPicArr thumbs:_photoThumbArr startIndex:index];
  322. [self.navigationController pushViewController:imagePageVC animated:YES];
  323. }
  324. - (void)onTouchSocialImage:(UITapGestureRecognizer *)gestureRecognizer{
  325. UIView *viewClicked = [gestureRecognizer view];
  326. NSLog(@"show image %ld", (long)viewClicked.tag);
  327. int index = (int)viewClicked.tag;
  328. ImagePagerViewController *imagePageVC = [[ImagePagerViewController alloc] init];
  329. NSString *image = [_socialPicArr objectAtIndex:index];
  330. NSString *thumb = [_socialThumbArr objectAtIndex:index];
  331. [imagePageVC setImage:image thumb:thumb];
  332. [self.navigationController pushViewController:imagePageVC animated:YES];
  333. }
  334. - (void)onTouchAvatar:(UITapGestureRecognizer *)gestureRecognizer{
  335. if(_nimUser && _nimUser.userInfo && _nimUser.userInfo.avatarUrl)
  336. {
  337. ImagePagerViewController *vc = [[ImagePagerViewController alloc] init];
  338. [vc setImage:_nimUser.userInfo.avatarUrl thumb:_nimUser.userInfo.thumbAvatarUrl];
  339. [self.navigationController pushViewController:vc animated:YES];
  340. }
  341. }
  342. - (IBAction)onTouchKick:(id)sender {
  343. if(_team == nil)
  344. {
  345. return;
  346. }
  347. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定移除改成员?" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
  348. __weak UIActionSheet *wSheet;
  349. __weak typeof(self) wself = self;
  350. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  351. if (index == wSheet.destructiveButtonIndex) {
  352. [wself removeMember];
  353. }
  354. }];
  355. }
  356. - (void)removeMember{
  357. [SVProgressHUD show];
  358. [[NIMSDK sharedSDK].teamManager kickUsers:@[_userId] fromTeam:_team.teamId completion:^(NSError * _Nullable error) {
  359. [SVProgressHUD dismiss];
  360. if(!error) {
  361. [self.view makeToast:@"移除成功"];
  362. [self.navigationController popViewControllerAnimated:YES];
  363. } else {
  364. [self.view makeToast:@"移除失败"];
  365. }
  366. }];
  367. }
  368. @end