addFriendController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // addFriendController.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/6/25.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "addFriendController.h"
  9. #import "FriendNetApi.h"
  10. @interface addFriendController()<UISearchBarDelegate>
  11. @property (weak, nonatomic) IBOutlet UIView *cntentView;
  12. @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
  13. @property (weak, nonatomic) IBOutlet UIImageView *avarvImg;
  14. @property (weak, nonatomic) IBOutlet UILabel *nameLb;
  15. @property (weak, nonatomic) IBOutlet UITextView *beizhuT;
  16. @property (weak, nonatomic) IBOutlet UIButton *quxiaoBt;
  17. @property (weak, nonatomic) IBOutlet UIButton *tianjiaBt;
  18. @property (weak, nonatomic) IBOutlet UILabel *beizhuNoteLb;
  19. @property (nonatomic,strong) NSString *keyword;
  20. @property (nonatomic,strong) NSDictionary *userDic;
  21. @end
  22. @implementation addFriendController
  23. -(void)viewDidLoad{
  24. [super viewDidLoad];
  25. [self.navigationController setNavigationBarHidden:YES animated:NO];
  26. // 创建一个点击手势
  27. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
  28. // 将手势添加到主视图上
  29. [self.view addGestureRecognizer:tap];
  30. _searchBar.delegate = self;
  31. [_searchBar becomeFirstResponder];
  32. _searchBar.searchTextField.backgroundColor =[UIColor whiteColor];
  33. // [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage new] forState:UIControlStateNormal]; // 移除背景图片
  34. // [[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setSearchFieldBackgroundImage:[UIImage new] forState:UIControlStateNormal]; // 仅在 UINavigationBar 中移除背景图片
  35. // 设置文本颜色
  36. [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
  37. _avarvImg.layer.cornerRadius = 35;
  38. _cntentView.layer.cornerRadius = 8.0f;
  39. _cntentView.layer.borderWidth = 0.5f;
  40. _cntentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
  41. _cntentView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
  42. _beizhuT.layer.cornerRadius = 8.0f;
  43. _beizhuT.layer.borderColor = [UIColor whiteColor].CGColor;
  44. _beizhuT.layer.backgroundColor = [UIColor whiteColor].CGColor;
  45. _beizhuT.textColor = UIColor.blackColor;
  46. _nameLb.text = @"没有数据";
  47. [_tianjiaBt setBackgroundColor:globalColor(GCTypeGreen)];
  48. _tianjiaBt.layer.cornerRadius = 8;
  49. _tianjiaBt.layer.masksToBounds = YES; // 防止子视图超出圆角边界
  50. _tianjiaBt.tintColor = UIColor.blackColor;
  51. self.userDic=nil;
  52. }
  53. -(void)dismissKeyboard{
  54. NSLog(@"dismissKeyboard");
  55. [self.view endEditing:YES];
  56. }
  57. - (IBAction)gotoBack:(id)sender {
  58. NSLog(@"gotoBackgotoBackgotoBack");
  59. [self dismissViewControllerAnimated:YES completion:nil];
  60. }
  61. - (IBAction)addFriendBt:(id)sender {
  62. if(self.userDic==nil){
  63. return;
  64. }
  65. NSDictionary *dis = @{
  66. @"friendId":self.userDic[@"id"],
  67. @"message":_beizhuT.text
  68. };
  69. [FriendNetApi shenqingADDfriend:dis succ:^(int code, NSDictionary * res) {
  70. NSLog(@"res:%@",res);
  71. self.userDic = nil;
  72. [self dismissViewControllerAnimated:YES completion:nil];
  73. } fail:^(NSError * _Nonnull error) {
  74. NSLog(@"error:%@",error);
  75. }];
  76. }
  77. - (IBAction)cancelBt:(id)sender {
  78. [self searchFriend];
  79. }
  80. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  81. // 当搜索文本改变时调用
  82. NSLog(@"Search text changed to: %@", searchText);
  83. self.keyword = searchText;
  84. }
  85. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  86. // 当用户点击搜索按钮时调用
  87. NSLog(@"Search button clicked with text: %@", searchBar.text);
  88. [searchBar resignFirstResponder]; // 隐藏键盘
  89. self.keyword = searchBar.text;
  90. [self searchFriend];
  91. }
  92. -(void)searchFriend{
  93. if(self.keyword.length>0){
  94. [FriendNetApi searchfriend:self.keyword succ:^(int code, NSDictionary * res) {
  95. NSLog(@"res:%@",res);
  96. NSArray *data= res[@"data"];
  97. if(data.count>0){
  98. self.userDic =data[0];
  99. NSLog(@"1:%@",self.userDic);
  100. [self getData];
  101. }
  102. else{
  103. }
  104. } fail:^(NSError * _Nonnull error) {
  105. }];
  106. }
  107. }
  108. -(void)getData{
  109. if(self.userDic==nil){
  110. self.nameLb.text = @"没有数据";
  111. _avarvImg.alpha = 0;
  112. return;
  113. }
  114. self.nameLb.text =self.userDic[@"name"];
  115. _avarvImg.alpha=1;
  116. NSString *url = self.userDic[@"avatar"];
  117. if([url isKindOfClass:[NSNull class]]){
  118. url=@"";
  119. }
  120. [_avarvImg sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"Avatar"]];
  121. }
  122. @end