431 lines
16 KiB
Objective-C
431 lines
16 KiB
Objective-C
//
|
|
// QXShareView.m
|
|
// QXLive
|
|
//
|
|
// Created by 启星 on 2025/5/21.
|
|
//
|
|
|
|
#import "QXShareView.h"
|
|
#import <WXApi.h>
|
|
#import <TencentOpenAPI/TencentOpenApiUmbrellaHeader.h>
|
|
#import "QXDynamicNetwork.h"
|
|
|
|
@interface QXShareView()<UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate,QXShareViewDelegate>
|
|
@property (nonatomic,strong)UIView *bgView;
|
|
@property (nonatomic,strong)UILabel *titleLabel;
|
|
@property (nonatomic,strong)UICollectionView *collectionView;
|
|
@property (nonatomic,strong)NSMutableArray *dataArray;
|
|
@property (nonatomic,strong)NSMutableArray *friendArray;
|
|
@property (nonatomic,strong)NSMutableArray *eventArray;
|
|
@end
|
|
@implementation QXShareView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubviews];
|
|
}
|
|
return self;
|
|
}
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
[self initSubviews];
|
|
}
|
|
return self;
|
|
}
|
|
-(void)initSubviews{
|
|
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
|
|
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
|
|
tap.delegate = self;
|
|
[self addGestureRecognizer:tap];
|
|
self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200)];
|
|
self.bgView.backgroundColor = [UIColor whiteColor];
|
|
[self addSubview:self.bgView];
|
|
|
|
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, SCREEN_WIDTH-32, 27)];
|
|
self.titleLabel.textColor = QXConfig.textColor;
|
|
self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
|
|
self.titleLabel.text = QXText(@"分享至");
|
|
[self.bgView addSubview:self.titleLabel];
|
|
|
|
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
|
layout.itemSize = CGSizeMake(SCREEN_WIDTH, 71);
|
|
layout.minimumLineSpacing = 0;
|
|
layout.minimumInteritemSpacing = 0;
|
|
layout.sectionInset = UIEdgeInsetsMake(0, 0, 20, 0);
|
|
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom+12, self.bgView.width, self.bgView.height-self.titleLabel.bottom-12-kSafeAreaBottom) collectionViewLayout:layout];
|
|
[self.collectionView registerClass:[QXShareViewContentCell class] forCellWithReuseIdentifier:@"QXShareViewContentCell"];
|
|
self.collectionView.delegate = self;
|
|
self.collectionView.dataSource = self;
|
|
self.collectionView.showsHorizontalScrollIndicator = NO;
|
|
self.collectionView.bounces = NO;
|
|
[self.bgView addSubview:self.collectionView];
|
|
}
|
|
-(void)setShareType:(QXShareViewType)shareType{
|
|
_shareType = shareType;
|
|
if (_shareType == QXShareViewTypeFind) {
|
|
self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScaleWidth(347));
|
|
[self.bgView addRoundedCornersWithRadius:16 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)];
|
|
self.collectionView.frame = CGRectMake(0, self.titleLabel.bottom+12, self.bgView.width, self.bgView.height-self.titleLabel.bottom-12-kSafeAreaBottom);
|
|
}else{
|
|
self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScaleWidth(200));
|
|
[self.bgView addRoundedCornersWithRadius:16 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)];
|
|
self.collectionView.frame = CGRectMake(0, self.titleLabel.bottom+12, self.bgView.width, self.bgView.height-self.titleLabel.bottom-12-kSafeAreaBottom);
|
|
}
|
|
[self.collectionView reloadData];
|
|
}
|
|
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
|
|
return touch.view == self;
|
|
}
|
|
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
|
|
if (self.shareType == QXShareViewTypeFind) {
|
|
return 3;
|
|
}else{
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
|
return 1;
|
|
}
|
|
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
QXShareViewContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXShareViewContentCell" forIndexPath:indexPath];
|
|
cell.delegate = self;
|
|
if (self.shareType == QXShareViewTypeFind) {
|
|
if (indexPath.section == 0) {
|
|
cell.dataArray = self.friendArray;
|
|
}else if (indexPath.section == 1){
|
|
cell.dataArray = self.dataArray;
|
|
}else{
|
|
cell.dataArray = [NSMutableArray arrayWithArray:self.eventArray];
|
|
}
|
|
}else{
|
|
cell.dataArray = self.dataArray;
|
|
}
|
|
return cell;
|
|
}
|
|
-(void)didClickShareModel:(QXShareViewModel *)model{
|
|
MJWeakSelf
|
|
[UIView animateWithDuration:0.3 animations:^{
|
|
self.bgView.y = SCREEN_HEIGHT;
|
|
} completion:^(BOOL finished) {
|
|
[self removeFromSuperview];
|
|
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didClickShareModel:)]) {
|
|
[weakSelf.delegate didClickShareModel:model];
|
|
}
|
|
[self shareWithPlatform:model];
|
|
}];
|
|
|
|
}
|
|
|
|
-(void)shareWithPlatform:(QXShareViewModel*)model{
|
|
// [WXApi startLogByLevel:(WXLogLevelDetail) logBlock:^(NSString * _Nonnull log) {
|
|
// QXLOG(@"wx--------------%@",log);
|
|
// }];
|
|
// [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult * _Nonnull result) {
|
|
// QXLOG(@"步骤wx--------------%ld-----------%@",step,result);
|
|
// }];
|
|
// SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
|
|
// req.bText = YES;
|
|
// req.text = @"分享的内容";
|
|
// req.scene = WXSceneSession;
|
|
// [WXApi sendReq:req completion:^(BOOL success) {
|
|
//
|
|
// }];
|
|
//
|
|
// return;
|
|
if ([model.name isEqualToString:QXText(@"微信好友")]) {
|
|
|
|
if (self.dynamicModel != nil) /*分享动态*/{
|
|
[self shareDynamicWithWechat:YES];
|
|
}else if (self.imageData != nil){
|
|
WXImageObject *imageObject = [WXImageObject object];
|
|
imageObject.imageData = self.imageData;
|
|
|
|
WXMediaMessage *message = [WXMediaMessage message];
|
|
// message.thumbData = nil;
|
|
message.mediaObject = imageObject;
|
|
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
|
|
req.bText = NO;
|
|
req.message = message;
|
|
req.scene = WXSceneSession;
|
|
[WXApi sendReq:req completion:^(BOOL success) {
|
|
|
|
}];
|
|
}
|
|
}else if ([model.name isEqualToString:QXText(@"朋友圈")]){
|
|
if (self.dynamicModel != nil) /*分享朋友圈*/{
|
|
[self shareDynamicWithWechat:NO];
|
|
}else if (self.imageData != nil){
|
|
WXImageObject *imageObject = [WXImageObject object];
|
|
imageObject.imageData = self.imageData;
|
|
|
|
WXMediaMessage *message = [WXMediaMessage message];
|
|
message.thumbData = self.imageData;
|
|
message.mediaObject = imageObject;
|
|
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
|
|
req.bText = NO;
|
|
req.message = message;
|
|
req.scene = WXSceneTimeline;
|
|
[WXApi sendReq:req completion:^(BOOL success) {
|
|
|
|
}];
|
|
}
|
|
}else if ([model.name isEqualToString:QXText(@"QQ")]){
|
|
if (self.dynamicModel != nil) /*分享动态*/{
|
|
[self shareDynamicWithQQ:YES];
|
|
}
|
|
}else if ([model.name isEqualToString:QXText(@"QQ空间")]){
|
|
if (self.dynamicModel != nil) /*分享朋友圈*/{
|
|
[self shareDynamicWithQQ:NO];
|
|
}
|
|
}
|
|
}
|
|
|
|
-(void)shareDynamicWithWechat:(BOOL)isSession{
|
|
__block WXWebpageObject *webpageObject = [WXWebpageObject object];
|
|
__block WXMediaMessage *message = [WXMediaMessage message];
|
|
webpageObject.webpageUrl = self.dynamicModel.share_url?self.dynamicModel.share_url:@"https://www.baidu.com";
|
|
if (self.dynamicModel.content.length > 20) {
|
|
message.description = [self.dynamicModel.content substringToIndex:20];
|
|
}else{
|
|
message.description = self.dynamicModel.content;
|
|
}
|
|
[message setThumbImage:[UIImage imageNamed:@"user_header_placehoulder"]];
|
|
message.mediaObject = webpageObject;
|
|
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
|
|
req.bText = NO;
|
|
req.message = message;
|
|
req.scene = isSession?WXSceneSession:WXSceneTimeline;
|
|
[WXApi sendReq:req completion:^(BOOL success) {
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)shareDynamicWithQQ:(BOOL)isSession{
|
|
|
|
if (self.dynamicModel.images.length == 0) {
|
|
NSString *content = @"";
|
|
if (self.dynamicModel.content.length > 20) {
|
|
content = [self.dynamicModel.content substringToIndex:20];
|
|
}else{
|
|
content = self.dynamicModel.content;
|
|
}
|
|
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL :[NSURL URLWithString:self.dynamicModel.share_url]
|
|
title: @""
|
|
description :content
|
|
previewImageURL:[NSURL URLWithString:self.dynamicModel.avatar]];
|
|
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
|
|
//将内容分享到qq
|
|
if (isSession) {
|
|
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
|
|
}else{
|
|
//将内容分享到qzone
|
|
QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
|
|
}
|
|
|
|
}else{
|
|
NSArray *images = [self.dynamicModel.images componentsSeparatedByString:@","];
|
|
NSString* img = images.firstObject;
|
|
NSString *content = @"";
|
|
if (self.dynamicModel.content.length > 20) {
|
|
content = [self.dynamicModel.content substringToIndex:20];
|
|
}else{
|
|
content = self.dynamicModel.content;
|
|
}
|
|
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL :[NSURL URLWithString:self.dynamicModel.share_url]
|
|
title: @""
|
|
description :content
|
|
previewImageURL:[NSURL URLWithString:img]];
|
|
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
|
|
//将内容分享到qq
|
|
if (isSession) {
|
|
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
|
|
}else{
|
|
//将内容分享到qzone
|
|
QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
-(void)setImageData:(NSData *)imageData{
|
|
_imageData = imageData;
|
|
}
|
|
|
|
-(void)setDynamicModel:(QXDynamicModel *)dynamicModel{
|
|
_dynamicModel = dynamicModel;
|
|
if (![dynamicModel.user_id isEqualToString:[QXGlobal shareGlobal].loginModel.user_id]) {
|
|
[self.eventArray removeLastObject];
|
|
}
|
|
MJWeakSelf
|
|
[QXDynamicNetwork mutualRelationshipWithPage:0 successBlock:^(NSArray<QXDynamicLikeModel *> * _Nonnull hotos) {
|
|
for (QXDynamicLikeModel*md in hotos) {
|
|
QXShareViewModel *model = [[QXShareViewModel alloc] init];
|
|
model.icon = md.avatar;
|
|
model.Id = md.user_id;
|
|
model.name = md.nickname;
|
|
[weakSelf.friendArray addObject:model];
|
|
}
|
|
[weakSelf.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
|
|
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
|
|
|
|
}];
|
|
}
|
|
|
|
-(NSMutableArray *)dataArray{
|
|
if (!_dataArray) {
|
|
_dataArray = [NSMutableArray array];
|
|
[_dataArray addObjectsFromArray:QXConfig.sharePlatforms];
|
|
}
|
|
return _dataArray;
|
|
}
|
|
-(NSMutableArray *)friendArray{
|
|
if (!_friendArray) {
|
|
_friendArray = [NSMutableArray array];
|
|
// QXShareViewModel *model1 = [[QXShareViewModel alloc] init];
|
|
// model1.icon = @"dynamic_copy_link";
|
|
// model1.name = QXText(@"复制链接");
|
|
// model1.Id = @"0";
|
|
//
|
|
// QXShareViewModel *model2 = [[QXShareViewModel alloc] init];
|
|
// model2.icon = @"dynamic_copy_link";
|
|
// model2.name = QXText(@"举报");
|
|
// model2.Id = @"1";
|
|
//
|
|
// QXShareViewModel *model3 = [[QXShareViewModel alloc] init];
|
|
// model3.icon = @"dynamic_copy_link";
|
|
// model3.name = QXText(@"删除");
|
|
// model3.Id = @"2";
|
|
// [_friendArray addObjectsFromArray:@[model1,model2,model3,model3,model3,model3,model3,model3]];
|
|
}
|
|
return _friendArray;
|
|
}
|
|
-(NSMutableArray *)eventArray{
|
|
if (!_eventArray) {
|
|
QXShareViewModel *model1 = [[QXShareViewModel alloc] init];
|
|
model1.icon = @"dynamic_copy_link";
|
|
model1.name = QXText(@"复制链接");
|
|
model1.Id = @"0";
|
|
|
|
QXShareViewModel *model2 = [[QXShareViewModel alloc] init];
|
|
model2.icon = @"dynamic_report";
|
|
model2.name = QXText(@"举报");
|
|
model2.Id = @"1";
|
|
|
|
QXShareViewModel *model3 = [[QXShareViewModel alloc] init];
|
|
model3.icon = @"dynamic_delete";
|
|
model3.name = QXText(@"删除");
|
|
model3.Id = @"2";
|
|
_eventArray = [NSMutableArray arrayWithArray:@[model1,model2,model3]];
|
|
}
|
|
return _eventArray;
|
|
}
|
|
-(void)showInView:(UIView *)view{
|
|
[view addSubview:self];
|
|
[UIView animateWithDuration:0.3 animations:^{
|
|
self.bgView.y = SCREEN_HEIGHT-self.bgView.height;
|
|
}];
|
|
}
|
|
-(void)hideView{
|
|
[UIView animateWithDuration:0.3 animations:^{
|
|
self.bgView.y = SCREEN_HEIGHT;
|
|
} completion:^(BOOL finished) {
|
|
[self removeFromSuperview];
|
|
}];
|
|
}
|
|
@end
|
|
|
|
|
|
@implementation QXShareViewContentCell
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubviews];
|
|
}
|
|
return self;
|
|
}
|
|
-(void)initSubviews{
|
|
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
|
layout.itemSize = CGSizeMake((SCREEN_WIDTH-16*4-16)/5, 71);
|
|
layout.minimumLineSpacing = 16;
|
|
layout.minimumInteritemSpacing = 16;
|
|
layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
|
|
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
|
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
|
|
[self.collectionView registerClass:[QXShareViewCell class] forCellWithReuseIdentifier:@"QXShareViewCell"];
|
|
self.collectionView.delegate = self;
|
|
self.collectionView.dataSource = self;
|
|
self.collectionView.showsHorizontalScrollIndicator = NO;
|
|
self.collectionView.bounces = NO;
|
|
[self.contentView addSubview:self.collectionView];
|
|
}
|
|
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
|
return self.dataArray.count;
|
|
}
|
|
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
QXShareViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXShareViewCell" forIndexPath:indexPath];
|
|
QXShareViewModel *model = self.dataArray[indexPath.row];
|
|
cell.model = model;
|
|
return cell;
|
|
}
|
|
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
QXShareViewModel *model = self.dataArray[indexPath.row];
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickShareModel:)]) {
|
|
[self.delegate didClickShareModel:model];
|
|
}
|
|
}
|
|
|
|
-(void)setDataArray:(NSMutableArray *)dataArray{
|
|
_dataArray = dataArray;
|
|
[self.collectionView reloadData];
|
|
}
|
|
@end
|
|
|
|
@implementation QXShareViewCell
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubviews];
|
|
}
|
|
return self;
|
|
}
|
|
-(void)initSubviews{
|
|
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, 45)];
|
|
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
|
[self.contentView addSubview:self.imageView];
|
|
|
|
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.imageView.bottom+8, self.width, 18)];
|
|
self.titleLabel.font = [UIFont systemFontOfSize:12];
|
|
self.titleLabel.textColor = QXConfig.textColor;
|
|
self.titleLabel.textAlignment = NSTextAlignmentCenter;
|
|
[self.contentView addSubview:self.titleLabel];
|
|
}
|
|
|
|
-(void)setModel:(QXShareViewModel *)model{
|
|
_model = model;
|
|
if ([model.icon hasPrefix:@"https"] || [model.icon hasPrefix:@"http"]) {
|
|
[self.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon]];
|
|
}else{
|
|
self.imageView.image = [UIImage imageNamed:model.icon];
|
|
}
|
|
self.titleLabel.text = model.name;
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
@implementation QXShareViewModel
|
|
|
|
|
|
|
|
@end
|