123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- //
- // LDShareImgPopView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/7/10.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDShareImgPopView.h"
- #import <WXApi.h>
- #import "HYUMShareManager.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import "LDShareManager.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import <Photos/Photos.h>
- @interface LDShareImgPopView (){
- UIImage *shareImg;
-
- }
- @property (nonatomic, strong) LDGoodDetailModel *goodModel;
- @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
- @property (nonatomic, strong) LDShareGoodsModel *model;
- @end
- @implementation LDShareImgPopView
- - (instancetype)initWithFrame:(CGRect)frame goodModel:(LDGoodDetailModel *)goodModel{
- self = [super initWithFrame:frame];
- if (self) {
- self.goodModel = goodModel;
- [self initSubViews];
- [self loadShareData];
- }
- return self;
- }
- - (void)initSubViews {
-
- UIView *bg = [[UIView alloc] initWithFrame:self.bounds];
- bg.backgroundColor = [UIColor clearColor];
- [self addSubview:bg];
-
- UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
- toolbar.barStyle = UIBarStyleBlack;
- [bg addSubview:toolbar];
-
- self.shareGoodView = [[LDPopShareGoodView alloc] initWithFrame:CGRectMake(0, Fitsize(30), Fitsize(254), Fitsize(455))];
- if (iPhoneX) {
- self.shareGoodView.y += Fitsize(100);
- }
- self.shareGoodView.centerX = self.width/2;
- self.shareGoodView.backgroundColor = [UIColor whiteColor];
- __weak typeof(self) weakSelf = self;
- self.shareGoodView.imgSuccBlock = ^{
- UIImage *resultImg = [weakSelf.shareGoodView changeToImage];
- shareImg = resultImg;
- if (weakSelf.imageReadBlock) {
- weakSelf.imageReadBlock(resultImg);
- }
- };
- [bg addSubview:self.shareGoodView];
-
- [self.shareGoodView addSubview:self.indicatorView];
- [self.indicatorView startAnimating];
-
- NSArray *titles = @[@"分享朋友圈",@"分享给好友",@"复制淘口令",@"保存图片"];
- NSArray *icons = @[@"wx_section",@"wx_wechat",@"copy_code",@"save_img"];
- CGFloat margin = Fitsize(30);
- CGFloat width = (self.width-Fitsize(60))/4;
- CGFloat height = width;
- for (int i = 0; i < titles.count; i++) {
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin+i*width, self.shareGoodView.bottom+Fitsize(20), width, height)];
- [button setTitle:titles[i] forState:UIControlStateNormal];
- button.titleLabel.font = [UIFont systemFontOfSize:Fitsize(12)];
- [button setImage:[UIImage imageNamed:icons[i]] forState:UIControlStateNormal];
- [button setButtonImageTitleStyle:ButtonImageTitleStyleTop padding:10];
- button.tag = 1000+i;
- [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
- [bg addSubview:button];
- if (i<2 && ![WXApi isWXAppInstalled]) {
- button.hidden = YES;
- }
- }
-
- UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-80, 40, 40)];
- [closeBtn setImage:[UIImage imageNamed:@"close_share"] forState:UIControlStateNormal];
- closeBtn.centerX = self.width/2;
- [closeBtn addTarget:self action:@selector(clsoeAction) forControlEvents:UIControlEventTouchUpInside];
- [bg addSubview:closeBtn];
- }
- - (void)loadShareData {
-
- NSString *url=[NSString stringWithFormat:@"%@/api/v2/adzoneCreate/shareCommonGoods",BaseURL];
- NSString *coupon_start_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.start_time : @"";
- NSString *coupon_end_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.end_time : @"";
- if (!coupon_start_time) coupon_start_time = @"";
- if (!coupon_end_time) coupon_end_time = @"";
- NSDictionary *dic=@{
- @"goods_id":self.goodModel.goods_id,
- @"is_coupon":self.goodModel.is_coupon,
- @"coupon_price":self.goodModel.coupon_price,
- @"price":self.goodModel.price,
- @"discount_price":self.goodModel.discount_price,
- @"commission_rate":self.goodModel.commission_rate,
- @"coupon_end_time":coupon_end_time,
- @"coupon_start_time":coupon_start_time
- };
- [LDHttp post:url params:dic success:^(id json) {
- LDShareGoodsTempModel *tmpModel=[LDShareGoodsTempModel yy_modelWithJSON:json[@"data"]];
- LDShareGoodsModel *model=[LDShareGoodsModel createShareGoodsModelByShareGoodsTempModel:tmpModel];
- model.commissionPrice=self.goodModel.commission_price;//佣金价格
- model.commission_rate=self.goodModel.commission_rate;
- model.discount_price=self.goodModel.discount_price;
- model.userinfo = tmpModel.userinfo;
- self.model = model;
-
- LDShareInfoModel *infoM =[LDShareInfoModel createShareGoodsModelByShareGoodsTempModel:tmpModel];
- self.shareGoodView.model = infoM;
- [self.indicatorView stopAnimating];
- } failure:^(NSError *error) {
-
- }];
-
-
- }
- - (UIActivityIndicatorView *)indicatorView {
- if (!_indicatorView) {
- _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
- _indicatorView.frame= CGRectMake(0, 0, 50, 50);
- _indicatorView.color = [UIColor homeRedColor];
- _indicatorView.center = CGPointMake(self.shareGoodView.width/2, self.shareGoodView.height/2);
- _indicatorView.hidesWhenStopped = YES;
- }
- return _indicatorView;
- }
- - (void)buttonAction:(UIButton *)sender {
- switch (sender.tag-1000) {
- case 0:
- //分享到朋友圈
- [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine];
- [MobClick event:goodDetailShare label:@"朋友圈"];
- break;
- case 1:
- //分享到微信
- [self shareImgToWeChat:UMSocialPlatformType_WechatSession];
- [MobClick event:goodDetailShare label:@"微信好友"];
- break;
- case 2:
- //复制淘口令
- [self copyCode];
- break;
- case 3:
- //保存图片
- [self saveImage];
- break;
- default:
- break;
- }
- }
- - (void)shareImgToWeChat:(UMSocialPlatformType)platformType {
-
- if (shareImg) {
- [LDShareManager shareImageWithplatformType:platformType withImg:shareImg complete:^(id result, NSError *error) {
- if (self.shareResult) {
- self.shareResult(result, error);
- }
- }];
-
- }else {
- [MBProgressHUD showError:@"图片生成中"];
- }
-
-
- }
- - (void)copyCode {
- if (self.model.infoStr.length > 0) {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- pasteboard.string =[NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥%@¥\n打开【手机淘宝】即可查看",self.model.title,self.model.originalPrice,self.model.ticketAfterPrice,self.model.infoStr];
- [SVProgressHUD showSuccessWithStatus:@"复制成功"];
- }else {
- [SVProgressHUD showErrorWithStatus:@"复制失败"];
- }
-
-
- }
- - (void)saveImage {
-
- if (shareImg) {
- __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
- [lib writeImageToSavedPhotosAlbum:shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
- if (error) {
- UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
- [alert show];
- }else {
- [MBProgressHUD showMessage:@"保存成功"];
- }
- lib = nil;
- }];
- }else {
- [MBProgressHUD showError:@"图片生成中"];
- }
-
-
- }
- - (void)clsoeAction {
- if (self.closeAction) {
- self.closeAction();
- }
- }
- @end
|