// // SPSBShareViewController.m // 我的社保 // // Created by jiaxian_he on 2021/5/31. // #import "SPSBShareViewController.h" #import "SPSBUIGeneralHeader.h" #import "WXApi.h" #import #import #import "SPSBUMEventAnalysis.h" #import "UIView+SPSBLoadingTipsView.h" NSString *const SPSBShareCircleOfFriends = @"SPSBShareCircleOfFriends"; NSString *const SPSBShareWechat = @"SPSBShareWechat"; NSString *const SPSBShareQQ = @"SPSBShareQQ"; NSString *const SPSBShareQzone = @"SPSBShareQzone"; NSString *const SPSBShareMiniProgram = @"SPSBShareMiniProgram"; NSString *const SPSBShareSinaWeibo = @"SPSBShareSinaWeibo"; @implementation SPSBShareModel - (instancetype)initWithTypeArray:(NSArray *)typeArray content:(NSString *)content image:(nullable id)image title:(NSString *)title url:(NSString *)url mediaType:(SSDKContentType)mediaType eventId:(nullable NSString *)eventId finish:(void(^_Nullable)(void))finish { self = [super init]; if (!self) return nil; _spsb_url = url; _spsb_image = image; _spsb_title = title; _spsb_mediaType = mediaType; _spsb_eventId = eventId; _spsb_content = content; _spsb_typeArray = typeArray; _spsb_finishAction = finish; return self; } @end @interface SPSBShareViewController () { NSMutableArray *_tagArray; } @end @implementation SPSBShareViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } #pragma mark - Action - (void)shareAction:(UIButton *)sender { if (_spsb_data.spsb_eventId) { [SPSBUMEventAnalysis analyseEventWithId:_spsb_data.spsb_eventId]; } id image; if ([_spsb_data.spsb_image isKindOfClass:[UIImage class]]) { image = _spsb_data.spsb_image; } else if ([_spsb_data.spsb_image isKindOfClass:[NSString class]]) { image = [[SSDKImage alloc] initWithURL:[NSURL URLWithString:_spsb_data.spsb_image]]; } else { image = [UIImage imageNamed:SPSBLogo]; } NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; NSInteger tag = [_tagArray[sender.tag - 3000] integerValue]; switch (tag) { case 2000: { if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) { [self.view showToastWithTitle:@"未安装微信"]; break; } [self dismissViewWithCompletion:^{ [shareParams SSDKSetupWeChatParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] thumbImage:nil image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeWechatTimeline]; [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2001: { if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) { [self.view showToastWithTitle:@"未安装微信"]; break; } [self dismissViewWithCompletion:^{ [shareParams SSDKSetupWeChatParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] thumbImage:nil image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeWechatSession]; [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2002: { if (![TencentOAuth iphoneQQInstalled]) { [self.view showToastWithTitle:@"未安装QQ"]; break; } [self dismissViewWithCompletion:^{ [shareParams SSDKSetupQQParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] audioFlashURL:nil videoFlashURL:nil thumbImage:nil images:image type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeQQFriend]; [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2003: { if (![TencentOAuth iphoneQQInstalled]) { [self.view showToastWithTitle:@"未安装QQ"]; break; } [self dismissViewWithCompletion:^{ [shareParams SSDKSetupQQParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] audioFlashURL:nil videoFlashURL:nil thumbImage:nil images:image type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeQZone]; [ShareSDK share:SSDKPlatformSubTypeQZone parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2004: { if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) { [self.view showToastWithTitle:@"未安装微信"]; break; } [self dismissViewWithCompletion:^{ [shareParams SSDKSetupCopyParamsByText:self->_spsb_data.spsb_content images:image url:[NSURL URLWithString:self->_spsb_data.spsb_url] type:self->_spsb_data.spsb_mediaType]; [shareParams SSDKSetupWeChatMiniProgramShareParamsByTitle:self->_spsb_data.spsb_title description:self->_spsb_data.spsb_content webpageUrl:[NSURL URLWithString:self->_spsb_data.spsb_url] path:@"page/home/index" thumbImage:jxh_getImage(share_img) hdThumbImage:jxh_getImage(share_img) userName:@"gh_2d56467457de" withShareTicket:false miniProgramType:0 forPlatformSubType:SSDKPlatformSubTypeWechatSession]; [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2005: { [self dismissViewWithCompletion:^{ [shareParams SSDKSetupSMSParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title images:image attachments:nil recipients:@[] type:self->_spsb_data.spsb_mediaType]; [ShareSDK share:SSDKPlatformTypeSMS parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; case 2006: { [self dismissViewWithCompletion:^{ [shareParams SSDKSetupSinaWeiboShareParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title images:image video:nil url:[NSURL URLWithString:self->_spsb_data.spsb_url] latitude:0 longitude:0 objectID:nil isShareToStory:false type:self->_spsb_data.spsb_mediaType]; [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { }]; }]; } break; default: break; } } - (void)shareCloseAction { [self dismissViewWithCompletion:^{ }]; } - (void)dismissViewWithCompletion:(void (^)(void))completion { jxh_windowUserInteractionEnabled(false); [self dismissAnimationWithIndex:_spsb_data.spsb_typeArray.count - 1 completion:completion]; } - (void)dismissAnimationWithIndex:(NSInteger)index completion:(void (^)(void))completion { if (index < 0) return; UIButton *button = [self.view viewWithTag:3000 + index]; NSInteger c = index / 3; [button makeConstraints:^(JXHConstraintMaker *make) { make.top.update(jxh_screenHeight() + c * 118); }]; [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutSubviews]; } completion:^(BOOL finished) { }]; if (index == 0) { [UIView animateWithDuration:0.5 animations:^{ self.view.alpha = 0; } completion:^(BOOL finished) { [self dismissViewControllerAnimated:false completion:^{ jxh_windowUserInteractionEnabled(true); completion(); if (self->_spsb_data.spsb_finishAction) { self->_spsb_data.spsb_finishAction(); } }]; }]; } jxh_delayAction(0.05, dispatch_get_main_queue(), ^{ [self dismissAnimationWithIndex:index - 1 completion:completion]; }); } - (void)showAnimationWithIndex:(NSInteger)index max:(NSInteger)max { NSInteger top = jxh_screenHeight() - 346 - jxh_safeInsets(spsb_keyWindow()).bottom; if (index + 1 > max) { jxh_windowUserInteractionEnabled(true); return; } UIButton *button = [self.view viewWithTag:3000 + index]; NSInteger c = index / 3; [button makeConstraints:^(JXHConstraintMaker *make) { make.top.update(top + c * 118); }]; [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutSubviews]; } completion:nil]; jxh_delayAction(0.05, dispatch_get_main_queue(), ^{ [self showAnimationWithIndex:index + 1 max:max]; }); } #pragma mark - NetworkAction #pragma mark - Overwrite - (void)setupAnimation { } - (void)showAnimation { jxh_windowUserInteractionEnabled(false); [self showAnimationWithIndex:0 max:_spsb_data.spsb_typeArray.count]; } #pragma mark - UI - (void)setupUI { self.view.backgroundColor = spsb_clear_color(); UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect]; [self.view addSubview:effectView]; [effectView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(0); }]; [self addViews]; [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareCloseAction)]]; } - (void)addViews { CGFloat left = (jxh_screenWidth() - 70 * 3 - 46 * 2) / 2; NSDictionary *dataDic = @{ SPSBShareWechat : @[@"微信好友", @"share_wechat", @2001], SPSBShareCircleOfFriends : @[@"朋友圈", @"share_friends", @2000], SPSBShareQQ : @[@"QQ好友", @"share_qq", @2002], SPSBShareQzone : @[@"QQ空间", @"share_qzone", @2003], SPSBShareMiniProgram : @[@"微信小程序", @"share_miniprograms", @2004], SPSBShareSinaWeibo : @[@"新浪微博", @"share_weibo", @2006], }; _tagArray = [@[] mutableCopy]; UIButton *tempButton = nil; for (NSInteger i = 0; i < _spsb_data.spsb_typeArray.count; i ++) { [_tagArray addObject:dataDic[_spsb_data.spsb_typeArray[i]][2]]; UIButton *button = [UIButton convenienceWithTarget:self action:@selector(shareAction:)]; button.tag = 3000 + i; [self.view addSubview:button]; NSInteger c = i / 3; [button makeConstraints:^(JXHConstraintMaker *make) { if (tempButton) { if (i % 3 == 0) { make.leading.equalTo(left); } else { make.leading.equalTo(tempButton.trailing).offset(46); } } else { make.leading.equalTo(left); } make.top.equalTo(jxh_screenHeight() + c * 118); make.width.equalTo(70); }]; UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dataDic[_spsb_data.spsb_typeArray[i]][1]]]; [button addSubview:icon]; [icon makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(0); make.centerX.equalTo(button); make.size.equalTo(CGSizeMake(60, 60)); }]; UILabel *label = [UILabel convenienceWithFont:spsb_font(14) text:dataDic[_spsb_data.spsb_typeArray[i]][0] textColor:spsb_000000_color(0.8) textAlignment:NSTextAlignmentCenter]; [button addSubview:label]; [label makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(icon.bottom).offset(10); make.centerX.equalTo(button); make.bottom.equalTo(0); }]; tempButton = button; } UIButton *close = [UIButton convenienceWithTarget:self action:@selector(shareCloseAction)]; [close setImage:jxh_getImage(share_close) state:JXHButtonControlStateNormal]; [self.view addSubview:close]; [close makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(tempButton.bottom).offset(60); make.centerX.equalTo(self.view); }]; } @end