NotiferMessageViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // NotiferMessageViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/4/18.
  6. //
  7. #import "NotiferMessageViewController.h"
  8. #import "NotiferMessageCell.h"
  9. #import "NotiferHeadView.h"
  10. #import "NotiferMessageModel.h"
  11. #import "KSBaseWKWebViewController.h"
  12. #import "CustomNavViewController.h"
  13. //#import "ReceiveEvaluateListController.h"
  14. #import "HomeworkDetailViewController.h"
  15. #import "UIButton+EnlargeEdge.h"
  16. @interface NotiferMessageViewController ()<UITableViewDelegate, UITableViewDataSource>
  17. @property (nonatomic, strong) NotiferHeadView *headView;
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSString *groupType;
  20. @end
  21. @implementation NotiferMessageViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. [self allocTitle:@"消息盒子"];
  26. [self configRightButton];
  27. [self configUI];
  28. }
  29. - (void)viewWillAppear:(BOOL)animated {
  30. [super viewWillAppear:animated];
  31. [self queryUnReadCount];
  32. [self resetSourceAndRequest];
  33. }
  34. - (void)configRightButton {
  35. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  36. [rightButton setTitle:@"全部已读" forState:UIControlStateNormal];
  37. [rightButton setTitleColor:HexRGB(0x999999) forState:UIControlStateNormal];
  38. [rightButton setImage:[UIImage imageNamed:@"clear_notifer"] forState:UIControlStateNormal];
  39. rightButton.frame =CGRectMake(0, 0, 80, 40);
  40. //使图片在右边,文字在左边(正常情况下是文字在右边,图片在左边)
  41. [rightButton setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  42. //设置图片和文字之间的间隙
  43. rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  44. [rightButton.titleLabel setFont:[UIFont systemFontOfSize:11.0f]];
  45. [rightButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
  46. [rightButton addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  47. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  48. self.navigationItem.rightBarButtonItem = rightItem;
  49. }
  50. - (void)rightBtnClick {
  51. [KSNetworkingManager batchSetReadRequest:KS_POST success:^(NSDictionary * _Nonnull dic) {
  52. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  53. [self MBPShow:@"清除成功"];
  54. [self resetSourceAndRequest];
  55. }
  56. else {
  57. [self MBPShow:MESSAGEKEY];
  58. }
  59. } faliure:^(NSError * _Nonnull error) {
  60. }];
  61. }
  62. - (void)configUI {
  63. [self.scrollView removeFromSuperview];
  64. [self.view addSubview:self.headView];
  65. [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.right.top.mas_equalTo(self.view);
  67. make.height.mas_equalTo(100);
  68. }];
  69. [self.view addSubview:self.tableView];
  70. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.right.mas_equalTo(self.view);
  72. make.top.mas_equalTo(self.headView.mas_bottom);
  73. make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
  74. }];
  75. MJWeakSelf;
  76. self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  77. [weakSelf resetSourceAndRequest];
  78. }];
  79. self.tableView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  80. if (weakSelf.isLoadMore) {
  81. weakSelf.pages += 1;
  82. [weakSelf requestData];
  83. }
  84. else {
  85. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  86. }
  87. }];
  88. }
  89. - (void)resetSourceAndRequest {
  90. [self resetParamenter];
  91. [self requestData];
  92. [self queryUnReadCount];
  93. }
  94. - (void)queryUnReadCount {
  95. [KSNetworkingManager queryCountOfUnreadRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  96. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  97. NSArray *countArray = [dic arrayValueForKey:@"data"];
  98. NSInteger courseCount = 0;
  99. NSInteger sysCount = 0;
  100. NSInteger noticeCount = 0;
  101. for (NSDictionary *parm in countArray) {
  102. if ([[parm stringValueForKey:@"key"] isEqualToString:@"COURSE"]) {
  103. courseCount = [parm integerValueForKey:@"value"];
  104. }
  105. if ([[parm stringValueForKey:@"key"] isEqualToString:@"SYSTEM"]) {
  106. sysCount = [parm integerValueForKey:@"value"];
  107. }
  108. if ([[parm stringValueForKey:@"key"] isEqualToString:@"NOTICE"]) {
  109. noticeCount = [parm integerValueForKey:@"value"];
  110. }
  111. }
  112. [self.headView configUnreadCountCourse:courseCount sysMessage:sysCount noticeCount:noticeCount];
  113. }
  114. else {
  115. [self MBPShow:MESSAGEKEY];
  116. }
  117. } faliure:^(NSError * _Nonnull error) {
  118. }];
  119. }
  120. - (void)resetParamenter {
  121. self.pages = 1;
  122. self.isLoadMore = YES;
  123. self.dataArray = [NSMutableArray array];
  124. [self.tableView.mj_footer resetNoMoreData];
  125. [self setPromptString:@"暂无消息~" imageName:@"empty_message" inView:self.tableView];
  126. [self.tableView reloadData];
  127. }
  128. - (void)endRefresh {
  129. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  130. [self.tableView.mj_header endRefreshing];
  131. [self.tableView.mj_footer endRefreshing];
  132. });
  133. }
  134. - (void)requestData {
  135. [self showhud];
  136. [KSNetworkingManager sysMessageListRequest:KS_POST group:self.groupType page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  137. [self removehub];
  138. [self endRefresh];
  139. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  140. // rows
  141. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  142. for (NSDictionary *parm in sourceArray) {
  143. NotiferMessageModel *model = [[NotiferMessageModel alloc] initWithDictionary:parm];
  144. [self.dataArray addObject:model];
  145. }
  146. if (sourceArray.count < self.rows) {
  147. self.isLoadMore = NO;
  148. }
  149. }
  150. else {
  151. [self MBPShow:MESSAGEKEY];
  152. }
  153. [self.tableView reloadData];
  154. [self changePromptLabelState];
  155. } faliure:^(NSError * _Nonnull error) {
  156. [self removehub];
  157. [self endRefresh];
  158. if (self.networkAvaiable == NO) {
  159. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  160. }
  161. [self.dataArray removeAllObjects];
  162. [self.tableView reloadData];
  163. [self changePromptLabelState];
  164. }];
  165. }
  166. #pragma mark ----- table data source
  167. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  168. return self.dataArray.count;
  169. }
  170. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  171. NotiferMessageModel *model = self.dataArray[indexPath.row];
  172. NotiferMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotiferMessageCell"];
  173. [cell configCellWithSource:model];
  174. return cell;
  175. }
  176. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  177. // 选择跳转
  178. NotiferMessageModel *model = self.dataArray[indexPath.row];
  179. if (model.readStatus == 0) {
  180. NotiferMessageCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  181. [self readMessage:model baseCell:cell];
  182. }
  183. else {
  184. // 跳转页面
  185. [self toDetailViewWithTypeString:model.memo];
  186. }
  187. }
  188. - (void)readMessage:(NotiferMessageModel *)model baseCell:(NotiferMessageCell *)cell {
  189. [KSNetworkingManager setReadMessage:KS_POST messageId:model.internalBaseClassIdentifier success:^(NSDictionary * _Nonnull dic) {
  190. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  191. model.readStatus = 1;
  192. cell.isRead = YES;
  193. // 跳转页面
  194. [self toDetailViewWithTypeString:model.memo];
  195. }
  196. else {
  197. [self MBPShow:MESSAGEKEY];
  198. }
  199. } faliure:^(NSError * _Nonnull error) {
  200. }];
  201. }
  202. - (void)toDetailViewWithTypeString:(NSString *)memo {
  203. if (![NSString isEmptyString:memo]) {
  204. NSString *headStr = [[memo componentsSeparatedByString:@"?"] firstObject];
  205. if ([headStr isEqualToString:@"buyPractice"] || [headStr isEqualToString:@"courseRemind"]) { // 课表
  206. [self toCourseTable];
  207. }
  208. else if ([headStr isEqualToString:@"evaluate"]) { // 评价页面
  209. // ReceiveEvaluateListController *ctrl = [[ReceiveEvaluateListController alloc] init];
  210. // [self.navigationController pushViewController:ctrl animated:YES];
  211. }
  212. else if ([headStr isEqualToString:@"H5"]) { // web
  213. NSString *valueStr = [[memo componentsSeparatedByString:@"?"] lastObject];
  214. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  215. webCtrl.url = valueStr;
  216. CustomNavViewController *navCtrl = self.tabBarController.selectedViewController;
  217. [navCtrl pushViewController:webCtrl animated:YES];
  218. }
  219. else if ([headStr isEqualToString:@"homework"]) { // 作业详情
  220. NSString *valueStr = [[memo componentsSeparatedByString:@"?"] lastObject];
  221. NSArray *parmArray = [valueStr componentsSeparatedByString:@"&"];
  222. NSString *courseId = nil;
  223. for (NSString *subStr in parmArray) {
  224. if ([subStr containsString:@"courseId"]) {
  225. courseId = [[subStr componentsSeparatedByString:@"="] lastObject];
  226. }
  227. }
  228. HomeworkDetailViewController *detailVC = [[HomeworkDetailViewController alloc] init];
  229. detailVC.courseId = courseId;
  230. [self.navigationController pushViewController:detailVC animated:YES];
  231. }
  232. }
  233. }
  234. - (void)toCourseTable {
  235. [self selectBarHomeWithIndex:1];
  236. [self.navigationController popToRootViewControllerAnimated:YES];
  237. }
  238. #pragma mark ---- lazying
  239. - (NotiferHeadView *)headView {
  240. if (!_headView) {
  241. _headView = [NotiferHeadView shareIntance];
  242. MJWeakSelf;
  243. [_headView chooseTypeCallback:^(NOTIFER_TYPE type) {
  244. [weakSelf chooseTypeSort:type];
  245. }];
  246. }
  247. return _headView;
  248. }
  249. - (void)chooseTypeSort:(NOTIFER_TYPE)type {
  250. switch (type) {
  251. case NOTIFER_TYPE_ALL:
  252. {
  253. self.groupType = nil;
  254. }
  255. break;
  256. case NOTIFER_TYPE_COURSE:
  257. {
  258. self.groupType = @"COURSE";
  259. }
  260. break;
  261. case NOTIFER_TYPE_SYSMSG:
  262. {
  263. self.groupType = @"SYSTEM";
  264. }
  265. break;
  266. case NOTIFER_TYPE_NOTICE:
  267. {
  268. self.groupType = @"NOTICE";
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. [self resetSourceAndRequest];
  275. }
  276. - (UITableView *)tableView {
  277. if (!_tableView) {
  278. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  279. _tableView.backgroundColor = [UIColor clearColor];
  280. _tableView.delegate = self;
  281. _tableView.dataSource = self;
  282. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  283. [_tableView registerNib:[UINib nibWithNibName:@"NotiferMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NotiferMessageCell"];
  284. _tableView.estimatedRowHeight = 125.0f;
  285. _tableView.rowHeight = UITableViewAutomaticDimension;
  286. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
  287. bottomView.backgroundColor = HexRGB(0xf6f8f9);
  288. _tableView.tableFooterView = bottomView;
  289. }
  290. return _tableView;
  291. }
  292. /*
  293. #pragma mark - Navigation
  294. // In a storyboard-based application, you will often want to do a little preparation before navigation
  295. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  296. // Get the new view controller using [segue destinationViewController].
  297. // Pass the selected object to the new view controller.
  298. }
  299. */
  300. @end