口袋优选

KBShareImgPopView.m 7.1KB

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