酷店

KDPTopViewController.m 17KB

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