YRMsgVC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // YRMsgVC.m
  3. // UU_Ent
  4. //
  5. // Created by liujl on 2019/4/29.
  6. // Copyright © 2019 UAS. All rights reserved.
  7. //
  8. #import "YRMsgVC.h"
  9. #import <UU_Ent/Message.pbrpc.h>
  10. #import "YRUserInfo.h"
  11. #import "YRMsgMainModel.h"
  12. #import "YRMsgMainCell.h"
  13. #import "YRWebVC.h"
  14. #import "YRTabBar.h"
  15. #import "YRMsgHeader.h"
  16. @interface YRMsgVC ()<UITableViewDelegate,UITableViewDataSource>
  17. @property(strong,nonatomic)YRTableView *tableView;
  18. @property(strong,nonatomic)NSMutableArray *dataArr;
  19. /**
  20. 头部视图
  21. */
  22. @property(strong,nonatomic)YRMsgHeader *headerView;
  23. /**
  24. 右边按钮
  25. */
  26. @property(strong,nonatomic)UIButton *rigBtn;
  27. /**
  28. 头部视图是否展开
  29. */
  30. @property(assign,nonatomic)BOOL isSpread;
  31. @end
  32. @implementation YRMsgVC
  33. - (instancetype)init
  34. {
  35. self = [super init];
  36. if (self) {
  37. [self regesterNotification];
  38. }
  39. return self;
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view.
  44. }
  45. -(void)viewWillAppear:(BOOL)animated{
  46. [super viewWillAppear:animated];
  47. [self setNavigationBarBackgroundImageWithAppear:YES];
  48. }
  49. -(void)createUI{
  50. self.navigationItem.title = @"消息";
  51. [self setNavigationBar];
  52. [self.view addSubview:self.tableView];
  53. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.edges.equalTo(self.view);
  55. }];
  56. [self setUpHeaderView];
  57. [self loadData];
  58. }
  59. -(void)setUpHeaderView{
  60. self.headerView = [[YRMsgHeader alloc]initWithFrame:CGRectMake(0, -121, self_width, 121)];
  61. self.tableView.estimatedRowHeight = 0;
  62. [self.tableView insertSubview:self.headerView atIndex:1];
  63. self.headerView.hidden = YES;
  64. }
  65. -(void)setNavigationBarBackgroundImageWithAppear:(BOOL)appear{
  66. if (appear) {
  67. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  68. if(statusXLately){
  69. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UU_newMsgNavBarX"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  70. }else{
  71. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UU_newMsgNavBar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  72. }
  73. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  74. }
  75. }
  76. -(void)setNavigationBar{
  77. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0]}];
  78. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  79. btn.frame = CGRectMake(0, 5, 22, 22);
  80. CGFloat imageY = 0;
  81. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0) {
  82. imageY = 5;
  83. }
  84. [btn setImage:[UIImage imageNamed:@"UU_msg_快捷打卡"] forState:UIControlStateNormal];
  85. self.rigBtn = btn;
  86. btn.tintColor = [UIColor whiteColor];
  87. [btn addTarget:self action:@selector(spreadOutHeaderOrNot:) forControlEvents:UIControlEventTouchUpInside];
  88. UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:btn];
  89. barButton.tintColor = [UIColor whiteColor];
  90. self.navigationItem.rightBarButtonItems = @[barButton];
  91. }
  92. /**
  93. 是否展开头部视图
  94. */
  95. -(void)spreadOutHeaderOrNot:(UIButton *)button{
  96. button.userInteractionEnabled = NO;
  97. if (!self.isSpread) {//缩起状态
  98. self.headerView.hidden = NO;
  99. [UIView animateWithDuration:1 animations:^{
  100. [self.tableView setContentOffset:CGPointMake(0, -121)];
  101. } completion:^(BOOL finished) {
  102. self.tableView.contentInset = UIEdgeInsetsMake(121, 0, 0,0);
  103. self.isSpread = !self.isSpread;
  104. button.userInteractionEnabled = YES;
  105. }];
  106. }else{//展开状态
  107. [UIView animateWithDuration:1 animations:^{
  108. [self.tableView setContentOffset:CGPointMake(0, 0)];
  109. } completion:^(BOOL finished) {
  110. self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0,0);
  111. self.headerView.hidden = YES;
  112. self.isSpread = !self.isSpread;
  113. button.userInteractionEnabled = YES;
  114. }];
  115. }
  116. }
  117. #pragma mark - 通知
  118. -(void)regesterNotification{
  119. //切换账套
  120. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:kYRUserMasterChangedNotification object:nil];
  121. }
  122. #pragma mark - 网络请求
  123. -(void)loadData{
  124. DLog(@"这里是消息的数据刷新");
  125. NSDictionary *comDic = [YRUserInfo shareManager].userCompanyInfo;
  126. if (comDic) {
  127. self.dataArr = [YRMsgMainModel mj_objectArrayWithKeyValuesArray:comDic[@"messageConfigArray"]];
  128. NSMutableArray *unArr =comDic[@"unreadMessageCountArray"];
  129. for (YRMsgMainModel *model in self.dataArr) {
  130. for (NSDictionary *unDic in unArr) {
  131. if ([model.code isEqualToString:FORMAT(@"%@",unDic[@"code"])]) {
  132. model.remindCount = [unDic[@"count"] integerValue]+arc4random_uniform(100);
  133. }
  134. }
  135. }
  136. [self calculteRemindCount];
  137. [self.tableView reloadData];
  138. }else{
  139. [self getDataFromServer];
  140. }
  141. }
  142. /**
  143. 获取配置信息
  144. */
  145. -(void)getDataFromServer{
  146. //获取配置信息
  147. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  148. MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
  149. GetMessageConfigRequest *request = [GetMessageConfigRequest message];
  150. GRPCProtoCall *call = [client RPCTogetConfigWithRequest:request handler:^(GetMessageConfigResponse * _Nullable response, NSError * _Nullable error) {
  151. if (!error) {//请求成功
  152. NSDictionary *dict = response.mj_keyValues;
  153. self.dataArr = [YRMsgMainModel mj_objectArrayWithKeyValuesArray:dict[@"configArray"]];
  154. [self getUnreadMessage];
  155. }else{//请求失败
  156. }
  157. }];
  158. call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
  159. [call start];
  160. }
  161. /**
  162. 获取未读消息条数
  163. */
  164. -(void)getUnreadMessage{
  165. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  166. MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
  167. GetUnreadMessageCountRequest *request = [GetUnreadMessageCountRequest message];
  168. GRPCProtoCall *call = [client RPCTogetUnreadCountWithRequest:request handler:^(GetUnreadMessageCountResponse * _Nullable response, NSError * _Nullable error) {
  169. [self.tableView.mj_header endRefreshing];
  170. for (YRMsgMainModel *model in self.dataArr) {
  171. for (UnreadMessageCount *msgUn in response.unreadArray) {
  172. if ([model.code isEqualToString:msgUn.code]) {
  173. model.remindCount = msgUn.count+arc4random_uniform(100);
  174. }
  175. }
  176. }
  177. [self calculteRemindCount];
  178. [self.tableView reloadData];
  179. }];
  180. call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
  181. [call start];
  182. }
  183. -(void)setMessageReadRequest:(YRMsgMainModel *)model{
  184. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  185. MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
  186. SetMessageReadRequest *request = [SetMessageReadRequest message];
  187. GRPCProtoCall *call = [client RPCTosetReadWithRequest:request handler:^(SetMessageReadResponse * _Nullable response, NSError * _Nullable error) {
  188. NSDictionary *dict = response.mj_keyValues;
  189. DLog(@"%@",dict);
  190. }];
  191. call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
  192. [call start];
  193. }
  194. #pragma mark - delegate && dataSource
  195. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  196. return 1;
  197. }
  198. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  199. return self.dataArr.count;
  200. }
  201. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  202. YRMsgMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YRMsgMainCellReid"];
  203. if (!cell) {
  204. cell = [[YRMsgMainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YRMsgMainCellReid"];
  205. }
  206. if (indexPath.row < self.dataArr.count) {
  207. cell.model = self.dataArr[indexPath.row];
  208. }
  209. return cell;
  210. }
  211. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  212. return 66;
  213. }
  214. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  215. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  216. YRMsgMainModel *model = self.dataArr[indexPath.row];
  217. if (model.viewType == 0) {//原生
  218. YRBaseVC *VC = (YRBaseVC *)[[NSClassFromString(model.desVC) alloc]init];
  219. if (!VC) {//出现异常
  220. VC = [YRBaseVC new];
  221. }
  222. VC.title = model.name;
  223. [self.navigationController pushViewController:VC animated:YES];
  224. }else{//网页
  225. YRWebVC *webVC = [[YRWebVC alloc]init];
  226. webVC.url = model.webURL;
  227. [self.navigationController pushViewController:webVC animated:YES];
  228. }
  229. }
  230. #pragma mark - 滑动
  231. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  232. //正在操作中不允许点击
  233. if(!self.rigBtn.userInteractionEnabled){
  234. return;
  235. }
  236. //展开状态
  237. if (self.isSpread) {
  238. //展开状态下不允许下拉
  239. if(scrollView.contentOffset.y < -121){
  240. [scrollView setContentOffset:CGPointMake(0, -121)];
  241. return;
  242. }
  243. //当滑动距离上面小于70时
  244. if (scrollView.contentOffset.y > -70)
  245. {
  246. self.rigBtn.userInteractionEnabled = NO;
  247. scrollView.userInteractionEnabled = NO;
  248. self.isSpread = NO;
  249. [UIView animateWithDuration:0.6 animations:^{
  250. self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
  251. } completion:^(BOOL finished) {
  252. [self.tableView setContentOffset:CGPointMake(0, 0)];
  253. scrollView.userInteractionEnabled = YES;
  254. self.headerView.hidden = YES;
  255. self.rigBtn.userInteractionEnabled = YES;
  256. }];
  257. }
  258. }
  259. }
  260. #pragma mark - 计数
  261. -(void)calculteRemindCount{
  262. YRTabBar *tabbar = (YRTabBar *)self.tabBarController.tabBar;
  263. NSUInteger remind = 0;
  264. for (YRMsgMainModel *model in self.dataArr) {
  265. remind += model.remindCount;
  266. }
  267. tabbar.remindCount = remind;
  268. }
  269. -(YRTableView *)tableView{
  270. if (!_tableView) {
  271. _tableView = [[YRTableView alloc]initWithFrame:self.view.frame];
  272. _tableView.delegate = self;
  273. _tableView.dataSource = self;
  274. _tableView.autoNoDataSet = NO;
  275. _tableView.tableHeaderView = [UIView new];
  276. _tableView.tableFooterView = [UIView new];
  277. @weakify(self)
  278. _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  279. @strongify(self)
  280. [self getDataFromServer];
  281. }];
  282. }
  283. return _tableView;
  284. }
  285. -(NSMutableArray *)dataArr{
  286. if (!_dataArr) {
  287. _dataArr = [[NSMutableArray alloc]init];
  288. }
  289. return _dataArr;
  290. }
  291. -(void)dealloc{
  292. //切换账套
  293. [[NSNotificationCenter defaultCenter] removeObserver:self name:kYRUserMasterChangedNotification object:nil];
  294. }
  295. @end