猎豆优选

LDShareImgPopView.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // LDShareImgPopView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/10.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDShareImgPopView.h"
  9. #import <WXApi.h>
  10. #import "HYUMShareManager.h"
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12. #import "LDShareManager.h"
  13. #import <AssetsLibrary/AssetsLibrary.h>
  14. #import <Photos/Photos.h>
  15. @interface LDShareImgPopView (){
  16. UIImage *shareImg;
  17. }
  18. @property (nonatomic, strong) LDGoodDetailModel *goodModel;
  19. @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
  20. @property (nonatomic, strong) LDShareGoodsModel *model;
  21. @end
  22. @implementation LDShareImgPopView
  23. - (instancetype)initWithFrame:(CGRect)frame goodModel:(LDGoodDetailModel *)goodModel{
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. self.goodModel = goodModel;
  27. [self initSubViews];
  28. [self loadShareData];
  29. }
  30. return self;
  31. }
  32. - (void)initSubViews {
  33. UIView *bg = [[UIView alloc] initWithFrame:self.bounds];
  34. bg.backgroundColor = [UIColor clearColor];
  35. [self addSubview:bg];
  36. UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
  37. toolbar.barStyle = UIBarStyleBlack;
  38. [bg addSubview:toolbar];
  39. self.shareGoodView = [[LDPopShareGoodView alloc] initWithFrame:CGRectMake(0, Fitsize(30), Fitsize(254), Fitsize(455))];
  40. if (iPhoneX) {
  41. self.shareGoodView.y += Fitsize(100);
  42. }
  43. self.shareGoodView.centerX = self.width/2;
  44. self.shareGoodView.backgroundColor = [UIColor whiteColor];
  45. __weak typeof(self) weakSelf = self;
  46. self.shareGoodView.imgSuccBlock = ^{
  47. UIImage *resultImg = [weakSelf.shareGoodView changeToImage];
  48. shareImg = resultImg;
  49. if (weakSelf.imageReadBlock) {
  50. weakSelf.imageReadBlock(resultImg);
  51. }
  52. };
  53. [bg addSubview:self.shareGoodView];
  54. [self.shareGoodView addSubview:self.indicatorView];
  55. [self.indicatorView startAnimating];
  56. NSArray *titles = @[@"分享朋友圈",@"分享给好友",@"复制淘口令",@"保存图片"];
  57. NSArray *icons = @[@"wx_section",@"wx_wechat",@"copy_code",@"save_img"];
  58. CGFloat margin = Fitsize(30);
  59. CGFloat width = (self.width-Fitsize(60))/4;
  60. CGFloat height = width;
  61. for (int i = 0; i < titles.count; i++) {
  62. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin+i*width, self.shareGoodView.bottom+Fitsize(20), width, height)];
  63. [button setTitle:titles[i] forState:UIControlStateNormal];
  64. button.titleLabel.font = [UIFont systemFontOfSize:Fitsize(12)];
  65. [button setImage:[UIImage imageNamed:icons[i]] forState:UIControlStateNormal];
  66. [button setButtonImageTitleStyle:ButtonImageTitleStyleTop padding:10];
  67. button.tag = 1000+i;
  68. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  69. [bg addSubview:button];
  70. if (i<2 && ![WXApi isWXAppInstalled]) {
  71. button.hidden = YES;
  72. }
  73. }
  74. UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-80, 40, 40)];
  75. [closeBtn setImage:[UIImage imageNamed:@"close_share"] forState:UIControlStateNormal];
  76. closeBtn.centerX = self.width/2;
  77. [closeBtn addTarget:self action:@selector(clsoeAction) forControlEvents:UIControlEventTouchUpInside];
  78. [bg addSubview:closeBtn];
  79. }
  80. - (void)loadShareData {
  81. NSString *url=[NSString stringWithFormat:@"%@/api/v2/adzoneCreate/shareCommonGoods",BaseURL];
  82. NSString *coupon_start_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.start_time : @"";
  83. NSString *coupon_end_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.end_time : @"";
  84. if (!coupon_start_time) coupon_start_time = @"";
  85. if (!coupon_end_time) coupon_end_time = @"";
  86. NSDictionary *dic=@{
  87. @"goods_id":self.goodModel.goods_id,
  88. @"is_coupon":self.goodModel.is_coupon,
  89. @"coupon_price":self.goodModel.coupon_price,
  90. @"price":self.goodModel.price,
  91. @"discount_price":self.goodModel.discount_price,
  92. @"commission_rate":self.goodModel.commission_rate,
  93. @"coupon_end_time":coupon_end_time,
  94. @"coupon_start_time":coupon_start_time
  95. };
  96. [LDHttp post:url params:dic success:^(id json) {
  97. LDShareGoodsTempModel *tmpModel=[LDShareGoodsTempModel yy_modelWithJSON:json[@"data"]];
  98. LDShareGoodsModel *model=[LDShareGoodsModel createShareGoodsModelByShareGoodsTempModel:tmpModel];
  99. model.commissionPrice=self.goodModel.commission_price;//佣金价格
  100. model.commission_rate=self.goodModel.commission_rate;
  101. model.discount_price=self.goodModel.discount_price;
  102. model.userinfo = tmpModel.userinfo;
  103. self.model = model;
  104. LDShareInfoModel *infoM =[LDShareInfoModel createShareGoodsModelByShareGoodsTempModel:tmpModel];
  105. self.shareGoodView.model = infoM;
  106. [self.indicatorView stopAnimating];
  107. } failure:^(NSError *error) {
  108. }];
  109. }
  110. - (UIActivityIndicatorView *)indicatorView {
  111. if (!_indicatorView) {
  112. _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
  113. _indicatorView.frame= CGRectMake(0, 0, 50, 50);
  114. _indicatorView.color = [UIColor homeRedColor];
  115. _indicatorView.center = CGPointMake(self.shareGoodView.width/2, self.shareGoodView.height/2);
  116. _indicatorView.hidesWhenStopped = YES;
  117. }
  118. return _indicatorView;
  119. }
  120. - (void)buttonAction:(UIButton *)sender {
  121. switch (sender.tag-1000) {
  122. case 0:
  123. //分享到朋友圈
  124. [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine];
  125. [MobClick event:goodDetailShare label:@"朋友圈"];
  126. break;
  127. case 1:
  128. //分享到微信
  129. [self shareImgToWeChat:UMSocialPlatformType_WechatSession];
  130. [MobClick event:goodDetailShare label:@"微信好友"];
  131. break;
  132. case 2:
  133. //复制淘口令
  134. [self copyCode];
  135. break;
  136. case 3:
  137. //保存图片
  138. [self saveImage];
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. - (void)shareImgToWeChat:(UMSocialPlatformType)platformType {
  145. if (shareImg) {
  146. [LDShareManager shareImageWithplatformType:platformType withImg:shareImg complete:^(id result, NSError *error) {
  147. if (self.shareResult) {
  148. self.shareResult(result, error);
  149. }
  150. }];
  151. }else {
  152. [MBProgressHUD showError:@"图片生成中"];
  153. }
  154. }
  155. - (void)copyCode {
  156. if (self.model.infoStr.length > 0) {
  157. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  158. pasteboard.string =[NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥%@¥\n打开【手机淘宝】即可查看",self.model.title,self.model.originalPrice,self.model.ticketAfterPrice,self.model.infoStr];
  159. [SVProgressHUD showSuccessWithStatus:@"复制成功"];
  160. }else {
  161. [SVProgressHUD showErrorWithStatus:@"复制失败"];
  162. }
  163. }
  164. - (void)saveImage {
  165. if (shareImg) {
  166. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  167. [lib writeImageToSavedPhotosAlbum:shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  168. if (error) {
  169. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  170. [alert show];
  171. }else {
  172. [MBProgressHUD showMessage:@"保存成功"];
  173. }
  174. lib = nil;
  175. }];
  176. }else {
  177. [MBProgressHUD showError:@"图片生成中"];
  178. }
  179. }
  180. - (void)clsoeAction {
  181. if (self.closeAction) {
  182. self.closeAction();
  183. }
  184. }
  185. @end