// // FKInviteCodeController.m // FirstLink // // Created by jack on 16/3/24. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKInviteCodeController.h" #import "WebViewController.h" #import "InviteCodeViewModel.h" #import "FKInviteUserItem.h" #import "FKWeixinShareView.h" #import "FLShareHelper.h" #import "FLImageHelper.h" #import "FLControllerHelper.h" #import "FKInviteEmptyView.h" #import "FKInviteTableCell.h" #import "FKRecommendRequest.h" #import "FKRecommendReform.h" static NSString *INVITE_TABLE_CELL_IDENTIFY = @"INVITE_TABLE_CELL_IDENTIFY"; #define INVITE_TABLE_CELL_HEIGHT 43 #define INVITE_MSG_REQ 800 @interface FKInviteCodeController () @property (nonatomic, strong) NSString *inviteCode; @property (nonatomic, strong) NSArray *inviteUserList; @property (nonatomic, strong) UILabel *codeLabel; @property (nonatomic, strong) UIButton *inviteButton; @property (nonatomic, strong) UILabel *stepLabel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) MASConstraint *tableHeight; @property (nonatomic, strong) FKInviteEmptyView *emptyView; @end @implementation FKInviteCodeController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = UIColorFromRGB(0xf4f4f4); [self addAllSubviews]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [self showLoginActionMenu]; return; } [self requestInviteMsg]; [self requestInviteCode]; [self configNavItem]; } - (void)configNavItem{ self.navigationItem.title = @"邀请码"; [self.navigationController setNavigationBarHidden:NO animated:YES]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"活动规则" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightNavItem)]; } - (void)requestInviteMsg{ [FKRecommendRequest requestDynamicMsgWithIdentify:INVITE_MSG_REQ deleagate:self]; } - (void)requestInviteCode { [self.hudView show:YES]; WeakSelf(weakSelf); [InviteCodeViewModel requestInviteCodeSuccess:^(MSGHeader *header, id responseObject) { [weakSelf.hudView hide:NO]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) { weakSelf.inviteCode = [InviteCodeViewModel parseCode:responseObject]; weakSelf.inviteUserList = [InviteCodeViewModel parserInviteList:responseObject]; [weakSelf resetContent]; } else { [weakSelf resetContent]; } } else { [FLProgressHUDHelper showText:responseObject inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [weakSelf.hudView hide:NO]; [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; } - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{ if (header.code.integerValue == RESPONSE_MSG_NORMAL){ if (identify == INVITE_MSG_REQ){ FKDynamicMessageItem *item = [FKRecommendReform parserDynamicItemWithDict:response]; self.stepLabel.text = [item combinedInviteMsg]; } } } - (void)addAllSubviews{ UIScrollView *scrollView = ({ UIScrollView *scroll = [[UIScrollView alloc]init]; scroll.showsVerticalScrollIndicator = NO; scroll.showsHorizontalScrollIndicator = NO; scroll.backgroundColor = [UIColor whiteColor]; scroll; }); self.scrollView = scrollView; UIImageView *imageView = ({ UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invite_letter"]]; [imageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; imageView; }); UIImageView *titleImgView = ({ UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invite_title_image"]]; imageView; }); UILabel *explainLabel = ({ UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:15]; label.textColor = UIColorFromRGB(0x333333); label.text = @"50元红包送好友 自己也得50元"; label; }); self.inviteButton = ({ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(clickInviteBtn:) forControlEvents:UIControlEventTouchUpInside]; button; }); UILabel *upTipLabel = ({ UILabel *label = [[UILabel alloc]init]; label.font = [UIFont boldSystemFontOfSize:15]; label.textColor = UIColorFromRGB(0x333333); label.text = @"奖励步骤:"; [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; label; }); UILabel *downTipLabel = ({ UILabel *label = [[UILabel alloc]init]; label.font = [UIFont boldSystemFontOfSize:15]; label.textColor = UIColorFromRGB(0x333333); label.text = @"我的奖励:"; [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; label; }); UILabel *stepLabel = ({ UILabel *label = [[UILabel alloc]init]; label.font = [UIFont systemFontOfSize:14]; label.textColor = UIColorFromRGB(0x333333); label.numberOfLines = 0; label.text = @" "; [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; label; }); self.stepLabel = stepLabel; [self.view addSubview:scrollView]; [scrollView addSubview:imageView]; [scrollView addSubview:titleImgView]; [scrollView addSubview:explainLabel]; [scrollView addSubview:upTipLabel]; [scrollView addSubview:downTipLabel]; [scrollView addSubview:stepLabel]; [scrollView addSubview:self.tableView]; [scrollView addSubview:self.emptyView]; [scrollView addSubview:self.codeLabel]; [scrollView addSubview:self.inviteButton]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [imageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(scrollView).offset(20); make.centerX.equalTo(scrollView); }]; [titleImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(imageView).offset(35); make.centerX.equalTo(scrollView); }]; [explainLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleImgView.mas_bottom).offset(10); make.centerX.equalTo(scrollView); }]; [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(explainLabel.mas_bottom).offset(9); make.centerX.equalTo(scrollView); }]; [self.inviteButton mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(imageView); make.centerX.equalTo(imageView); make.size.mas_equalTo(CGSizeMake(260, 45)); }]; [upTipLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(imageView.mas_bottom).offset(30); make.left.equalTo(scrollView).offset(25); }]; [stepLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(upTipLabel.mas_bottom).offset(20); make.left.equalTo(scrollView).offset(25); make.right.equalTo(scrollView).offset(- 25); make.width.mas_equalTo(UISCREENWIDTH - 50); }]; [downTipLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(upTipLabel); make.top.equalTo(stepLabel.mas_bottom).offset(25); }]; [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(downTipLabel.mas_bottom).offset(15); make.left.equalTo(downTipLabel); make.right.equalTo(stepLabel); make.height.mas_equalTo(120); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(downTipLabel.mas_bottom).offset(15); make.left.equalTo(downTipLabel); make.right.equalTo(stepLabel); self.tableHeight = make.height.mas_equalTo(4 * INVITE_TABLE_CELL_HEIGHT + 10); make.bottom.equalTo(scrollView).offset(- 20); }]; } #pragma mark - tableView datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.inviteUserList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ FKInviteTableCell *cell = [tableView dequeueReusableCellWithIdentifier:INVITE_TABLE_CELL_IDENTIFY]; FKInviteUserItem *item = [self userItemForIndex:indexPath.row]; if (item){ [cell.imgView.avatarView sd_setImageWithURL:[NSURL URLWithString:item.headPic]]; cell.nameLabel.text = item.nickName; cell.titleLabel.text = item.inviteReward; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return INVITE_TABLE_CELL_HEIGHT; } #pragma mark - method - (FKInviteUserItem *)userItemForIndex:(NSInteger)index{ if (index >= 0 && index < self.inviteUserList.count){ return self.inviteUserList[index]; } return nil; } - (void)resetContent{ self.codeLabel.text = [NSString stringWithFormat:@"邀请码:%@", self.inviteCode]; if (self.inviteUserList.count){ self.tableView.hidden = NO; self.emptyView.hidden = YES; self.tableHeight.mas_equalTo(INVITE_TABLE_CELL_HEIGHT * self.inviteUserList.count + 10); [self.scrollView setNeedsLayout]; [self.scrollView layoutIfNeeded]; }else{ self.tableView.hidden = YES; self.emptyView.hidden = NO; } [self.tableView reloadData]; } //- (NSString *)stringForStep{ // return @"1、发红包链接给好友/告诉好友你的邀请码\n\n2、新人好友从分享链接领取你的红包/使用你的邀请码领取50元红包\n\n3、你将获得总价值50元的国际运费抵扣券(20元、30元各一张)"; //} #pragma mark - action - (void)clickRightNavItem{ NSString *url = [NSString stringWithFormat:@"%@/support/invite-code.html", [[FKServerUtil sharedInstance] webServer]]; [self pushWebViewController:url segmentStr:nil]; } - (void)clickShareBtn:(UIButton *)sender{ [self goToShareForInvite]; } - (void)clickInviteBtn:(UIButton *)sender{ [self goToShareForInvite]; } - (void)goToShareForInvite{ if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [self showLoginActionMenu]; return; } FKWeixinShareView *shareView = [[FKWeixinShareView alloc]init]; WeakSelf(weakSelf); [shareView showInView:self.navigationController.view actionBlock:^(kShareViewActionType type) { [weakSelf inviteForType:type]; }]; } - (void)inviteForType:(kShareViewActionType)type{ if (type == kShareViewActionTypeNone) return; NSString *urlTag = @"10"; kBehaviorType behaviorType = kBehaviorDetailShareF; WeixinScene scene = WeixinSceneFriend; if (type == kShareViewActionTypeCircle){ urlTag = @"11"; behaviorType = kBehaviorDetailShareC; scene = WeixinSceneCicle; } WeakSelf(weakSelf); [InviteCodeViewModel requestShareURL:urlTag success:^(MSGHeader *header, FKInviteShareItem* shareItem) { if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if ([shareItem isKindOfClass:[FKInviteShareItem class]]){ [FLShareHelper shareToWeixin:shareItem.shareTitle description:shareItem.shareDesc thumbImage:[self getShareImg] webURL:shareItem.shareUrl scene:scene]; } } else { [FLProgressHUDHelper showText:header.msg inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; [BehaviorTrackManger requestBehaviorType:behaviorType jsonString:nil]; } - (UIImage *)getShareImg{ return [UIImage imageNamed:@"shareInvite_img"]; } - (NSString*)titleForWeixin { return [NSString stringWithFormat:@"我正在使用剁手帮,快来抢优惠"]; } - (NSString*)descForWeixin { return @"剁手帮,海淘免税店扫货神器!帮你买遍全世界!"; } - (void)shareToWeixinFriend:(id)sender { WeakSelf(weakSelf); [InviteCodeViewModel requestShareURL:@"10" success:^(MSGHeader *header, id responseObject) { if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [FLShareHelper shareToWeixin:[weakSelf titleForWeixin] description:[weakSelf descForWeixin] thumbImage:[FLImageHelper getAppIcon] webURL:responseObject scene:WeixinSceneFriend]; } else { [FLProgressHUDHelper showText:responseObject inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; [BehaviorTrackManger requestBehaviorType:kBehaviorInviteShareF jsonString:nil]; } - (void)shareToWeixinCircle:(id)sender { WeakSelf(weakSelf); [InviteCodeViewModel requestShareURL:@"11" success:^(MSGHeader *header, id responseObject) { if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [FLShareHelper shareToWeixin:[weakSelf titleForWeixin] description:nil thumbImage:[FLImageHelper getAppIcon] webURL:responseObject scene:WeixinSceneCicle]; } else { [FLProgressHUDHelper showText:responseObject inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; // 行为统计 [BehaviorTrackManger requestBehaviorType:kBehaviorInviteShareC jsonString:nil]; } - (void)pushWebViewController:(NSString *)urlString segmentStr:(NSString *)segmentStr{ WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"]; newViewController.url = urlString; newViewController.urlSegment = segmentStr; newViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newViewController animated:YES]; } #pragma mark - property - (UILabel *)codeLabel{ if (_codeLabel == nil) { _codeLabel = [[UILabel alloc]init]; _codeLabel.font = [UIFont boldSystemFontOfSize:15]; _codeLabel.textColor = UIColorFromRGB(0xf55e87); _codeLabel.text = @" "; [_codeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; } return _codeLabel; } - (FKInviteEmptyView *)emptyView{ if (_emptyView == nil) { _emptyView = [[FKInviteEmptyView alloc]init]; [_emptyView.shareBtn addTarget:self action:@selector(clickShareBtn:) forControlEvents:UIControlEventTouchUpInside]; _emptyView.hidden = YES; } return _emptyView; } - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.scrollEnabled = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4); _tableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0); [_tableView registerClass:[FKInviteTableCell class] forCellReuseIdentifier:INVITE_TABLE_CELL_IDENTIFY]; } return _tableView; } @end