猎豆优选

LDTimeLineViewController.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //
  2. // LDTimeLineViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/27.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDTimeLineViewController.h"
  9. #import "LDTimeLineTableCell.h"
  10. #import "UIView+SDAutoLayout.h"
  11. #import "UITableView+SDAutoTableViewCellHeight.h"
  12. #import "LDShareGoodViewController.h"
  13. #import "LDTimeLineChannelModel.h"
  14. #import "LDTimeLineTopView.h"
  15. #import "LDShareRequestViewModel.h"
  16. #import "LDShareRequestViewModel.h"
  17. #import "LDLoginViewController.h"
  18. #import "LDTimeLineRecomCell.h"
  19. #import "LDTimeLineShareView.h"
  20. #import "LDShareManager.h"
  21. #import <AssetsLibrary/AssetsLibrary.h>
  22. #import <AssetsLibrary/AssetsLibrary.h>
  23. #import <Photos/Photos.h>
  24. #import "LDTimeLineSeconeListVc.h"
  25. #import <AlibabaAuthSDK/ALBBSDK.h>
  26. #import <AlibabaAuthSDK/ALBBSession.h>
  27. #import "LDTaobaoWebAuthorController.h"
  28. @interface LDTimeLineViewController ()<UITableViewDelegate,UITableViewDataSource,TimeLineShareViewDelegate,LDTimeLineTopViewDelegate>
  29. {
  30. NSInteger _page;
  31. ActivityIndicatorView *_indicatorView;
  32. }
  33. @property (nonatomic, strong) UITableView *tableView;
  34. @property (nonatomic, strong) NSMutableArray *dataArr;
  35. @property (nonatomic, assign) NSInteger page;
  36. @property (nonatomic, strong) LDTimeLineTopView *topHeader;
  37. @end
  38. @implementation LDTimeLineViewController
  39. - (void)viewWillDisappear:(BOOL)animated {
  40. [super viewWillDisappear:animated];
  41. [LoadingView dismiss];
  42. }
  43. - (void)viewDidAppear:(BOOL)animated {
  44. [super viewDidAppear:animated];
  45. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  46. }
  47. -(void)viewWillAppear:(BOOL)animated
  48. {
  49. [super viewWillAppear:animated];
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. [self configTableView];
  54. [self configNavigationBar];
  55. [self requestData];
  56. [self addRefreshNotifacation];
  57. }
  58. - (void)configTableView {
  59. _page = 1;
  60. [self.view addSubview:self.tableView];
  61. if (@available(iOS 11.0, *)) {
  62. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  63. } else {
  64. self.automaticallyAdjustsScrollViewInsets = NO;
  65. }
  66. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  67. _indicatorView = indicatorView;
  68. }
  69. - (void)configNavigationBar{
  70. [self.navigationBar setNavTitle:@"优选圈"];
  71. self.view.backgroundColor = [UIColor whiteColor];
  72. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  73. self.navigationBar.backgroundColor = [UIColor changeColor];
  74. }
  75. - (void)addRefreshNotifacation {
  76. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ChangeSex object:nil];
  77. }
  78. - (void)refreshData {
  79. [self.tableView.mj_header beginRefreshing];
  80. }
  81. - (void)requestData {
  82. [self loadTimelineData];
  83. [self loadChannelHeaderData];
  84. }
  85. - (void)loadTimelineData {
  86. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanGoodsList",BaseURL];
  87. NSDictionary *para = @{@"page":@(_page)};
  88. [LDHttp post:url params:para success:^(id json) {
  89. if ([self.tableView.mj_header isRefreshing]) {
  90. [self.dataArr removeAllObjects];
  91. }
  92. NSArray *list = [NSArray yy_modelArrayWithClass:[LDTimeLineModel class] json:json];
  93. [self.dataArr addObjectsFromArray:list];
  94. if (list.count == 0) {
  95. [self.tableView.mj_footer endRefreshingWithNoMoreData];
  96. }else {
  97. [self.tableView.mj_footer endRefreshing];
  98. }
  99. [self.tableView.mj_header endRefreshing];
  100. [self.tableView reloadData];
  101. [_indicatorView stopAnimating];
  102. } failure:^(NSError *error) {
  103. [self.tableView.mj_header endRefreshing];
  104. [self.tableView.mj_footer endRefreshing];
  105. [_indicatorView stopAnimating];
  106. }];
  107. }
  108. - (void)loadChannelHeaderData {
  109. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/labelList",BaseURL];
  110. [LDHttp post:url params:nil success:^(id json) {
  111. NSArray *list = [NSArray yy_modelArrayWithClass:[LDTimeLineChannelModel class] json:json];
  112. if (list.count ==0) {
  113. self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
  114. }else{
  115. self.tableView.tableHeaderView = self.topHeader;
  116. [self.topHeader setData:list];
  117. }
  118. } failure:^(NSError *error) {
  119. }];
  120. }
  121. #pragma ----private
  122. - (void)shareWithModel:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{
  123. if (![AccountTool isLogin]) {
  124. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  125. login.modalPresentationStyle = UIModalPresentationFullScreen;
  126. [self presentViewController:login animated:YES completion:nil];
  127. return;
  128. }
  129. NSString *url = [NSString stringWithFormat:@"%@/api/v2/relationAuth/isAuthOrNo",BaseURL];
  130. [LDHttp post:url params:nil success:^(id json) {
  131. NSNumber *flag = json[@"flag"];
  132. if (flag.boolValue) {
  133. //已授权 直接分享
  134. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time
  135. coupon_id:model.coupon_id];
  136. [LoadingView show];
  137. [LDShareRequestViewModel requestGoodDetailParamGoods_id:requestModel success:^(LDShareInfoModel * _Nonnull infoModel, NSDictionary * _Nonnull dic) {
  138. [LoadingView dismiss];
  139. LDShareGoodViewController *shareVc = [[LDShareGoodViewController alloc] init];
  140. shareVc.goodModel = infoModel;
  141. shareVc.shareResult = ^{
  142. [self shareRequest:model indexPath:indexPath];
  143. };
  144. [self.navigationController pushViewController:shareVc animated:YES];
  145. } failure:^(NSError * _Nonnull error) {
  146. [LoadingView dismiss];
  147. }];
  148. }else {
  149. //未授权 去授权
  150. [LDTaobaoAuthorTool taobaoAuthorManager:json[@"auth_url"] type:@"1" currentVc:self];
  151. }
  152. } failure:^(NSError *error) {
  153. }];
  154. }
  155. - (void)shareRequest:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath {
  156. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/youXuanShareUser",BaseURL];
  157. NSDictionary *para = @{@"goods_id":model.goods_id,@"type":model.type};
  158. [LDHttp post:url params:para success:^(id json) {
  159. NSNumber *flag = json[@"flag"];
  160. if (flag.boolValue) {
  161. NSMutableArray *arr = [NSMutableArray array];
  162. [arr addObjectsFromArray:model.shareUser];
  163. NSString *nick = [AccountTool account].nickname;
  164. if (nick.length == 0) {
  165. nick = @"我";
  166. }
  167. if ([arr containsObject:nick]) {
  168. [arr removeObject:nick];
  169. }
  170. [arr addObject:nick];
  171. model.shareUser = arr;
  172. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  173. }
  174. } failure:^(NSError *error) {
  175. }];
  176. }
  177. - (void)showShareView:(LDTimeLineModel *)model indexPath:(NSIndexPath *)indexPath{
  178. LDTimeLineShareView *shareView = [[LDTimeLineShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  179. shareView.delegate = self;
  180. shareView.model = model;
  181. shareView.indexPath = indexPath;
  182. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  183. [shareView showInView:window];
  184. }
  185. #pragma mark --- timelineDelegate ---
  186. - (void)timelineShareViewDidSeledtedIndex:(NSInteger)index model:(nonnull LDTimeLineModel *)model indexPath:(nonnull NSIndexPath *)indexPath{
  187. switch (index) {
  188. case 0:
  189. [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine model:model indexPath:indexPath];
  190. break;
  191. case 1:
  192. [self shareImgToWeChat:UMSocialPlatformType_WechatSession model:model indexPath:indexPath];
  193. break;
  194. case 2:
  195. [self saveImage:model];
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. /**
  202. 分享
  203. */
  204. - (void)shareImgToWeChat:(UMSocialPlatformType)platformType model:(LDTimeLineModel *)model indexPath:(NSIndexPath *)inexPath{
  205. if (platformType == UMSocialPlatformType_WechatTimeLine) {
  206. [MobClick event:EveryDayMorningCircleShare];
  207. }else {
  208. [MobClick event:EveryDayMorningFriendShare];
  209. }
  210. [self shareRequest:model indexPath:inexPath];
  211. if (model.img) {
  212. [LDShareManager shareImageWithplatformType:platformType withImg:model.img complete:^(id result, NSError *error) {
  213. }];
  214. }
  215. }
  216. - (void)saveImage:(LDTimeLineModel *)model {
  217. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.img] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  218. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  219. [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  220. if (error) {
  221. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  222. [alert show];
  223. }else {
  224. [XHToast showCenterWithText:@"保存成功"];
  225. }
  226. lib = nil;
  227. }];
  228. }];
  229. }
  230. #pragma mark ============ UITableView Delegate && DataSource ==========
  231. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  232. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  233. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  234. }
  235. }
  236. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  237. return 0.1;
  238. }
  239. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  240. return self.dataArr.count;
  241. }
  242. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  243. LDTimeLineModel *model = self.dataArr[indexPath.row];
  244. switch ([model.type integerValue]) {//列表
  245. case 1:
  246. {
  247. LDTimeLineTableCell *cell = [LDTimeLineTableCell cellWithTableView:tableView];
  248. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  249. cell.shareClickBlock = ^{
  250. [self shareWithModel:model indexPath:indexPath];
  251. [MobClick event:OptimizingCircleShareClick];
  252. };
  253. cell.tapBlock = ^{
  254. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithLDTimeLineModel:model];
  255. LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init];
  256. detail.requestModel = requestModel;
  257. [self.navigationController pushViewController:detail animated:YES];
  258. [MobClick event:OptimizingCircleListClick];
  259. };
  260. cell.clickBtn = ^(LDTimeLineModel * _Nonnull models) {
  261. LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init];
  262. listModel.Id=models.label_id;
  263. listModel.title=models.subscribe_title;
  264. [self clickModel:listModel];
  265. };
  266. cell.model = model;
  267. return cell;
  268. }
  269. break;
  270. case 2:
  271. {
  272. LDTimeLineRecomCell *cell = [LDTimeLineRecomCell cellWithTableView:tableView];
  273. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  274. cell.model = model;
  275. cell.shareClickBlock = ^{
  276. [self showShareView:model indexPath:indexPath];
  277. [MobClick event:OptimizingCircleShareClick label:model.img];
  278. };
  279. return cell;
  280. }
  281. break;
  282. default:
  283. {
  284. LDTimeLineTableCell *cell = [LDTimeLineTableCell cellWithTableView:tableView];
  285. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  286. cell.shareClickBlock = ^{
  287. [self shareWithModel:model indexPath:indexPath];
  288. };
  289. cell.clickBtn = ^(LDTimeLineModel * _Nonnull models) {
  290. LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init];
  291. listModel.Id=models.label_id;
  292. listModel.title=models.subscribe_title;
  293. [self clickModel:listModel];
  294. };
  295. cell.model = model;
  296. return cell;
  297. }
  298. break;
  299. }
  300. }
  301. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  302. LDTimeLineModel *model = self.dataArr[indexPath.row];
  303. switch ([model.type integerValue]) {
  304. case 1:
  305. NSLog(@"%.2f",[self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH]);
  306. return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineTableCell class] contentViewWidth:SCREEN_WIDTH];
  307. break;
  308. case 2:
  309. return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LDTimeLineRecomCell class] contentViewWidth:SCREEN_WIDTH];
  310. break;
  311. default:
  312. return 0;
  313. break;
  314. }
  315. }
  316. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  317. // LDTimeLineModel *models =self.dataArr[indexPath.row];
  318. // LDTimeLineChannelModel*listModel =[[LDTimeLineChannelModel alloc]init];
  319. // listModel.Id=models.label_id;
  320. // listModel.title=models.subscribe_title;
  321. // [self clickModel:listModel];
  322. }
  323. #pragma mark------LDTimeLineTopViewDelegate
  324. -(void)clickModel:(LDTimeLineChannelModel *)model
  325. {
  326. LDTimeLineSeconeListVc *listV =[[LDTimeLineSeconeListVc alloc]init];
  327. listV.model=model;
  328. [self.navigationController pushViewController:listV animated:YES];
  329. }
  330. #pragma mark ------- layzer ---------
  331. - (UITableView *)tableView {
  332. if (!_tableView) {
  333. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight) style:UITableViewStyleGrouped];
  334. _tableView.estimatedSectionHeaderHeight = 0;
  335. _tableView.estimatedSectionFooterHeight = 0;
  336. _tableView.sectionFooterHeight = 0;
  337. _tableView.sectionHeaderHeight = 0;
  338. _tableView.estimatedRowHeight = 0;
  339. _tableView.delegate = self;
  340. _tableView.dataSource = self;
  341. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  342. _tableView.backgroundColor = [UIColor whiteColor];
  343. _tableView.bounces = YES;
  344. _tableView.showsVerticalScrollIndicator = NO;
  345. _tableView.separatorColor = [UIColor lineColor];
  346. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  347. _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  348. self.page = 1;
  349. [_tableView.mj_footer resetNoMoreData];
  350. [self requestData];
  351. }];
  352. MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  353. _page++;
  354. [self loadTimelineData];
  355. }];
  356. if (@available(iOS 11.0, *)) {
  357. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  358. }else {
  359. self.automaticallyAdjustsScrollViewInsets = NO;
  360. }
  361. _tableView.mj_footer = footer;
  362. }
  363. return _tableView;
  364. }
  365. - (NSMutableArray *)dataArr {
  366. if (!_dataArr) {
  367. _dataArr = [NSMutableArray array];
  368. }
  369. return _dataArr;
  370. }
  371. - (LDTimeLineTopView *)topHeader {
  372. if (!_topHeader) {
  373. _topHeader = [[LDTimeLineTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 133)];
  374. _topHeader.delegate=self;
  375. }
  376. return _topHeader;
  377. }
  378. @end