酷店

KDPTopViewController.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //
  2. // KDPTopViewController.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPTopViewController.h"
  9. #define liveEarningCell @"liveEarningCell"
  10. #define bannerCell @"bannerCell"
  11. #define recommendCell @"recommendCell"
  12. #define recommendHead @"recommendHead"
  13. @interface KDPTopViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
  14. {
  15. NSTimer *order_timer;
  16. NSInteger addNum;
  17. }
  18. @property(nonatomic,strong)NSMutableArray *dataArray;
  19. @property(nonatomic,strong)NSArray *bannerArray;
  20. @property(nonatomic,copy)NSString *orderNumber;
  21. @property(nonatomic,copy)NSString *order_rebate_money;
  22. @property(nonatomic,strong)UIButton *headImgBtn;//左侧头像按钮
  23. @property(nonatomic,strong)UIButton *messageButton;//右侧消息按钮
  24. @property(nonatomic,strong)UICollectionView *dataCollectionView;
  25. @property(nonatomic,strong)KDPServiceView *serviceView;
  26. //服务端传参
  27. @property(nonatomic,strong)NSString *pageNum;
  28. @property(nonatomic,strong)NSString *selection_page;
  29. @property(nonatomic,strong)NSString *selection_id;
  30. @property(nonatomic,strong)NSString *selection_id_count;
  31. @property(nonatomic,strong)NSString *selection_id_page;
  32. @end
  33. @implementation KDPTopViewController
  34. -(void)initialieValues
  35. {
  36. self.pageNum =@"1";
  37. self.selection_id=@"0";
  38. self.selection_id_count=@"0";
  39. self.selection_id_page=@"0";
  40. self.selection_page=@"0";
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. [self initialieValues];
  45. [self initUI];
  46. }
  47. #pragma makr---首页收益
  48. -(void)getUserTodayIncom
  49. {
  50. [KDPNetworkRequestHTTP postURL:topIncomeUrl params:nil success:^(id _Nonnull json) {
  51. //相差的金额,需要减去上次显示的数量
  52. NSInteger newnum=[json[@"num"] integerValue];
  53. addNum= newnum-self.orderNumber.integerValue;
  54. self.orderNumber =json[@"num"];//总数量;
  55. self.order_rebate_money = json[@"total_rebate"];
  56. [self.dataCollectionView reloadData];
  57. } failure:^(NSError * _Nonnull error) {
  58. }];
  59. }
  60. #pragma makr---首页banner
  61. -(void)getBanner
  62. {
  63. // [LoadingView show];
  64. [KDPNetworkRequestHTTP getURL:bannerURL params:nil success:^(id _Nonnull json) {
  65. [LoadingView dismiss];
  66. self.bannerArray=[NSArray yy_modelArrayWithClass:[KDPBannerModel class] json:json[@"data"]];
  67. [self.dataCollectionView reloadData];
  68. } failure:^(NSError * _Nonnull error) {
  69. [LoadingView dismiss];
  70. }];
  71. }
  72. #pragma makr---获取首页数据
  73. -(void)getRecommendGood
  74. {
  75. // [LoadingView show];
  76. NSDictionary *dic=@{@"page":self.pageNum,@"selection_page":self.selection_page,@"selection_id":self.selection_id,@"selection_id_page":self.selection_id_page,@"selection_id_count":self.selection_id_count};
  77. [KDPNetworkRequestHTTP postURL:topGoodRecommURL params:dic success:^(id _Nonnull json) {
  78. [LoadingView dismiss];
  79. self.selection_id_page = json[@"selection_id_count"];
  80. self.selection_id = json[@"selection_id"];
  81. self.selection_page = json[@"selection_page"];
  82. self.selection_id_count = json[@"selection_id_count"];
  83. if (self.pageNum.integerValue == 1) {
  84. [self.dataArray removeAllObjects];
  85. }
  86. NSArray *currentA=[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]];
  87. [self.dataCollectionView.mj_header endRefreshing];
  88. [self.dataCollectionView.mj_footer endRefreshing];
  89. if (currentA.count == 0) {
  90. [self.dataCollectionView.mj_footer endRefreshingWithNoMoreData];
  91. }
  92. [self.dataArray addObjectsFromArray:currentA];
  93. [self.dataCollectionView reloadData];
  94. } failure:^(NSError * _Nonnull error) {
  95. [LoadingView dismiss];
  96. [self.dataCollectionView.mj_header endRefreshing];
  97. [self.dataCollectionView.mj_footer endRefreshing];
  98. }];
  99. }
  100. -(void)initUI
  101. {
  102. self.navBar.navTitleLabel.text=@"酷店";
  103. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
  104. [self.navBar addSubview:self.headImgBtn];
  105. [self.navBar addSubview:self.messageButton];
  106. [self.headImgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.left.mas_equalTo(10);
  108. make.top.mas_equalTo(KDStatusHeight+4);
  109. make.height.width.mas_equalTo(32);
  110. }];
  111. // UIImageView *imgV=[[UIImageView alloc]init];
  112. // imgV.image=[UIImage imageNamed:@"LIVE"];
  113. // [self.navBar addSubview:imgV];
  114. // [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  115. // make.left.mas_equalTo(self.headImgBtn.mas_left).offset(5);
  116. // make.width.mas_equalTo(22);
  117. // make.height.mas_equalTo(9);
  118. // make.top.mas_equalTo(self.headImgBtn.mas_bottom).offset(-7);
  119. // }];
  120. [self.messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.right.mas_equalTo(-10);
  122. make.top.mas_equalTo(KDStatusHeight+10);
  123. make.width.height.mas_equalTo(22);
  124. }];
  125. [_messageButton ba_moveBadgeWithX:22 Y:2];
  126. [self.view addSubview:self.dataCollectionView];
  127. [self.dataCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  128. make.left.right.bottom.mas_equalTo(0);
  129. make.top.mas_equalTo(KDNavBarHeight);
  130. }];
  131. }
  132. #pragma mark---collectionDelegate&&DataSource
  133. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. if (indexPath.section == 1) {
  136. KDPBannerCell *bannerC=[collectionView dequeueReusableCellWithReuseIdentifier:bannerCell forIndexPath:indexPath];
  137. bannerC.bannerArr = self.bannerArray;
  138. bannerC.bannerBlock = ^(KDPBannerModel * _Nonnull model) {
  139. if (model.click_type.integerValue == 1) {//h5
  140. KDPWebInteractionVC *web=[[KDPWebInteractionVC alloc]init];
  141. web.url=model.click_param;
  142. [self.navigationController pushViewController:web animated:YES];
  143. }else if (model.click_type.integerValue == 2){//商品详情
  144. KDPGoodDetailVC *goodD=[[KDPGoodDetailVC alloc]init];
  145. KDPGoodsModel *models=[[KDPGoodsModel alloc]init];
  146. models.goods_id=model.click_param;
  147. goodD.model=models;
  148. [self.navigationController pushViewController:goodD animated:YES];
  149. }else if (model.click_type.integerValue == 3)
  150. {//x专题列表
  151. KDPProjectListVC *listV=[[KDPProjectListVC alloc]init];
  152. listV.navBar.navTitleLabel.text=model.title;
  153. listV.projectID=model.click_param;
  154. if (model.type.length == 0) {
  155. listV.type = @"module";
  156. }else{
  157. listV.type = model.type;
  158. }
  159. [self.navigationController pushViewController:listV animated:YES];
  160. }
  161. };
  162. return bannerC;
  163. }
  164. if (indexPath.section == 2) {
  165. KDPRecommendGoodCell *goodC=[collectionView dequeueReusableCellWithReuseIdentifier:recommendCell forIndexPath:indexPath];
  166. goodC.model=self.dataArray[indexPath.row];
  167. return goodC;
  168. }
  169. KDPLiveEarningCell *earinCell =[collectionView dequeueReusableCellWithReuseIdentifier:liveEarningCell forIndexPath:indexPath];
  170. if (addNum>0) {
  171. earinCell.addNumL.text=[NSString stringWithFormat:@"+%ld",addNum];
  172. }else{
  173. earinCell.addNumL.text=@"";
  174. }
  175. earinCell.orderNumgL.text=self.orderNumber;
  176. earinCell.rebet_money=self.order_rebate_money;
  177. return earinCell;
  178. }
  179. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  180. {
  181. return 3;
  182. }
  183. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  184. {
  185. if (section == 2) {
  186. return self.dataArray.count;
  187. }
  188. if (section == 1&& self.bannerArray.count==1) {
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  194. {
  195. if (indexPath.section == 1) {
  196. if (self.bannerArray.count==0 ) {
  197. return CGSizeMake(SCREEN_WIDTH, 0.1);
  198. }
  199. return CGSizeMake(SCREEN_WIDTH, 113);
  200. }
  201. if (indexPath.section == 2) {
  202. return CGSizeMake(SCREEN_WIDTH, 121);
  203. }
  204. return CGSizeMake(SCREEN_WIDTH, 116);
  205. }
  206. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  207. {
  208. if (indexPath.section == 0) {
  209. KDPTodayLiveDataVC *todayV=[[KDPTodayLiveDataVC alloc]init];
  210. [self.navigationController pushViewController:todayV animated:YES];
  211. }
  212. if (indexPath.section == 2) {
  213. KDPGoodDetailVC *goodDetail=[[KDPGoodDetailVC alloc]init];
  214. KDPGoodsModel *models=self.dataArray[indexPath.row];
  215. goodDetail.model=models;
  216. [self.navigationController pushViewController:goodDetail animated:YES];
  217. }
  218. }
  219. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  220. {
  221. if (section == 2) {
  222. return CGSizeMake(SCREEN_WIDTH, 60);
  223. }
  224. return CGSizeMake(0, 0 );
  225. }
  226. -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  227. {
  228. if (indexPath.section == 2) {
  229. KDPRecommendHeadView *headV=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead forIndexPath:indexPath];
  230. return headV;
  231. }
  232. return nil;
  233. }
  234. -(NSMutableArray *)dataArray
  235. {
  236. if (!_dataArray) {
  237. _dataArray=[NSMutableArray array];
  238. }
  239. return _dataArray;
  240. }
  241. #pragma mark---页面布局
  242. -(UIButton *)headImgBtn
  243. {
  244. if (!_headImgBtn) {
  245. _headImgBtn=[[UIButton alloc]init];
  246. _headImgBtn.backgroundColor=[UIColor colorWithHexString:ThemeColor];
  247. _headImgBtn.layer.cornerRadius=16;
  248. _headImgBtn.layer.masksToBounds=YES;
  249. _headImgBtn.layer.borderColor=[UIColor colorWithHexString:@"#FFB444"].CGColor;
  250. UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 28, 28)];
  251. imgv.layer.cornerRadius=14;
  252. imgv.layer.masksToBounds=YES;
  253. imgv.image=[UIImage imageNamed:placholderImg];
  254. imgv.tag=1000;
  255. [_headImgBtn addSubview:imgv];
  256. _headImgBtn.layer.borderWidth=1;
  257. [_headImgBtn addTarget:self action:@selector(drawerClickButton) forControlEvents:UIControlEventTouchUpInside];
  258. _headImgBtn.adjustsImageWhenHighlighted=NO;
  259. }
  260. return _headImgBtn;
  261. }
  262. -(UIButton *)messageButton
  263. {
  264. if (!_messageButton) {
  265. _messageButton=[[UIButton alloc]init];
  266. [_messageButton setImage:[UIImage imageNamed:@"message_icon"] forState:UIControlStateNormal];
  267. _messageButton.adjustsImageWhenHighlighted=NO;
  268. [_messageButton addTarget:self action:@selector(messageclickButton) forControlEvents:UIControlEventTouchUpInside];
  269. [_messageButton ba_setBadgeLabelAttributes:^(BABadgeLabel *badgeLabel) {
  270. UILabel *label = (UILabel *)badgeLabel;
  271. label.width=10;
  272. label.textColor = [UIColor baseColor];
  273. label.backgroundColor = [UIColor redColor];
  274. label.height = label.width;
  275. label.font = [UIFont systemFontOfSize:8];
  276. }];
  277. [_messageButton ba_hiddenBadge];
  278. }
  279. return _messageButton;
  280. }
  281. -(UICollectionView *)dataCollectionView
  282. {
  283. if (!_dataCollectionView) {
  284. UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
  285. layout.minimumLineSpacing=0;
  286. layout.minimumInteritemSpacing=0;
  287. _dataCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight-KDTabBarHeight) collectionViewLayout:layout];
  288. _dataCollectionView.backgroundColor=[UIColor colorWithHexString:LineColor];
  289. [_dataCollectionView registerClass:[KDPLiveEarningCell class] forCellWithReuseIdentifier:liveEarningCell];
  290. [_dataCollectionView registerClass:[KDPBannerCell class] forCellWithReuseIdentifier:bannerCell];
  291. [_dataCollectionView registerClass:[KDPRecommendGoodCell class] forCellWithReuseIdentifier:recommendCell];
  292. [_dataCollectionView registerClass:[KDPRecommendHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead];
  293. [_dataCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
  294. _dataCollectionView.showsVerticalScrollIndicator=NO;
  295. _dataCollectionView.delegate=self;
  296. _dataCollectionView.dataSource=self;
  297. _dataCollectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
  298. [self getBanner];
  299. [self initialieValues];
  300. [self getRecommendGood];
  301. }];
  302. _dataCollectionView.emptyDataSetDelegate = self;
  303. _dataCollectionView.emptyDataSetSource = self;
  304. if (@available(iOS 11.0, *)) {
  305. _dataCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  306. } else {
  307. self.automaticallyAdjustsScrollViewInsets = NO;
  308. } _dataCollectionView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  309. self.pageNum=[NSString stringWithFormat:@"%ld",self.pageNum.integerValue+1];
  310. [self getRecommendGood];
  311. }];
  312. }
  313. return _dataCollectionView;
  314. }
  315. #pragma mark---侧滑
  316. -(void)drawerClickButton
  317. {
  318. KDPDrawerVC *vcFrame =[[KDPDrawerVC alloc]init];
  319. CWLateralSlideConfiguration *con =[CWLateralSlideConfiguration defaultConfiguration];
  320. con.distance=SCREEN_WIDTH/3*2;
  321. [self cw_showDrawerViewController:vcFrame animationType:0 configuration:con];
  322. }
  323. #pragma mark---消息中心
  324. -(void)messageclickButton
  325. {
  326. KDPNoticeViewController *noticeV=[[KDPNoticeViewController alloc]init];
  327. [self.navigationController pushViewController:noticeV animated:YES];
  328. }
  329. -(void)viewWillAppear:(BOOL)animated
  330. {
  331. [super viewWillAppear:animated];
  332. self.tabBarController.tabBar.hidden=NO;
  333. [self updateData];
  334. }
  335. -(void)viewWillDisappear:(BOOL)animated
  336. {
  337. [super viewWillDisappear:animated];
  338. [order_timer invalidate];
  339. }
  340. #pragma mark---更新数据
  341. -(void)updateData
  342. {
  343. if ([KDPAccountTool isLogin]) {
  344. [self getUserTodayIncom];
  345. [self isOpenService];
  346. if (self.pageNum.integerValue ==1) {
  347. [self initialieValues];
  348. }
  349. [self getRecommendGood];
  350. [self getBanner];
  351. [self messageUnreadNumData];
  352. order_timer = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(order_timerAction) userInfo:nil repeats:YES];
  353. [[NSRunLoop mainRunLoop] addTimer:order_timer forMode:NSDefaultRunLoopMode];
  354. [self setUserHeadImg];
  355. }
  356. }
  357. -(void)order_timerAction
  358. {
  359. if ([KDPAccountTool isLogin]) {
  360. [self getUserTodayIncom];
  361. }
  362. }
  363. #pragma mark----设置头像
  364. -(void)setUserHeadImg
  365. {
  366. KDPAccountModel *model=[KDPAccountTool account];
  367. UIImageView *imgv=[self.headImgBtn viewWithTag:1000];
  368. [imgv sd_setImageWithURL:[NSURL URLWithString:model.img]placeholderImage:[UIImage imageNamed:placholderImg]];
  369. }
  370. #pragma mark---打开服务提示页面
  371. -(KDPServiceView *)serviceView
  372. {
  373. if (!_serviceView) {
  374. _serviceView=[[KDPServiceView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  375. __weak KDPTopViewController *weakself=self;
  376. _serviceView.serviceBlock = ^{
  377. KDPWebInteractionVC *pidV=[[KDPWebInteractionVC alloc]init];
  378. pidV.url=custSerURL;
  379. [weakself cw_pushViewController:pidV];
  380. [weakself.serviceView removeFromSuperview];
  381. };
  382. }
  383. return _serviceView;
  384. }
  385. #pragma mark---获取未读消息个数
  386. -(void)messageUnreadNumData
  387. {
  388. if ([KDPAccountTool isLogin]) {
  389. [KDPNetworkRequestHTTP postURL:message_numURL params:nil success:^(id _Nonnull json) {
  390. if ([json[@"data"][@"message_new"] integerValue] == 1) {//是否有新消息 0:无 1:有
  391. [self.messageButton ba_showBadge];
  392. }else{
  393. [self.messageButton ba_hiddenBadge];
  394. }
  395. } failure:^(NSError * _Nonnull error) {
  396. }];
  397. }
  398. }
  399. #pragma mark---判断是否打开客服页面
  400. -(void)isOpenService
  401. {
  402. //存储第一次打开APP,是否显示客服页面,
  403. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  404. NSString *oneStep =[defaults objectForKey:@"step"];
  405. if (oneStep.integerValue == 0) {
  406. [UIView animateWithDuration:0.3 animations:^{
  407. [[UIApplication sharedApplication].keyWindow addSubview:self.serviceView];
  408. }];
  409. [defaults setValue:@"1" forKey:@"step"];
  410. [defaults synchronize];
  411. }
  412. }
  413. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  414. return [UIImage imageNamed:@"no_order"];
  415. }
  416. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  417. return YES;
  418. }
  419. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  420. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  421. }
  422. - (CGFloat )spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  423. return 30;
  424. }
  425. @end