Sin descripción

FKInviteCodeController.m 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //
  2. // FKInviteCodeController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/3/24.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKInviteCodeController.h"
  9. #import "WebViewController.h"
  10. #import "InviteCodeViewModel.h"
  11. #import "FKInviteUserItem.h"
  12. #import "FKWeixinShareView.h"
  13. #import "FLShareHelper.h"
  14. #import "FLImageHelper.h"
  15. #import "FLControllerHelper.h"
  16. #import "FKInviteEmptyView.h"
  17. #import "FKInviteTableCell.h"
  18. #import "FKRecommendRequest.h"
  19. #import "FKRecommendReform.h"
  20. static NSString *INVITE_TABLE_CELL_IDENTIFY = @"INVITE_TABLE_CELL_IDENTIFY";
  21. #define INVITE_TABLE_CELL_HEIGHT 43
  22. #define INVITE_MSG_REQ 800
  23. @interface FKInviteCodeController () <UITableViewDelegate, UITableViewDataSource, FLNetworkDelegate>
  24. @property (nonatomic, strong) NSString *inviteCode;
  25. @property (nonatomic, strong) NSArray *inviteUserList;
  26. @property (nonatomic, strong) UILabel *codeLabel;
  27. @property (nonatomic, strong) UIButton *inviteButton;
  28. @property (nonatomic, strong) UILabel *stepLabel;
  29. @property (nonatomic, strong) UITableView *tableView;
  30. @property (nonatomic, strong) UIScrollView *scrollView;
  31. @property (nonatomic, strong) MASConstraint *tableHeight;
  32. @property (nonatomic, strong) FKInviteEmptyView *emptyView;
  33. @end
  34. @implementation FKInviteCodeController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.view.backgroundColor = UIColorFromRGB(0xf4f4f4);
  38. [self addAllSubviews];
  39. }
  40. - (void)viewWillAppear:(BOOL)animated{
  41. [super viewWillAppear:animated];
  42. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  43. [self showLoginActionMenu];
  44. return;
  45. }
  46. [self requestInviteMsg];
  47. [self requestInviteCode];
  48. [self configNavItem];
  49. }
  50. - (void)configNavItem{
  51. self.navigationItem.title = @"邀请码";
  52. [self.navigationController setNavigationBarHidden:NO animated:YES];
  53. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"活动规则"
  54. style:UIBarButtonItemStylePlain
  55. target:self
  56. action:@selector(clickRightNavItem)];
  57. }
  58. - (void)requestInviteMsg{
  59. [FKRecommendRequest requestDynamicMsgWithIdentify:INVITE_MSG_REQ deleagate:self];
  60. }
  61. - (void)requestInviteCode {
  62. [self.hudView show:YES];
  63. WeakSelf(weakSelf);
  64. [InviteCodeViewModel requestInviteCodeSuccess:^(MSGHeader *header, id responseObject) {
  65. [weakSelf.hudView hide:NO];
  66. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  67. if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
  68. weakSelf.inviteCode = [InviteCodeViewModel parseCode:responseObject];
  69. weakSelf.inviteUserList = [InviteCodeViewModel parserInviteList:responseObject];
  70. [weakSelf resetContent];
  71. } else {
  72. [weakSelf resetContent];
  73. }
  74. } else {
  75. [FLProgressHUDHelper showText:responseObject
  76. inView:weakSelf.view];
  77. }
  78. } failure:^(MSGHeader *header, NSError *error) {
  79. [weakSelf.hudView hide:NO];
  80. [FLProgressHUDHelper showText:error.localizedDescription
  81. inView:weakSelf.view];
  82. }];
  83. }
  84. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
  85. if (header.code.integerValue == RESPONSE_MSG_NORMAL){
  86. if (identify == INVITE_MSG_REQ){
  87. FKDynamicMessageItem *item = [FKRecommendReform parserDynamicItemWithDict:response];
  88. self.stepLabel.text = [item combinedInviteMsg];
  89. }
  90. }
  91. }
  92. - (void)addAllSubviews{
  93. UIScrollView *scrollView = ({
  94. UIScrollView *scroll = [[UIScrollView alloc]init];
  95. scroll.showsVerticalScrollIndicator = NO;
  96. scroll.showsHorizontalScrollIndicator = NO;
  97. scroll.backgroundColor = [UIColor whiteColor];
  98. scroll;
  99. });
  100. self.scrollView = scrollView;
  101. UIImageView *imageView = ({
  102. UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invite_letter"]];
  103. [imageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  104. imageView;
  105. });
  106. UIImageView *titleImgView = ({
  107. UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invite_title_image"]];
  108. imageView;
  109. });
  110. UILabel *explainLabel = ({
  111. UILabel *label = [[UILabel alloc] init];
  112. label.font = [UIFont systemFontOfSize:15];
  113. label.textColor = UIColorFromRGB(0x333333);
  114. label.text = @"50元红包送好友 自己也得50元";
  115. label;
  116. });
  117. self.inviteButton = ({
  118. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  119. [button addTarget:self action:@selector(clickInviteBtn:) forControlEvents:UIControlEventTouchUpInside];
  120. button;
  121. });
  122. UILabel *upTipLabel = ({
  123. UILabel *label = [[UILabel alloc]init];
  124. label.font = [UIFont boldSystemFontOfSize:15];
  125. label.textColor = UIColorFromRGB(0x333333);
  126. label.text = @"奖励步骤:";
  127. [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  128. label;
  129. });
  130. UILabel *downTipLabel = ({
  131. UILabel *label = [[UILabel alloc]init];
  132. label.font = [UIFont boldSystemFontOfSize:15];
  133. label.textColor = UIColorFromRGB(0x333333);
  134. label.text = @"我的奖励:";
  135. [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  136. label;
  137. });
  138. UILabel *stepLabel = ({
  139. UILabel *label = [[UILabel alloc]init];
  140. label.font = [UIFont systemFontOfSize:14];
  141. label.textColor = UIColorFromRGB(0x333333);
  142. label.numberOfLines = 0;
  143. label.text = @" ";
  144. [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  145. label;
  146. });
  147. self.stepLabel = stepLabel;
  148. [self.view addSubview:scrollView];
  149. [scrollView addSubview:imageView];
  150. [scrollView addSubview:titleImgView];
  151. [scrollView addSubview:explainLabel];
  152. [scrollView addSubview:upTipLabel];
  153. [scrollView addSubview:downTipLabel];
  154. [scrollView addSubview:stepLabel];
  155. [scrollView addSubview:self.tableView];
  156. [scrollView addSubview:self.emptyView];
  157. [scrollView addSubview:self.codeLabel];
  158. [scrollView addSubview:self.inviteButton];
  159. [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  160. make.edges.insets(UIEdgeInsetsZero);
  161. }];
  162. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  163. make.top.equalTo(scrollView).offset(20);
  164. make.centerX.equalTo(scrollView);
  165. }];
  166. [titleImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  167. make.top.equalTo(imageView).offset(35);
  168. make.centerX.equalTo(scrollView);
  169. }];
  170. [explainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  171. make.top.equalTo(titleImgView.mas_bottom).offset(10);
  172. make.centerX.equalTo(scrollView);
  173. }];
  174. [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.top.equalTo(explainLabel.mas_bottom).offset(9);
  176. make.centerX.equalTo(scrollView);
  177. }];
  178. [self.inviteButton mas_makeConstraints:^(MASConstraintMaker *make) {
  179. make.bottom.equalTo(imageView);
  180. make.centerX.equalTo(imageView);
  181. make.size.mas_equalTo(CGSizeMake(260, 45));
  182. }];
  183. [upTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  184. make.top.equalTo(imageView.mas_bottom).offset(30);
  185. make.left.equalTo(scrollView).offset(25);
  186. }];
  187. [stepLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  188. make.top.equalTo(upTipLabel.mas_bottom).offset(20);
  189. make.left.equalTo(scrollView).offset(25);
  190. make.right.equalTo(scrollView).offset(- 25);
  191. make.width.mas_equalTo(UISCREENWIDTH - 50);
  192. }];
  193. [downTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  194. make.left.equalTo(upTipLabel);
  195. make.top.equalTo(stepLabel.mas_bottom).offset(25);
  196. }];
  197. [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
  198. make.top.equalTo(downTipLabel.mas_bottom).offset(15);
  199. make.left.equalTo(downTipLabel);
  200. make.right.equalTo(stepLabel);
  201. make.height.mas_equalTo(120);
  202. }];
  203. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  204. make.top.equalTo(downTipLabel.mas_bottom).offset(15);
  205. make.left.equalTo(downTipLabel);
  206. make.right.equalTo(stepLabel);
  207. self.tableHeight = make.height.mas_equalTo(4 * INVITE_TABLE_CELL_HEIGHT + 10);
  208. make.bottom.equalTo(scrollView).offset(- 20);
  209. }];
  210. }
  211. #pragma mark - tableView datasource
  212. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  213. return self.inviteUserList.count;
  214. }
  215. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  216. FKInviteTableCell *cell = [tableView dequeueReusableCellWithIdentifier:INVITE_TABLE_CELL_IDENTIFY];
  217. FKInviteUserItem *item = [self userItemForIndex:indexPath.row];
  218. if (item){
  219. [cell.imgView.avatarView sd_setImageWithURL:[NSURL URLWithString:item.headPic]];
  220. cell.nameLabel.text = item.nickName;
  221. cell.titleLabel.text = item.inviteReward;
  222. }
  223. return cell;
  224. }
  225. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  226. return INVITE_TABLE_CELL_HEIGHT;
  227. }
  228. #pragma mark - method
  229. - (FKInviteUserItem *)userItemForIndex:(NSInteger)index{
  230. if (index >= 0 && index < self.inviteUserList.count){
  231. return self.inviteUserList[index];
  232. }
  233. return nil;
  234. }
  235. - (void)resetContent{
  236. self.codeLabel.text = [NSString stringWithFormat:@"邀请码:%@", self.inviteCode];
  237. if (self.inviteUserList.count){
  238. self.tableView.hidden = NO;
  239. self.emptyView.hidden = YES;
  240. self.tableHeight.mas_equalTo(INVITE_TABLE_CELL_HEIGHT * self.inviteUserList.count + 10);
  241. [self.scrollView setNeedsLayout];
  242. [self.scrollView layoutIfNeeded];
  243. }else{
  244. self.tableView.hidden = YES;
  245. self.emptyView.hidden = NO;
  246. }
  247. [self.tableView reloadData];
  248. }
  249. //- (NSString *)stringForStep{
  250. // return @"1、发红包链接给好友/告诉好友你的邀请码\n\n2、新人好友从分享链接领取你的红包/使用你的邀请码领取50元红包\n\n3、你将获得总价值50元的国际运费抵扣券(20元、30元各一张)";
  251. //}
  252. #pragma mark - action
  253. - (void)clickRightNavItem{
  254. NSString *url = [NSString stringWithFormat:@"%@/support/invite-code.html", [[FKServerUtil sharedInstance] webServer]];
  255. [self pushWebViewController:url segmentStr:nil];
  256. }
  257. - (void)clickShareBtn:(UIButton *)sender{
  258. [self goToShareForInvite];
  259. }
  260. - (void)clickInviteBtn:(UIButton *)sender{
  261. [self goToShareForInvite];
  262. }
  263. - (void)goToShareForInvite{
  264. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  265. [self showLoginActionMenu];
  266. return;
  267. }
  268. FKWeixinShareView *shareView = [[FKWeixinShareView alloc]init];
  269. WeakSelf(weakSelf);
  270. [shareView showInView:self.navigationController.view actionBlock:^(kShareViewActionType type) {
  271. [weakSelf inviteForType:type];
  272. }];
  273. }
  274. - (void)inviteForType:(kShareViewActionType)type{
  275. if (type == kShareViewActionTypeNone) return;
  276. NSString *urlTag = @"10";
  277. kBehaviorType behaviorType = kBehaviorDetailShareF;
  278. WeixinScene scene = WeixinSceneFriend;
  279. if (type == kShareViewActionTypeCircle){
  280. urlTag = @"11";
  281. behaviorType = kBehaviorDetailShareC;
  282. scene = WeixinSceneCicle;
  283. }
  284. WeakSelf(weakSelf);
  285. [InviteCodeViewModel requestShareURL:urlTag
  286. success:^(MSGHeader *header, FKInviteShareItem* shareItem)
  287. {
  288. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  289. if ([shareItem isKindOfClass:[FKInviteShareItem class]]){
  290. [FLShareHelper shareToWeixin:shareItem.shareTitle
  291. description:shareItem.shareDesc
  292. thumbImage:[self getShareImg]
  293. webURL:shareItem.shareUrl
  294. scene:scene];
  295. }
  296. } else {
  297. [FLProgressHUDHelper showText:header.msg inView:weakSelf.view];
  298. }
  299. } failure:^(MSGHeader *header, NSError *error) {
  300. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  301. }];
  302. [BehaviorTrackManger requestBehaviorType:behaviorType jsonString:nil];
  303. }
  304. - (UIImage *)getShareImg{
  305. return [UIImage imageNamed:@"shareInvite_img"];
  306. }
  307. - (NSString*)titleForWeixin {
  308. return [NSString stringWithFormat:@"我正在使用剁手帮,快来抢优惠"];
  309. }
  310. - (NSString*)descForWeixin {
  311. return @"剁手帮,海淘免税店扫货神器!帮你买遍全世界!";
  312. }
  313. - (void)shareToWeixinFriend:(id)sender {
  314. WeakSelf(weakSelf);
  315. [InviteCodeViewModel requestShareURL:@"10"
  316. success:^(MSGHeader *header, id responseObject)
  317. {
  318. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  319. [FLShareHelper shareToWeixin:[weakSelf titleForWeixin]
  320. description:[weakSelf descForWeixin]
  321. thumbImage:[FLImageHelper getAppIcon]
  322. webURL:responseObject
  323. scene:WeixinSceneFriend];
  324. } else {
  325. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  326. }
  327. } failure:^(MSGHeader *header, NSError *error) {
  328. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  329. }];
  330. [BehaviorTrackManger requestBehaviorType:kBehaviorInviteShareF jsonString:nil];
  331. }
  332. - (void)shareToWeixinCircle:(id)sender {
  333. WeakSelf(weakSelf);
  334. [InviteCodeViewModel requestShareURL:@"11"
  335. success:^(MSGHeader *header, id responseObject)
  336. {
  337. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  338. [FLShareHelper shareToWeixin:[weakSelf titleForWeixin]
  339. description:nil
  340. thumbImage:[FLImageHelper getAppIcon]
  341. webURL:responseObject
  342. scene:WeixinSceneCicle];
  343. } else {
  344. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  345. }
  346. } failure:^(MSGHeader *header, NSError *error) {
  347. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  348. }];
  349. // 行为统计
  350. [BehaviorTrackManger requestBehaviorType:kBehaviorInviteShareC jsonString:nil];
  351. }
  352. - (void)pushWebViewController:(NSString *)urlString segmentStr:(NSString *)segmentStr{
  353. WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"];
  354. newViewController.url = urlString;
  355. newViewController.urlSegment = segmentStr;
  356. newViewController.hidesBottomBarWhenPushed = YES;
  357. [self.navigationController pushViewController:newViewController animated:YES];
  358. }
  359. #pragma mark - property
  360. - (UILabel *)codeLabel{
  361. if (_codeLabel == nil) {
  362. _codeLabel = [[UILabel alloc]init];
  363. _codeLabel.font = [UIFont boldSystemFontOfSize:15];
  364. _codeLabel.textColor = UIColorFromRGB(0xf55e87);
  365. _codeLabel.text = @" ";
  366. [_codeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  367. }
  368. return _codeLabel;
  369. }
  370. - (FKInviteEmptyView *)emptyView{
  371. if (_emptyView == nil) {
  372. _emptyView = [[FKInviteEmptyView alloc]init];
  373. [_emptyView.shareBtn addTarget:self action:@selector(clickShareBtn:) forControlEvents:UIControlEventTouchUpInside];
  374. _emptyView.hidden = YES;
  375. }
  376. return _emptyView;
  377. }
  378. - (UITableView *)tableView{
  379. if (_tableView == nil) {
  380. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  381. _tableView.delegate = self;
  382. _tableView.dataSource = self;
  383. _tableView.scrollEnabled = NO;
  384. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  385. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  386. _tableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
  387. [_tableView registerClass:[FKInviteTableCell class] forCellReuseIdentifier:INVITE_TABLE_CELL_IDENTIFY];
  388. }
  389. return _tableView;
  390. }
  391. @end