UserInfoViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. @interface UserInfoViewController ()
  19. @property (weak, nonatomic) IBOutlet UIImageView *bannerImage;
  20. @property (weak, nonatomic) IBOutlet UIView *avatarContainer;
  21. @property (weak, nonatomic) IBOutlet UIImageView *genderImage;
  22. @property (weak, nonatomic) IBOutlet UILabel *nickLabel;
  23. @property (weak, nonatomic) IBOutlet UILabel *idLabel;
  24. @property (weak, nonatomic) IBOutlet UIButton *followBtn;
  25. @property (weak, nonatomic) IBOutlet UIButton *msgBtn;
  26. @property (weak, nonatomic) IBOutlet UIButton *giftBtn;
  27. @property (weak, nonatomic) IBOutlet UILabel *picTitle;
  28. @property (weak, nonatomic) IBOutlet UIView *picContainer;
  29. @property (weak, nonatomic) IBOutlet UILabel *morePicLabel;
  30. @property (weak, nonatomic) IBOutlet UILabel *ageLabel;
  31. @property (weak, nonatomic) IBOutlet UILabel *hobbyLabel;
  32. @property (weak, nonatomic) IBOutlet UILabel *addressLabel;
  33. @property (nonatomic, strong) NIMAvatarImageView *avatar;
  34. @property (nonatomic, strong) NSString *userId;
  35. @property (nonatomic, strong) NIMUser *nimUser;
  36. @property (nonatomic, strong) UserInfo *userInfo;
  37. @property (nonatomic, assign) int followState;
  38. @end
  39. @implementation UserInfoViewController
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. self.tableView.contentInset = UIEdgeInsetsMake(-69, 0, 0, 0);
  44. _followBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  45. _followBtn.layer.borderWidth = 1;
  46. _followBtn.layer.cornerRadius = _followBtn.height/2;
  47. _msgBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  48. _msgBtn.layer.borderWidth = 1;
  49. _msgBtn.layer.cornerRadius = _msgBtn.height/2;
  50. _giftBtn.layer.cornerRadius = _giftBtn.height/2;
  51. if(_userInfo)
  52. {
  53. [self followState:_userInfo.follow];
  54. }
  55. }
  56. - (void)didReceiveMemoryWarning {
  57. [super didReceiveMemoryWarning];
  58. // Dispose of any resources that can be recreated.
  59. }
  60. - (void)viewWillAppear:(BOOL)animated{
  61. [super viewWillAppear:animated];
  62. [self.navigationItem setTitle:@""];
  63. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  64. [appDelegate transparentNavBar:self];
  65. }
  66. - (void)viewWillDisappear:(BOOL)animated{
  67. [super viewWillDisappear:animated];
  68. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  69. [appDelegate orangeNavBar:self];
  70. }
  71. - (void)setUserId:(NSString *)userId{
  72. __weak typeof(self) wself = self;
  73. [[HttpRequest shared] userInfo:userId success:^(UserInfo *userInfo) {
  74. [wself setUser:userInfo];
  75. } failure:^{
  76. }];
  77. }
  78. - (void)setUser:(UserInfo *)userInfo{
  79. _userId = userInfo.userId;
  80. self.userInfo = userInfo;
  81. [self followState:userInfo.follow];
  82. [self fetchNIMUser];
  83. }
  84. - (void)fetchNIMUser{
  85. __weak typeof(self) wself = self;
  86. [[NIMSDK sharedSDK].userManager fetchUserInfos:@[_userId] completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  87. _nimUser = [users objectAtIndex:0];
  88. [wself fillInfo];
  89. }];
  90. }
  91. - (void)fillInfo
  92. {
  93. if(!_avatar)
  94. {
  95. _avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, _avatarContainer.width, _avatarContainer.height)];
  96. [_avatarContainer addSubview:_avatar];
  97. }
  98. NSURL *url = _nimUser.userInfo.avatarUrl ? [NSURL URLWithString:_nimUser.userInfo.avatarUrl] : nil;
  99. [_avatar nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  100. [_genderImage setImage:[[User sharedInfo] getGenderImg:_nimUser]];
  101. _nickLabel.text = [[User sharedInfo] getUserName:_nimUser];
  102. _idLabel.text = [NSString stringWithFormat:@"ID:%@", _nimUser.userId];
  103. if(_userInfo.showAge > 0)
  104. {
  105. if(_userInfo.age > 0)
  106. {
  107. _ageLabel.text = [@(_userInfo.age) stringValue];
  108. }
  109. else
  110. {
  111. _ageLabel.text = @"未知";
  112. }
  113. }
  114. else
  115. {
  116. _ageLabel.text = @"保密";
  117. }
  118. NSString *hobby = _userInfo.hobbyStr;
  119. if(hobby && hobby.length > 0)
  120. {
  121. _hobbyLabel.text = hobby;
  122. }
  123. else
  124. {
  125. _hobbyLabel.text = @"";
  126. }
  127. if(_userInfo.city && _userInfo.district && _userInfo.city.length > 0 && _userInfo.district.length > 0)
  128. {
  129. if([_userInfo.city isEqualToString:_userInfo.district])
  130. {
  131. _addressLabel.text = _userInfo.district;
  132. }
  133. else
  134. {
  135. _addressLabel.text = [NSString stringWithFormat:@"%@%@", _userInfo.city, _userInfo.district];
  136. }
  137. }
  138. else
  139. {
  140. _addressLabel.text = @"未知";
  141. }
  142. NSString *imgURL = _userInfo.socialBanner;
  143. url = imgURL && imgURL.length > 0 ? [NSURL URLWithString:imgURL] : nil;
  144. [_bannerImage sd_setImageWithURL:url placeholderImage:User.defaultSocialBanner];
  145. }
  146. - (void)followState:(int)state{
  147. _followState = state;
  148. if(state == 1)
  149. {
  150. [_followBtn setTitle:@"已关注" forState:UIControlStateNormal];
  151. }
  152. else if(state == 0)
  153. {
  154. [_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
  155. }
  156. }
  157. - (IBAction)onTouchFollow:(id)sender {
  158. __weak typeof(self) wself = self;
  159. [[HttpRequest shared] follow:_userId success:^(int state) {
  160. [wself followState:state];
  161. if(state == 1)
  162. [wself.view makeToast:@"关注成功" duration:2.0 position:CSToastPositionCenter];
  163. else
  164. [wself.view makeToast:@"关注取消" duration:2.0 position:CSToastPositionCenter];
  165. } failure:^{
  166. [wself.view makeToast:@"操作失败" duration:2.0 position:CSToastPositionCenter];
  167. }];
  168. }
  169. - (IBAction)onTouchMsg:(id)sender {
  170. NIMSession *session = [NIMSession session:_userId type:NIMSessionTypeP2P];
  171. NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:session];
  172. [self.navigationController pushViewController:vc animated:YES];
  173. }
  174. - (IBAction)onTouchGift:(id)sender {
  175. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  176. UserGiftViewController *userGiftVC = [board instantiateViewControllerWithIdentifier:@"UserGift"];
  177. [userGiftVC setUser:_nimUser];
  178. [self.navigationController pushViewController:userGiftVC animated:YES];
  179. }
  180. - (IBAction)onTouchMorePic:(id)sender {
  181. NSLog(@"onTouchMorePic");
  182. }
  183. @end