口袋优选

KBSignInViewController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // KBSignInViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/9/27.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBSignInViewController.h"
  9. #import "KBSignInView.h"
  10. #import "KBSignInModel.h"
  11. #import "KBSignTypeModel.h"
  12. #import "KBSignPopView.h"
  13. #import "KBSignSuccView.h"
  14. #import "CCAlertShowView.h"
  15. #import "KBSignInClickView.h"
  16. #import "KBRollingCollectionViewCell.h"
  17. #import "KBPopRouleView.h"
  18. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  19. #import <AlibabaAuthSDK/ALBBSession.h>
  20. #import <AlibabaAuthSDK/ALBBSDK.h>
  21. @interface KBSignInViewController ()<KBSignInClickViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  22. {
  23. UIButton *signBtn;
  24. UILabel *tipLb;
  25. CGFloat collectionViewWidth;
  26. }
  27. @property (nonatomic ,strong) UICollectionView *userCollectionView;
  28. @property (nonatomic ,strong) NSMutableArray *userArray;
  29. @property (nonatomic ,strong) NSMutableArray *collectionCellWithArry;
  30. @property (nonatomic ,strong) NSTimer *usertimer;
  31. @property (nonatomic, strong) KBSignInView *signView;
  32. @property (nonatomic, strong) KBSignInModel *signInModel;
  33. //@property (nonatomic, strong) KBSignTypeModel *signTypeModel;
  34. @property (nonatomic, strong) KBSignInClickView *signTableView;
  35. @property (nonatomic, strong) UIImageView *contentImgView;
  36. @property (nonatomic, strong) UIImageView *topImgView;
  37. @property (nonatomic, strong) UIImageView *middleImgView;
  38. @end
  39. @implementation KBSignInViewController
  40. - (void)dealloc {
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  46. [self getSignAction];
  47. [self signUserRollingAction];
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. self.userArray = [NSMutableArray arrayWithCapacity:0];
  52. self.collectionCellWithArry = [NSMutableArray arrayWithCapacity:0];
  53. [self configNavigationBar];
  54. [self initSubViews];
  55. [self addObserverForApp];
  56. }
  57. - (void)addObserverForApp {
  58. [[NSNotificationCenter defaultCenter] addObserver:self
  59. selector:@selector(getSignAction)
  60. name:UIApplicationDidBecomeActiveNotification object:nil];
  61. }
  62. - (void)initSubViews {
  63. UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  64. bgImgView.image = [UIImage imageNamed:@"signIn_bg"];
  65. [self.view addSubview:bgImgView];
  66. self.contentImgView = [[UIImageView alloc] init];
  67. self.contentImgView.image = [UIImage imageNamed:@"signInContent_bg"];
  68. [self.view addSubview:self.contentImgView];
  69. [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.top.mas_equalTo(NavBarHeight+87);
  71. make.width.mas_equalTo(FITSIZE(323));
  72. make.height.mas_equalTo(FITSIZE(472));
  73. make.centerX.mas_equalTo(self.view);
  74. }];
  75. self.topImgView = [[UIImageView alloc] init];
  76. [self.view addSubview: self.topImgView];
  77. [self.topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.top.mas_equalTo(NavBarHeight+52);
  79. make.width.mas_equalTo(FITSIZE(266));
  80. make.height.mas_equalTo(FITSIZE(72));
  81. make.centerX.mas_equalTo(self.view);
  82. }];
  83. self.middleImgView = [[UIImageView alloc] init];
  84. [self.view addSubview: self.middleImgView];
  85. [self.middleImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.top.mas_equalTo(self.topImgView.mas_bottom).offset(24);
  87. make.width.mas_equalTo(FITSIZE(314));
  88. make.height.mas_equalTo(FITSIZE(38));
  89. make.centerX.mas_equalTo(self.view);
  90. }];
  91. self.signView = [[KBSignInView alloc] init];
  92. self.signView.backgroundColor = [UIColor clearColor];
  93. [self.view addSubview:self.signView];
  94. [self.signView mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.top.mas_equalTo(self.middleImgView.mas_bottom).offset(23);
  96. make.width.mas_equalTo(self.contentImgView);
  97. make.height.mas_equalTo(20);
  98. make.centerX.mas_equalTo(self.view);
  99. }];
  100. [self.view addSubview:self.signTableView];
  101. [self.signTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.top.mas_equalTo(self.signView.mas_bottom).offset(10);
  103. make.width.mas_equalTo(self.contentImgView);
  104. make.height.mas_equalTo(FITSIZE(72*4));
  105. make.left.mas_equalTo(self.contentImgView.mas_left);
  106. }];
  107. [self.view addSubview:self.userCollectionView];
  108. UIButton *ruleBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  109. [ruleBtn setTitle:@"红包规则" forState:UIControlStateNormal];
  110. [ruleBtn setTitleColor:[UIColor YHColorWithHex:0x8B7117] forState:UIControlStateNormal];
  111. ruleBtn.backgroundColor=[UIColor clearColor];
  112. ruleBtn.titleLabel.font=[UIFont systemFontOfSize:12];
  113. [self.view addSubview:ruleBtn];
  114. [ruleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  115. make.top.mas_equalTo(self.contentImgView.mas_bottom).offset(23);
  116. make.centerX.mas_equalTo(self.view);
  117. make.width.mas_equalTo(SCREEN_WIDTH-FITSIZE(320));
  118. make.height.mas_equalTo(FITSIZE(22));
  119. }];
  120. [[ruleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
  121. KBPopRouleView *popView = [[KBPopRouleView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(290)) text:self.signInModel.red_roule];
  122. CCAlertShowView *showView = [CCAlertShowView alertViewWithView:popView];
  123. [showView show];
  124. }];
  125. }
  126. - (void)getSignAction {
  127. NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/getUserActiveNew",BaseURL];
  128. [KBHttp get:url params:nil success:^(id json) {
  129. self.signInModel = [KBSignInModel yy_modelWithJSON:json[@"data"]];
  130. if (self.signInModel.sign == 1) {
  131. [self refreshUI];
  132. }else {
  133. [self refreshUI];
  134. KBSignPopView *popView = [[KBSignPopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(382)) text:self.signInModel.red_message];
  135. popView.clickBtn.hidden = NO;
  136. CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:NO];
  137. [showView show];
  138. [[popView.clickBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
  139. [self signAction];
  140. [showView dismiss];
  141. }];
  142. }
  143. } failure:^(NSError *error) {
  144. }];
  145. }
  146. - (void)signAction {
  147. NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/complateActive",BaseURL];
  148. [KBHttp get:url params:nil success:^(id json) {
  149. [self getSignAction];
  150. } failure:^(NSError *error) {
  151. }];
  152. }
  153. - (void)refreshUI {
  154. [self.topImgView sd_setImageWithURL:[NSURL URLWithString:self.signInModel.sign_img] placeholderImage:nil];
  155. [self.middleImgView sd_setImageWithURL:[NSURL URLWithString:self.signInModel.describe_img] placeholderImage:nil];
  156. [self.signView setSignModelArr:self.signInModel.days];
  157. [self.signTableView refreshTableViewWithArry:self.signInModel.detail];
  158. }
  159. - (void)configNavigationBar {
  160. [self.navigationBar setNavTitle:@"签到领红包"];
  161. self.navigationBar.navTitleLabel.textColor = [UIColor blackColor];
  162. self.navigationBar.backgroundColor = [UIColor whiteColor];
  163. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  164. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  165. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  166. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  167. }
  168. //- (void)showSuccessView {
  169. // KBSignSuccView *succView = [[KBSignSuccView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(290)) text:self.signTypeModel.red_message];
  170. // CCAlertShowView *showView = [CCAlertShowView alertViewWithView:succView];
  171. // succView.sureClick = ^{
  172. // [showView dismiss];
  173. // };
  174. // [showView show];
  175. //
  176. //}
  177. - (void)clickViewIndexPath:(NSIndexPath *)index {
  178. [self changeBtnTypeWhenClick:index];
  179. KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]];
  180. NSString *tbUrl = model.ot_url;
  181. if (tbUrl.length > 0) {
  182. [self jumpToTaobaoWithUrl:tbUrl parentVc:self];
  183. UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
  184. pasteBoard.string = model.red_url==nil?@"":model.red_url;
  185. return;
  186. }
  187. NSURL *url = [NSURL URLWithString:@"taobao://"];
  188. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  189. KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]];
  190. UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
  191. pasteBoard.string = model.red_url==nil?@"":model.red_url;
  192. [[UIApplication sharedApplication] openURL:url];
  193. }else{
  194. [MBProgressHUD showMessage:self.signInModel.uninstall_taobao];
  195. }
  196. }
  197. - (void)changeBtnTypeWhenClick:(NSIndexPath *)index {
  198. KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]];
  199. NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/ButtonChange",BaseURL];
  200. NSString *nowDay = self.signInModel.now_days==nil?@"":self.signInModel.now_days;
  201. NSString *Id = model.btn==nil?@"":model.btn;
  202. NSDictionary *para = @{@"days":nowDay,
  203. @"red":Id
  204. };
  205. [KBHttp get:url params:para success:^(id json) {
  206. } failure:^(NSError *error) {
  207. }];
  208. }
  209. - (KBSignInClickView *)signTableView {
  210. if (!_signTableView) {
  211. _signTableView = [[KBSignInClickView alloc] init];
  212. _signTableView.clickViewDelegate = self;
  213. }
  214. return _signTableView;
  215. }
  216. #pragma mark UICollectionView滚动
  217. - (void)signUserRollingAction {
  218. NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/getRollscreen",BaseURL];
  219. [KBHttp get:url params:nil success:^(id json) {
  220. [self.userArray removeAllObjects];
  221. [self.collectionCellWithArry removeAllObjects];
  222. NSArray *jsonArry = [NSArray arrayWithArray:json[@"data"]];
  223. for (NSDictionary *userDic in jsonArry) {
  224. [self.userArray addObject:[KBSignInRollingUser yy_modelWithJSON:userDic]];
  225. }
  226. for (KBSignInRollingUser *userModel in self.userArray) {
  227. CGFloat cellWidth = 20+[self getWidhWithString:userModel.res withFontSize:12]+10;
  228. [self.collectionCellWithArry addObject:[NSNumber numberWithFloat:cellWidth]];
  229. collectionViewWidth+=cellWidth;
  230. }
  231. [self makeTime];
  232. [self.userCollectionView reloadData];
  233. } failure:^(NSError *error) {
  234. }];
  235. }
  236. - (void)makeTime {
  237. if (collectionViewWidth>SCREEN_WIDTH) {
  238. if(!self.usertimer){
  239. self.usertimer=[NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(clicknetxt) userInfo:nil repeats:YES];
  240. [[NSRunLoop mainRunLoop] addTimer:self.usertimer forMode:NSRunLoopCommonModes];
  241. }
  242. }
  243. }
  244. -(void)clicknetxt
  245. {
  246. self.userCollectionView.contentOffset=CGPointMake(self.userCollectionView.contentOffset.x+1, self.userCollectionView.contentOffset.y);
  247. }
  248. - (UICollectionView *)userCollectionView {
  249. if (!_userCollectionView) {
  250. UICollectionViewFlowLayout *layout1=[[UICollectionViewFlowLayout alloc]init];
  251. layout1.scrollDirection=UICollectionViewScrollDirectionHorizontal;
  252. _userCollectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0,NavBarHeight, SCREEN_WIDTH,27+9*2) collectionViewLayout:layout1];
  253. _userCollectionView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
  254. [_userCollectionView registerClass:[KBRollingCollectionViewCell class] forCellWithReuseIdentifier:@"KBRollingCollectionViewCell"];
  255. _userCollectionView.delegate=self;
  256. _userCollectionView.dataSource=self;
  257. _userCollectionView.backgroundColor = [UIColor clearColor];
  258. _userCollectionView.showsHorizontalScrollIndicator=NO;
  259. }
  260. return _userCollectionView;
  261. }
  262. #pragma mark collection代理
  263. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  264. {
  265. return self.userArray.count;
  266. }
  267. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  268. {
  269. KBRollingCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"KBRollingCollectionViewCell" forIndexPath:indexPath];
  270. KBSignInRollingUser *model=self.userArray[indexPath.item];
  271. cell.model=model;
  272. return cell;
  273. }
  274. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  275. {
  276. return UIEdgeInsetsMake(0, 10, 0, 0);
  277. }
  278. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. CGFloat cellWidth = [self.collectionCellWithArry[indexPath.row] floatValue];
  281. return CGSizeMake(cellWidth, self.userCollectionView.frame.size.height);
  282. }
  283. - (CGFloat)getWidhWithString:(NSString *)string withFontSize:(CGFloat)fontSize {
  284. NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
  285. CGSize textSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
  286. return textSize.width;
  287. }
  288. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  289. {
  290. if([scrollView isEqual:self.userCollectionView]) {
  291. if(scrollView.contentSize.width>SCREEN_WIDTH){
  292. if(scrollView.contentOffset.x>=scrollView.contentSize.width){
  293. [scrollView setContentOffset:CGPointMake(-SCREEN_WIDTH, 0)];
  294. }
  295. }
  296. }
  297. }
  298. - (void)backAction {
  299. [self.navigationController popViewControllerAnimated:YES];
  300. }
  301. -(void)viewWillDisappear:(BOOL)animated {
  302. [super viewWillDisappear:animated];
  303. if (self.usertimer) {
  304. [self.usertimer invalidate];
  305. self.usertimer=nil;
  306. }
  307. }
  308. - (void)jumpToTaobaoWithUrl:(NSString *)url parentVc:(UIViewController *)parentVc{
  309. id<AlibcTradePage> page = [AlibcTradePageFactory page:url];
  310. //淘客信息
  311. AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
  312. taoKeParams.pid= ALTK_PID;
  313. //打开方式
  314. AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
  315. showParam.openType = AlibcOpenTypeNative;
  316. [[AlibcTradeSDK sharedInstance].tradeService show:parentVc page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
  317. } tradeProcessFailedCallback:^(NSError * _Nullable error) {
  318. }];
  319. }
  320. /*
  321. #pragma mark - Navigation
  322. // In a storyboard-based application, you will often want to do a little preparation before navigation
  323. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  324. // Get the new view controller using [segue destinationViewController].
  325. // Pass the selected object to the new view controller.
  326. }
  327. */
  328. @end