猎豆优选

LDShareGoodViewController.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // LDShareGoodViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDShareGoodViewController.h"
  9. #import "CCCopyLabel.h"
  10. #import "LDImageContainerView.h"
  11. #import "LDShareImgPopView.h"
  12. #import "LDShareImageCellModel.h"
  13. #import "LDBottomShareView.h"
  14. #import "LDShareDesView.h"
  15. #import "LDPopShareGoodView.h"
  16. #import "LDShareManager.h"
  17. #import <AssetsLibrary/AssetsLibrary.h>
  18. #import <AssetsLibrary/AssetsLibrary.h>
  19. #import <Photos/Photos.h>
  20. #import "LDShareTimeLinePopView.h"
  21. #import "CCAlertShowView.h"
  22. @interface LDShareGoodViewController ()<BottomShareViewDelegate>
  23. {
  24. UIImage *_shareImg;//海报图片
  25. }
  26. @property (nonatomic, strong) NSMutableArray *imageArray;
  27. @property (nonatomic, strong) UIScrollView *scrollView;
  28. @property (nonatomic, strong) UIView *containerView;
  29. @property (nonatomic, strong) CCCopyLabel *contentLabel;
  30. @property (nonatomic, strong) UIView *line;
  31. @property (nonatomic, strong) LDImageContainerView *imgContainer;
  32. //@property (nonatomic, strong) UIButton *showCommissionBtn;
  33. @property (nonatomic, strong) LDBottomShareView *bottomShareView;
  34. @property (nonatomic, strong) LDShareDesView *shareDesView;
  35. @property (nonatomic, strong) LDPopShareGoodView *shareGoodView;
  36. @end
  37. @implementation LDShareGoodViewController
  38. - (void)viewWillDisappear:(BOOL)animated {
  39. [super viewWillDisappear:animated];
  40. [SVProgressHUD dismiss];
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. [self configNavigationBar];
  45. [self initSubViews];
  46. [self requestData];
  47. }
  48. - (void)configNavigationBar {
  49. [self.navigationBar setNavTitle:@"分享商品"];
  50. self.navigationBar.backgroundColor = [UIColor changeColor];
  51. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  52. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  53. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  54. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  55. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  56. }
  57. - (void)backAction {
  58. [self.navigationController popViewControllerAnimated:YES];
  59. }
  60. - (void)requestData {
  61. for (NSString *imgUrl in self.goodModel.small_images) {
  62. LDShareImageCellModel *model = [[LDShareImageCellModel alloc] init];
  63. model.imgUrl = imgUrl;
  64. [self.imageArray addObject:model];
  65. }
  66. LDShareImageCellModel *firstModel = [[LDShareImageCellModel alloc] init];
  67. firstModel.isSelected = YES;
  68. [self.imageArray insertObject:firstModel atIndex:0];
  69. self.shareGoodView = [[LDPopShareGoodView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, Fitsize(254), Fitsize(455))];
  70. self.shareGoodView.backgroundColor = [UIColor whiteColor];
  71. [self.view addSubview:self.shareGoodView];
  72. __weak typeof(self) weakSelf = self;
  73. self.shareGoodView.imgSuccBlock = ^{
  74. UIImage *resultImg = [weakSelf.shareGoodView changeToImage];
  75. _shareImg = resultImg;
  76. firstModel.shareImg = resultImg;
  77. [weakSelf.imgContainer reloadWithDataArray:weakSelf.imageArray];
  78. };
  79. [self.imgContainer reloadWithDataArray:self.imageArray];
  80. self.shareGoodView.model = self.goodModel;
  81. }
  82. - (void)initSubViews {
  83. [self.view addSubview:self.scrollView];
  84. [self.scrollView addSubview:self.containerView];
  85. [self.containerView addSubview:self.contentLabel];
  86. [self.containerView addSubview:self.line];
  87. [self.containerView addSubview:self.imgContainer];
  88. // [self.containerView addSubview:self.showCommissionBtn];
  89. [self.view addSubview:self.shareDesView];
  90. [self.view addSubview:self.bottomShareView];
  91. [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.top.mas_equalTo(NavBarHeight);
  93. make.left.mas_equalTo(0);
  94. make.width.mas_equalTo(SCREEN_WIDTH);
  95. make.height.mas_equalTo(SCREEN_HEIGHT-NavBarHeight);
  96. }];
  97. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.edges.equalTo(self.scrollView);
  99. make.width.equalTo(self.scrollView);
  100. }];
  101. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.left.top.mas_equalTo(15);
  103. make.right.mas_equalTo(-15);
  104. }];
  105. [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
  106. make.left.mas_equalTo(15);
  107. make.right.mas_equalTo(-15);
  108. make.height.mas_equalTo(1);
  109. make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(15);
  110. }];
  111. UIButton *tipeBtn =[[UIButton alloc]init];
  112. [tipeBtn setTitle:@"好友扫描二维码后将成为你的潜在好友" forState:UIControlStateNormal];
  113. [tipeBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal];
  114. [tipeBtn setImage:[UIImage imageNamed:@"share_tip"] forState:UIControlStateNormal];
  115. tipeBtn.titleLabel.font=[UIFont systemFontOfSize:14];
  116. [tipeBtn setButtonStyle:WSLButtonStyleImageLeft spacing:5];
  117. [self.containerView addSubview:tipeBtn];
  118. [self.imgContainer mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.left.right.mas_equalTo(0);
  120. make.top.mas_equalTo(self.line.mas_bottom).mas_offset(10);
  121. make.height.mas_equalTo(150);
  122. }];
  123. // [self.showCommissionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  124. // make.left.mas_equalTo(15);
  125. // make.top.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(20);
  126. // make.width.mas_equalTo(200);
  127. // make.height.mas_equalTo(30);
  128. // }];
  129. [self.bottomShareView mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.left.mas_equalTo(0);
  131. make.bottom.mas_equalTo(-BottomMargin);
  132. make.width.mas_equalTo(SCREEN_WIDTH);
  133. make.height.mas_equalTo(SCREEN_WIDTH/4);
  134. }];
  135. [self.shareDesView mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.left.mas_equalTo(0);
  137. make.width.mas_equalTo(SCREEN_WIDTH);
  138. make.bottom.mas_equalTo(self.bottomShareView.mas_top);
  139. make.height.mas_equalTo(40);
  140. }];
  141. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  142. make.bottom.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(160);// 这里放最后一个view的底部
  143. }];
  144. [tipeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  145. make.top.mas_equalTo(self.imgContainer.mas_bottom).offset(25);
  146. make.left.mas_equalTo(15);
  147. }];
  148. }
  149. - (void)showSelectAction:(UIButton *)sender {
  150. sender.selected = !sender.selected;
  151. if (sender.selected) {
  152. NSString *text = self.goodModel.tkl_content;
  153. self.contentLabel.text = text;
  154. }else {
  155. NSString *text =self.goodModel.tkl_content;
  156. self.contentLabel.text = text;
  157. }
  158. }
  159. #pragma mark ----- bottomShareViewDelegate -----
  160. - (void)BottomShareViewDidSelectedIndex:(NSInteger)index {
  161. switch (index) {
  162. case 0:
  163. //分享到朋友圈
  164. [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine];
  165. [MobClick event:NewGoodDetailCircleShare label:@"朋友圈"];
  166. break;
  167. case 1:
  168. //分享到微信
  169. [self shareImgToWeChat:UMSocialPlatformType_WechatSession];
  170. [MobClick event:NewGoodDetailFriendShare label:@"微信好友"];
  171. break;
  172. case 2:
  173. //复制淘口令
  174. [self copyCode];
  175. break;
  176. case 3:
  177. //保存图片
  178. [self saveImage:nil];
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. /**
  185. 分享
  186. */
  187. - (void)shareImgToWeChat:(UMSocialPlatformType)platformType {
  188. switch (platformType) {
  189. case UMSocialPlatformType_WechatSession:
  190. [self shareToWechatSession];
  191. break;
  192. case UMSocialPlatformType_WechatTimeLine:
  193. [self shareToWechatTimeLine];
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. /**
  200. 分享到朋友圈
  201. */
  202. - (void)shareToWechatTimeLine {
  203. //获取选中的图片
  204. NSMutableArray *saveArray = [NSMutableArray array];
  205. for (LDShareImageCellModel *model in self.imageArray) {
  206. if (model.isSelected) {
  207. [saveArray addObject:model];
  208. }
  209. }
  210. if (saveArray.count == 0) {
  211. [XHToast showCenterWithText:@"您还没有选择图片"];
  212. return;
  213. }
  214. if (saveArray.count == 1) {
  215. LDShareImageCellModel *model = saveArray.firstObject;
  216. if (model.imgUrl) {
  217. [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withImg:model.imgUrl complete:nil];
  218. }else {
  219. if (_shareImg) {
  220. [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withImg:_shareImg complete:nil];
  221. }else {
  222. [XHToast showCenterWithText:@"图片加载中"];
  223. }
  224. }
  225. }else {
  226. LDShareTimeLinePopView *popView = [[LDShareTimeLinePopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(325), Fitsize(240))];
  227. CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:NO];
  228. popView.cancelBlock = ^{
  229. [showView dismiss];
  230. };
  231. popView.openBlock = ^{
  232. [self saveImage:^{
  233. NSURL *url = [NSURL URLWithString:@"weixin://"];
  234. [[UIApplication sharedApplication] openURL:url];
  235. }];
  236. };
  237. }
  238. }
  239. /**
  240. 分享到好友
  241. */
  242. - (void)shareToWechatSession {
  243. if (!_shareImg) {
  244. [XHToast showCenterWithText:@"图片加载中"];
  245. return;
  246. }
  247. if (self.shareResult) {
  248. self.shareResult();
  249. }
  250. //获取选中的图片
  251. NSMutableArray *saveArray = [NSMutableArray array];
  252. for (LDShareImageCellModel *model in self.imageArray) {
  253. if (model.isSelected) {
  254. [saveArray addObject:model];
  255. }
  256. }
  257. if (saveArray.count == 0) {
  258. [XHToast showCenterWithText:@"您还没有选择图片"];
  259. return;
  260. }
  261. if(saveArray.count == 1)
  262. {
  263. LDShareImageCellModel *model = saveArray.firstObject;
  264. if (model.imgUrl) {
  265. [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatSession withImg:model.imgUrl complete:nil];
  266. }else {
  267. if (_shareImg) {
  268. [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatSession withImg:_shareImg complete:nil];
  269. }else {
  270. [XHToast showCenterWithText:@"图片加载中"];
  271. }
  272. }
  273. }else{
  274. [LoadingView show];
  275. NSMutableArray *activityItems = [NSMutableArray array];
  276. for (LDShareImageCellModel *model in saveArray) {
  277. if (model.imgUrl) {
  278. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  279. if (image) {
  280. [activityItems addObject:image];
  281. if (activityItems.count == saveArray.count) {
  282. [self showActivityViewController:activityItems];
  283. }
  284. }
  285. }];
  286. }else {
  287. [activityItems addObject:_shareImg];
  288. if (activityItems.count == saveArray.count) {
  289. [self showActivityViewController:activityItems];
  290. }
  291. }
  292. }
  293. }
  294. }
  295. - (void)showActivityViewController:(NSMutableArray *)activityItems {
  296. NSArray *imgArr = activityItems.mutableCopy;
  297. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:imgArr applicationActivities:nil];
  298. activityVC.modalPresentationStyle = UIModalPresentationFullScreen;
  299. [self presentViewController:activityVC animated:TRUE completion:^{
  300. [LoadingView dismiss];
  301. }];
  302. //回调
  303. activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  304. if (completed) {
  305. }else{
  306. }
  307. };
  308. }
  309. /**
  310. 复制邀请码
  311. */
  312. - (void)copyCode {
  313. if (self.goodModel.tkl && self.contentLabel.text) {
  314. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  315. pasteboard.string = self.contentLabel.text;
  316. [XHToast showCenterWithText:@"复制成功"];
  317. }else {
  318. [XHToast showCenterWithText:@"复制失败"];
  319. }
  320. }
  321. - (void)saveImage:(void(^)(void))complete {
  322. if (_shareImg) {
  323. __block NSInteger count = 0;
  324. NSMutableArray *saveArray = [NSMutableArray array];
  325. [self.imageArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  326. LDShareImageCellModel *model = obj;
  327. if (model.isSelected) {
  328. [saveArray addObject:model];
  329. }
  330. }];
  331. if (saveArray.count == 0) {
  332. [XHToast showCenterWithText:@"您还没有选择图片"];
  333. return;
  334. }
  335. [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
  336. for (int i = 0; i < saveArray.count; i++) {
  337. LDShareImageCellModel *model = saveArray[i];
  338. if (model.shareImg) {
  339. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  340. [lib writeImageToSavedPhotosAlbum:model.shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  341. if (error) {
  342. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  343. [alert show];
  344. }else {
  345. count++;
  346. [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
  347. if (count == saveArray.count) {
  348. [SVProgressHUD dismiss];
  349. [XHToast showCenterWithText:@"保存成功"];
  350. if (complete) {
  351. complete();
  352. }
  353. }
  354. }
  355. lib = nil;
  356. }];
  357. }else {
  358. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  359. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  360. [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  361. if (error) {
  362. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  363. [alert show];
  364. }else {
  365. count++;
  366. [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
  367. if (count == saveArray.count) {
  368. [SVProgressHUD dismiss];
  369. [XHToast showCenterWithText:@"保存成功"];
  370. if (complete) {
  371. complete();
  372. }
  373. }
  374. }
  375. lib = nil;
  376. }];
  377. }];
  378. }
  379. }
  380. }else {
  381. [XHToast showCenterWithText:@"图片生成中"];
  382. }
  383. }
  384. #pragma mark -----
  385. - (CCCopyLabel *)contentLabel {
  386. if (!_contentLabel) {
  387. _contentLabel = [[CCCopyLabel alloc] init];
  388. _contentLabel.font = [UIFont systemFontOfSize:15];
  389. _contentLabel.numberOfLines = 0;
  390. NSString *text = self.goodModel.tkl_content;
  391. _contentLabel.text = text;
  392. }
  393. return _contentLabel;
  394. }
  395. - (UIView *)line {
  396. if (!_line) {
  397. _line = [[UIView alloc] init];
  398. _line.backgroundColor = [UIColor YHColorWithHex:0xE5E4E6];
  399. }
  400. return _line;
  401. }
  402. - (UIScrollView *)scrollView {
  403. if (!_scrollView) {
  404. _scrollView = [[UIScrollView alloc] init];
  405. }
  406. return _scrollView;
  407. }
  408. - (UIView *)containerView {
  409. if (!_containerView) {
  410. _containerView = [UIView new];
  411. }
  412. return _containerView;
  413. }
  414. - (LDImageContainerView *)imgContainer {
  415. if (!_imgContainer) {
  416. _imgContainer = [LDImageContainerView new];
  417. }
  418. return _imgContainer;
  419. }
  420. //- (UIButton *)showCommissionBtn {
  421. // if (!_showCommissionBtn) {
  422. // _showCommissionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  423. // [_showCommissionBtn setTitle:@"显示猎豆优选收益" forState:UIControlStateNormal];
  424. // _showCommissionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  425. // [_showCommissionBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal];
  426. // [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_n"] forState:UIControlStateNormal];
  427. // [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_s"] forState:UIControlStateSelected];
  428. // _showCommissionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  429. // [_showCommissionBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:4];
  430. // [_showCommissionBtn addTarget:self action:@selector(showSelectAction:) forControlEvents:UIControlEventTouchUpInside];
  431. // }
  432. // return _showCommissionBtn;
  433. //}
  434. - (NSMutableArray *)imageArray {
  435. if (!_imageArray) {
  436. _imageArray = [NSMutableArray array];
  437. }
  438. return _imageArray;
  439. }
  440. - (LDBottomShareView *)bottomShareView {
  441. if (!_bottomShareView) {
  442. _bottomShareView = [[LDBottomShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/4)];
  443. _bottomShareView.delegate = self;
  444. }
  445. return _bottomShareView;
  446. }
  447. - (LDShareDesView *)shareDesView {
  448. if (!_shareDesView) {
  449. _shareDesView = [[LDShareDesView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) commissPrice:self.goodModel.self_commission];
  450. }
  451. return _shareDesView;
  452. }
  453. @end