// // KDPNoticeViewController.m // KuDianProject // // Created by admin on 2019/7/11. // Copyright © 2019 KDP. All rights reserved. // #import "KDPNoticeViewController.h" #import "KDPNoticeModel.h" #import "KDPMessageTableViewCell.h" #import "KDPForecastVC.h" #import "KDPAccountVC.h" @interface KDPNoticeViewController () @property (nonatomic, strong) UITableView *tableView; @property(nonatomic,strong)UIView *messageNoView; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) UIButton *clearBtn; @end @implementation KDPNoticeViewController -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tabBarController.tabBar.hidden=YES; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpNav]; [self.view addSubview:self.messageNoView]; if (![self isOpenNotice]) {//是否开启通知 self.messageNoView.hidden=NO; self.tableView.frame=CGRectMake(0, KDNavBarHeight+50, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight-50); }else{ self.messageNoView.hidden=YES; self.tableView.frame=CGRectMake(0, KDNavBarHeight+10, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight-10); } self.view.backgroundColor = [UIColor colorWithHex:0xF2F2F2]; [self.view addSubview:self.tableView]; } - (void)setUpNav{ self.navBar.navTitleLabel.text = @"我的消息"; [self.navBar addleftReturnButton:self selector:@selector(backAction)]; UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [clearBtn setImage:[UIImage imageNamed:@"clear_icon"] forState:UIControlStateNormal]; clearBtn.frame = CGRectMake(SCREEN_WIDTH-17-16, KDNavBarHeight-16-17, 17, 17); [clearBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside]; clearBtn.hidden=YES; [self.navBar addSubview:clearBtn]; self.clearBtn = clearBtn; } - (void)backAction{ if (self.presentingViewController) { [self dismissViewControllerAnimated:YES completion:nil]; } else{ [self.navigationController popViewControllerAnimated:YES]; } } - (void)clearAction{ NSString *clearUrl = [NSString stringWithFormat:@"%@api/message_push/allMessageRead",KDURL]; [KDPNetworkRequestHTTP postURL:clearUrl params:nil success:^(id _Nonnull json) { if ([json[@"flag"]intValue] == 1) { for (KDPNoticeModel *model in self.dataSource) { model.is_view = @"1"; } [MBProgressHUD showMessage:@"全部已读"]; [self.tableView reloadData]; } } failure:^(NSError * _Nonnull error) { }]; } - (void)requestMessageData{ NSDictionary *params = @{@"page":@(self.page)}; NSString *messageUrl = [NSString stringWithFormat:@"%@api/message_push/messagePushList",KDURL]; [LoadingView show]; [KDPNetworkRequestHTTP postURL:messageUrl params:params success:^(id _Nonnull json) { [LoadingView dismiss]; NSArray *list = [NSArray yy_modelArrayWithClass:[KDPNoticeModel class] json:json[@"data"]]; if (self.page == 1) { [self.dataSource removeAllObjects]; } if (list.count>0) { [self.dataSource addObjectsFromArray:list]; [self.tableView.mj_footer endRefreshing]; [self.tableView.mj_header endRefreshing]; }else { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshingWithNoMoreData]; } self.clearBtn.hidden = self.dataSource.count ? NO : YES; [LoadingView dismiss]; [self.tableView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; }]; } - (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) style:UITableViewStylePlain]; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.rowHeight = 121; _tableView.emptyDataSetDelegate = self; _tableView.emptyDataSetSource = self; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSelectionStyleNone; _tableView.backgroundColor = [UIColor clearColor]; _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [_tableView registerClass:[KDPMessageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([KDPMessageTableViewCell class])]; _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; [self requestMessageData]; }]; _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.page ++; [self requestMessageData]; }]; [_tableView.mj_header beginRefreshing]; } return _tableView; } - (NSMutableArray *)dataSource{ if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ KDPMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([KDPMessageTableViewCell class])]; [cell configWithViewModel:self.dataSource[indexPath.row] indexpath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataSource.count; } - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"no_order"]; } - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { KDPNoticeModel *model =self.dataSource[indexPath.row]; [self messageisreadLoad:model]; switch (model.message_type.integerValue) { case 2://商品详情 [self goodDetailWithModel:model]; break; case 3://版本更新 [self updataArrrapp]; break; case 4://收入结算 [self ForecastVClist]; break; case 5://提现通过 [self myAccountList]; break; default: break; } } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}]; } - (CGFloat )spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{ return 30; } -(void)messageisreadLoad:(KDPNoticeModel *)model { [LoadingView show]; [KDPNetworkRequestHTTP postURL:[NSString stringWithFormat:@"%@api/message_push/messageClick",KDURL] params:@{@"message_id":model.Id,@"person_group":model.person_group,@"is_view":model.is_view} success:^(id _Nonnull json) { [LoadingView dismiss]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; } #pragma mark----收入结算 -(void)ForecastVClist { KDPForecastVC *forecastV =[[KDPForecastVC alloc]init]; [self.navigationController pushViewController:forecastV animated:YES]; } #pragma mark---商品详情 -(void)goodDetailWithModel:(KDPNoticeModel *)model { KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init]; KDPGoodsModel *goodM =[[KDPGoodsModel alloc]init]; goodM.goods_id=model.goods_id; detailVC.model = goodM; [self.navigationController pushViewController:detailVC animated:YES]; } /** 版本更新 */ - (void)updataArrrapp { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_STORE_URL]]; } #pragma mark---提现通过 -(void)myAccountList { KDPAccountVC *drawV =[[KDPAccountVC alloc]init]; [self.navigationController pushViewController:drawV animated:YES]; } -(UIView *)messageNoView { if (!_messageNoView) { _messageNoView =[[UIView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, 50)]; _messageNoView.backgroundColor=[UIColor colorWithHexString:LineColor]; UIButton *backBtn =[[UIButton alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH,30)]; backBtn.backgroundColor =[UIColor whiteColor]; [self.messageNoView addSubview:backBtn]; UIButton *openButton =[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-56, 6.5, 40,17)]; [openButton setTitle:@"开启" forState:UIControlStateNormal]; [openButton setTitleColor:[UIColor colorWithHexString:ThemeColor] forState:UIControlStateNormal]; openButton.layer.cornerRadius=8.5; openButton.layer.masksToBounds=YES; openButton.layer.borderColor =[UIColor colorWithHexString:ThemeColor].CGColor; openButton.layer.borderWidth=0.5; openButton.titleLabel.font =[UIFont systemFontOfSize:9]; [openButton addTarget:self action:@selector(openNotice) forControlEvents:UIControlEventTouchUpInside]; [backBtn addSubview:openButton]; UIImageView *messageIcon =[[UIImageView alloc]initWithFrame:CGRectMake(15, 7.5, 15, 15)]; messageIcon.image =[UIImage imageNamed:@"icon_message"]; [backBtn addSubview:messageIcon]; UILabel *tipL =[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)]; tipL.textColor=[UIColor colorWithHexString:ThemeColor]; tipL.font=[UIFont systemFontOfSize:12]; [backBtn addSubview:tipL]; tipL.text=@"您还未开启消息通知,开启获取最新通知"; tipL.tag = 1234; } return _messageNoView; } #pragma mark---开启通知 -(void)openNotice { UIApplication *application = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([application canOpenURL:url]) { if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { if (@available(iOS 10.0, *)) { [application openURL:url options:@{} completionHandler:nil]; } else { // Fallback on earlier versions } } else { [application openURL:url]; } } } //判断是否开启通知 - (BOOL)isOpenNotice { // 判断用户是否允许接收通知 BOOL isEnable = NO; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) { // iOS版本 >=8.0 处理逻辑 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; isEnable = (UIUserNotificationTypeNone == setting.types) ? NO : YES; } else { // iOS版本 <8.0 处理逻辑 UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; isEnable = (UIRemoteNotificationTypeNone == type) ? NO : YES; } return isEnable; } @end