zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//
//  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()<UICollectionViewDelegate, UICollectionViewDataSource>
@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<UITouch *> *)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