天天省钱快报

KBShareImgPopView.m 7.1KB

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