You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
9.5 KiB
233 lines
9.5 KiB
4 years ago
|
//
|
||
|
// TFShareManager.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/5.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFShareManager.h"
|
||
|
#import "TFShareView.h"
|
||
|
#import "TFShareModel.h"
|
||
|
#import "TFShareBodyModel.h"
|
||
|
#if __has_include(<UMShare/UMShare.h>)
|
||
|
#import <UMShare/UMShare.h>
|
||
|
#endif
|
||
|
#import <ShareSDK/ShareSDK.h>
|
||
|
|
||
|
@implementation TFShareManager
|
||
|
|
||
|
+ (void)shareWithProduction_id:(NSString *)production_id chapter_id:(NSString * _Nullable)chapter_id type:(TFShareType)type
|
||
|
{
|
||
|
TFShareBodyModel *t_model = [[TFShareBodyModel alloc] init];
|
||
|
|
||
|
[self getDetailsWithShareModel:t_model isShareApp:NO type:type production_id:production_id chapter_id:chapter_id];
|
||
|
|
||
|
[self showShareView:^(TFSharingPlatform platform) {
|
||
|
[self share:platform shareModel:t_model];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)shareApp
|
||
|
{
|
||
|
TFShareBodyModel *t_model = [[TFShareBodyModel alloc] init];
|
||
|
|
||
|
[self getDetailsWithShareModel:t_model isShareApp:YES type:kNilOptions production_id:nil chapter_id:nil];
|
||
|
|
||
|
[self showShareView:^(TFSharingPlatform platform) {
|
||
|
[self share:platform shareModel:t_model];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)shareWithTitle:(NSString *)title desc:(NSString *)desc imageUrl:(NSString *)imageUrl shareUrl:(NSString *)shareUrl
|
||
|
{
|
||
|
TFShareBodyModel *t_model = [[TFShareBodyModel alloc] init];
|
||
|
t_model.title = title;
|
||
|
t_model.desc = desc;
|
||
|
t_model.imageUrl = imageUrl;
|
||
|
t_model.shareUrl = shareUrl;
|
||
|
[self showShareView:^(TFSharingPlatform platform) {
|
||
|
[self share:platform shareModel:t_model];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
/// 从服务器获取分享的详细数据
|
||
|
+ (void)getDetailsWithShareModel:(TFShareBodyModel *)shareModel isShareApp:(BOOL)isShareApp type:(TFShareType)type production_id:(NSString * _Nullable)production_id chapter_id:(NSString * _Nullable)chapter_id
|
||
|
{
|
||
|
NSMutableDictionary *params = [NSMutableDictionary dictionary];
|
||
|
if (!isShareApp) {
|
||
|
[params setObject:@(type) forKey:@"type"];
|
||
|
[params setObject:production_id ?: @"" forKey:@"novel_id"];
|
||
|
if (!kObjectIsEmpty(chapter_id)) {
|
||
|
[params setObject:chapter_id forKey:@"chapter_id"];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[TFNetworkTools POST:isShareApp ? App_Share : App_Chapter_Share parameters:params model:TFShareModel.class success:^(BOOL isSuccess, TFShareModel *_Nullable t_model, TFNetworkRequestModel *requestModel) {
|
||
|
if (isSuccess) {
|
||
|
NSString *shareUrl = @"";
|
||
|
if (TFUserInfoManager.isLogin) {
|
||
|
shareUrl = [NSString stringWithFormat:@"%@?uid=%zd&osType=1&product=1", t_model.link ?: [NSString stringWithFormat:@"%@%@", APIURL, Site_Share], TFUserInfoManager.shareInstance.uid];
|
||
|
} else {
|
||
|
shareUrl = [NSString stringWithFormat:@"%@?osType=1&product=1", t_model.link ?: [NSString stringWithFormat:@"%@%@", APIURL, Site_Share]];
|
||
|
}
|
||
|
|
||
|
shareModel.title = @"";
|
||
|
shareModel.title = t_model.title;
|
||
|
shareModel.desc = t_model.desc;
|
||
|
shareModel.imageUrl = t_model.imgUrl;
|
||
|
shareModel.shareUrl = shareUrl;
|
||
|
} else {
|
||
|
shareModel.error = requestModel.msg ?: TFLocalizedString(@"分享错误");
|
||
|
}
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
shareModel.error = TFLocalizedString(@"分享错误");
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
/// 弹出分享框
|
||
|
+ (void)showShareView:(void(^)(TFSharingPlatform platform))clickBlock
|
||
|
{
|
||
|
TFShareView *shareView = [[TFShareView alloc] init];
|
||
|
shareView.clickHandler = ^(TFSharingPlatform platform) {
|
||
|
!clickBlock ?: clickBlock(platform);
|
||
|
};
|
||
|
[shareView show];
|
||
|
}
|
||
|
|
||
|
/// 分享至指定平台
|
||
|
+ (void)share:(TFSharingPlatform)platform shareModel:(TFShareBodyModel *)shareModel
|
||
|
{
|
||
|
if (kObjectIsEmpty(shareModel.shareUrl) && kObjectIsEmpty(shareModel.imageUrl)) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享地址出错,请稍后重试")];
|
||
|
return;
|
||
|
}
|
||
|
// 获取分享平台的数据
|
||
|
NSMutableDictionary *params = ({
|
||
|
NSMutableDictionary *params = [NSMutableDictionary dictionary];
|
||
|
[params SSDKSetupShareParamsByText:shareModel.desc images:shareModel.shareImage url:[NSURL URLWithString:shareModel.shareUrl] title:shareModel.title type:SSDKContentTypeAuto];
|
||
|
params;
|
||
|
});
|
||
|
|
||
|
// 分享至指定平台
|
||
|
switch (platform) {
|
||
|
case TFSharingPlatformQQ: {
|
||
|
|
||
|
[ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:params onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
|
||
|
if (error.code == 200104) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"请先安装客户端")];
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
switch (state) {
|
||
|
case SSDKResponseStateSuccess: {
|
||
|
[self shareCallback];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateFail: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateCancel:
|
||
|
break;
|
||
|
default: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TFSharingPlatformQQSpace: {
|
||
|
[ShareSDK share:SSDKPlatformSubTypeQZone parameters:params onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
|
||
|
if (error.code == 200104) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"请先安装客户端")];
|
||
|
return;
|
||
|
}
|
||
|
switch (state) {
|
||
|
case SSDKResponseStateSuccess: {
|
||
|
[self shareCallback];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateFail: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateCancel:
|
||
|
break;
|
||
|
default: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TFSharingPlatformWeChat: {
|
||
|
|
||
|
[ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:params onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
|
||
|
if (error.code == 200104) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"请先安装客户端")];
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
switch (state) {
|
||
|
case SSDKResponseStateSuccess: {
|
||
|
[self shareCallback];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateFail: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateCancel:
|
||
|
break;
|
||
|
default: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TFSharingPlatformFriendCircle: {
|
||
|
[ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:params onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
|
||
|
if (error.code == 200104) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"请先安装客户端")];
|
||
|
return;
|
||
|
}
|
||
|
switch (state) {
|
||
|
case SSDKResponseStateSuccess: {
|
||
|
[self shareCallback];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateFail: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
break;
|
||
|
case SSDKResponseStateCancel:
|
||
|
break;
|
||
|
default: {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"分享失败")];
|
||
|
}
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// 分享回调
|
||
|
+ (void)shareCallback
|
||
|
{
|
||
|
[TFNetworkTools POST:Share_Back parameters:nil model:nil success:^(BOOL isSuccess, NSDictionary *_Nullable t_model, TFNetworkRequestModel *requestModel) {
|
||
|
if (isSuccess) {
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Share_Success object:nil];
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"分享成功")];
|
||
|
}
|
||
|
} failure:nil];
|
||
|
}
|
||
|
|
||
|
@end
|