// // PBShareView.m // IphoneBIMe // // Created by zjf on 2018/10/26. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBShareView.h" #import "PBShareBtn.h" #import "PBShareCell.h" #import "PBNetworkModel.h" #define ALineCount 4 static NSString *const cellID = @"cellID"; @interface PBShareView() @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) NSMutableArray *btnArrM; @property (nonatomic, assign) NSInteger btnSelectIndex; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArrM; @end @implementation PBShareView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0]; [self addSubview:self.bgView]; self.btnSelectIndex = 2; [self setupUI]; } return self; } - (void)setupUI { UILabel *validityDateL = [UILabel z_labelWithText:@"文件分享有效期" Color:DescColor isBold:NO Font:DescFontSize]; [self.bgView addSubview:validityDateL]; [validityDateL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(16); make.left.equalTo(self.bgView).offset(30); make.height.equalTo(@20); }]; NSArray *arr = @[@"1天", @"7天", @"永久有效"]; for (NSInteger i = 0; i < 3; i++) { PBShareBtn *btn = [PBShareBtn shareBtn]; btn.tag = 100 + i; if (i == 2) { btn.image = [UIImage imageNamed:@"Share_periodValidity_selected"]; }else { btn.image = [UIImage imageNamed:@"Share_periodValidity_normal"]; } btn.title = arr[i]; [self.bgView addSubview:btn]; [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside]; if (i == 0) { [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(validityDateL.mas_bottom).offset(16); make.left.equalTo(self.bgView).offset(40); make.width.equalTo(@100); }]; }else if (i == 1){ [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(validityDateL.mas_bottom).offset(16); make.centerX.equalTo(self.bgView); make.width.equalTo(@100); }]; }else { [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(validityDateL.mas_bottom).offset(16); make.right.equalTo(self.bgView).offset(-39); make.width.equalTo(@100); }]; } [self.btnArrM addObject:btn]; } UIView *lineV = [[UIView alloc] init]; lineV.backgroundColor = [UIColor z_colorWithR:221 G:221 B:221]; [self.bgView addSubview:lineV]; [lineV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(96); make.left.equalTo(self.bgView).offset(43); make.right.equalTo(self.bgView).offset(32); make.height.equalTo(@1); }]; [self.bgView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lineV.mas_bottom); make.left.right.equalTo(self.bgView); make.height.equalTo(@144); }]; UIButton *cancelBtn = [UIButton z_textButton:@"取消" fontSize:20.0 normalColor:TitleColor]; cancelBtn.backgroundColor = [UIColor whiteColor]; [cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside]; [self.bgView addSubview:cancelBtn]; [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.collectionView.mas_bottom); make.left.right.equalTo(self.bgView); make.height.equalTo(@49); }]; } - (void)cancelBtnAction { [self hidden]; } - (void)clickAction:(PBShareBtn *)btn { if ((btn.tag - 100) == self.btnSelectIndex) { return; } btn.image = [UIImage imageNamed:@"Share_periodValidity_selected"]; PBShareBtn *shareBtn = self.btnArrM[self.btnSelectIndex]; shareBtn.image = [UIImage imageNamed:@"Share_periodValidity_normal"]; self.btnSelectIndex = btn.tag - 100; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint point=[[touches anyObject]locationInView:self]; CALayer *layer=[self.layer hitTest:point]; if (layer ==self.layer) { [self hidden]; } } - (void)hidden { [UIView animateWithDuration:0.3 animations:^{ self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0]; self.bgView.y = PBScreenHeight; }completion:^(BOOL finished) { for (UIView *cover in PBKeyWindow.subviews) { if ([cover isKindOfClass:[PBShareView class]]) { [cover removeFromSuperview]; } } }]; } - (void)show { [UIView animateWithDuration:0.3 animations:^{ self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5]; self.bgView.y = PBScreenHeight - self.visualViewHeight; }]; } #pragma mark - 懒加载 - (UIView *)bgView { if (_bgView == nil) { _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, PBScreenHeight, PBScreenWidth, PBScreenHeight)]; _bgView.backgroundColor = PBColor(242, 242, 242); } return _bgView; } - (NSMutableArray *)btnArrM { if (_btnArrM == nil) { _btnArrM = [[NSMutableArray alloc] init]; } return _btnArrM; } - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; CGFloat itemW = PBScreenWidth / ALineCount; layout.itemSize = CGSizeMake(itemW, 144); layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionVertical; _collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [_collectionView registerClass:[PBShareCell class] forCellWithReuseIdentifier:cellID]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = PBColor(242, 242, 242); } return _collectionView; } - (NSMutableArray *)dataArrM { if (_dataArrM == nil) { _dataArrM = [[NSMutableArray alloc] init]; NSArray *titleArr = @[@"腾讯QQ", @"复制私密链接", @"微信", @"朋友圈"]; NSArray *imageArr = @[@"Share_qq", @"Share_private", @"Share_wechat", @"Share_moments"]; NSArray *numArr = @[@"QQ", @"Private", @"WeChat", @"Moments"]; NSURL * qq_app_url = [NSURL URLWithString:@"mqq://"]; BOOL qqAli = [[UIApplication sharedApplication] canOpenURL:qq_app_url]; if (qqAli) { NSDictionary *dict = @{ @"title" : titleArr[0], @"image" : [UIImage imageNamed:imageArr[0]], @"num" : numArr[0] }; [_dataArrM addObject:dict]; } NSDictionary *dict = @{ @"title" : titleArr[1], @"image" : [UIImage imageNamed:imageArr[1]], @"num" : numArr[1] }; [_dataArrM addObject:dict]; NSURL * wechat_app_url = [NSURL URLWithString:@"weixin://"]; BOOL wechatAli = [[UIApplication sharedApplication] canOpenURL:wechat_app_url]; if (wechatAli) { NSDictionary *dict = @{ @"title" : titleArr[2], @"image" : [UIImage imageNamed:imageArr[2]], @"num" : numArr[2] }; NSDictionary *dict1 = @{ @"title" : titleArr[3], @"image" : [UIImage imageNamed:imageArr[3]], @"num" : numArr[3] }; [_dataArrM addObject:dict]; [_dataArrM addObject:dict1]; } } return _dataArrM; } - (void)setVisualViewHeight:(NSInteger)visualViewHeight { _visualViewHeight = visualViewHeight; } - (void)setProjectID:(NSString *)projectID { _projectID = projectID; } - (void)setModelID:(NSString *)modelID { _modelID = modelID; } - (void)setViewController:(UIViewController *)viewController { _viewController = viewController; } - (void)setTitle:(NSString *)title { _title = title; } - (void)setDesc:(NSString *)desc { _desc = desc; } - (void)setImage:(UIImage *)image { _image = image; } - (void)setViewID:(NSString *)viewID { _viewID = viewID; } - (void)setViewPointID:(NSString *)viewPointID { _viewPointID = viewPointID; } - (void)setDocId:(NSString *)docId { _docId = docId; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.dataArrM.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ PBShareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.dict = self.dataArrM[indexPath.row]; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self hidden]; NSString *hasRandomPwd = @"0"; NSDictionary *data = self.dataArrM[indexPath.row]; if ([[data valueForKey:@"num"] isEqualToString:@"Private"]) { hasRandomPwd = @"1"; } NSString *dayCount; switch (self.btnSelectIndex) { case 0: dayCount = @"1"; break; case 1: dayCount = @"7"; break; case 2: dayCount = @"-1"; break; default: break; } [YJProgressHUD showProgress:@"" inView:self]; if (_docId) { [[PBNetworkTools sharedTools] RequestShareUrlWithBIMComposerID:self.projectID andPrivilegeStr:@"Download" andDocIds:self.docId andHasRandomPwd:hasRandomPwd andDaycount:dayCount andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"生成连接失败" inView:self]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { NSDictionary *dict = networkModel.Data; NSString *url = [NSString stringWithFormat:@"%@/#/LinkShare/DocShare/Index?SessionId=%@",WebUrl, [dict valueForKey:@"shd_SessionId"]]; if ([[data valueForKey:@"num"] isEqualToString:@"QQ"]) { [self shareQQWithUrl:url]; }else if ([[data valueForKey:@"num"] isEqualToString:@"Private"]) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = [NSString stringWithFormat:@"链接:%@ 密码:%@",url,[dict valueForKey:@"shd_Pwd"]]; [YJProgressHUD showMessage:@"已复制到剪切板" inView:self]; }else if([[data valueForKey:@"num"] isEqualToString:@"WeChat"]) { [self shareWeChatWithUrl:url]; }else if([[data valueForKey:@"num"]isEqualToString:@"Moments"]) { [self shareMomentsWithUrl:url]; }else { [YJProgressHUD showMessage:@"分享失败" inView:nil]; } }else { [YJProgressHUD showMessage:@"生成连接失败" inView:nil]; } }]; }else { [[PBNetworkTools sharedTools] RequestShareUrlWithProjectId:self.projectID andModelId:self.modelID andViewID:self.viewID andViewpointID:self.viewPointID andHasRandomPwd:hasRandomPwd andDaycount:dayCount andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"生成连接失败" inView:self]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { NSDictionary *dict = networkModel.Data; NSString *url = [NSString stringWithFormat:@"%@%@",WebUrl, [dict valueForKey:@"url"]]; if ([[data valueForKey:@"num"] isEqualToString:@"QQ"]) { [self shareQQWithUrl:url]; }else if ([[data valueForKey:@"num"] isEqualToString:@"Private"]) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = [NSString stringWithFormat:@"链接:%@ 密码:%@",url,[dict valueForKey:@"pwd"]]; [YJProgressHUD showMessage:@"已复制到剪切板" inView:self]; }else if([[data valueForKey:@"num"] isEqualToString:@"WeChat"]) { [self shareWeChatWithUrl:url]; }else if([[data valueForKey:@"num"]isEqualToString:@"Moments"]) { [self shareMomentsWithUrl:url]; }else { [YJProgressHUD showMessage:@"分享失败" inView:nil]; } }else { [YJProgressHUD showMessage:@"生成连接失败" inView:nil]; } }]; } } //分享到 QQ - (void)shareQQWithUrl:(NSString *)url { UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image]; //设置网页地址 shareObject.webpageUrl = url; //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject]; //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject; /** 设置分享 @param data 分享返回信息 @param error 失败信息 @param UMSocialPlatformType 分享平台 */ [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) { if (error) { UMSocialLogInfo(@"************Share fail with error %@*********",error); }else{ if ([data isKindOfClass:[UMSocialShareResponse class]]) { UMSocialShareResponse *resp = data; //分享结果消息 UMSocialLogInfo(@"response message is %@",resp.message); //第三方原始返回的数据 UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse); }else{ UMSocialLogInfo(@"response data is %@",data); } } }]; } //分享到微信 - (void)shareWeChatWithUrl:(NSString *)url { UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image]; //设置网页地址 shareObject.webpageUrl = url; //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject]; //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject; [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) { if (error) { UMSocialLogInfo(@"************Share fail with error %@*********",error); }else{ if ([data isKindOfClass:[UMSocialShareResponse class]]) { UMSocialShareResponse *resp = data; //分享结果消息 UMSocialLogInfo(@"response message is %@",resp.message); //第三方原始返回的数据 UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse); }else{ UMSocialLogInfo(@"response data is %@",data); } } }]; } //分享到朋友圈 - (void)shareMomentsWithUrl:(NSString *)url { UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image]; //设置网页地址 shareObject.webpageUrl = url; //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject]; //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject; /** 设置分享 @param data 分享返回信息 @param error 失败信息 @param UMSocialPlatformType 分享平台 */ [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) { if (error) { UMSocialLogInfo(@"************Share fail with error %@*********",error); }else{ if ([data isKindOfClass:[UMSocialShareResponse class]]) { UMSocialShareResponse *resp = data; //分享结果消息 UMSocialLogInfo(@"response message is %@",resp.message); //第三方原始返回的数据 UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse); }else{ UMSocialLogInfo(@"response data is %@",data); } } }]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end