Files
peko-ios/YuMi/Modules/YMLogin/Presenter/LoginPresenter.m

265 lines
12 KiB
Mathematica
Raw Normal View History

2023-07-14 18:50:55 +08:00
//
// LoginPresenter.m
// YUMI
//
// Created by zu on 2021/9/1.
//
#import "LoginPresenter.h"
///Third
#import <ReactiveObjC/ReactiveObjC.h>
#import <ShareSDK/ShareSDK.h>
///APi
#import "Api+Login.h"
///Tool
#import "AccountInfoStorage.h"
#import "XNDJTDDLoadingTool.h"
#import "YUMIMacroUitls.h"
///P
#import "LoginProtocol.h"
///Model
#import "ThirdUserInfo.h"
#import "AccountModel.h"
2023-08-11 14:46:56 +08:00
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
2023-12-29 16:43:37 +08:00
#import "YuMi-swift.h"
2023-07-14 18:50:55 +08:00
@implementation LoginPresenter
2023-08-14 14:39:41 +08:00
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (instancetype)init{
self = [super init];
if(self){
2023-08-14 17:16:30 +08:00
@weakify(self);
2023-08-14 14:39:41 +08:00
[[NSNotificationCenter defaultCenter] addObserverForName:FBSDKProfileDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:
^(NSNotification *notification) {
2023-08-14 17:16:30 +08:00
@strongify(self);
2023-08-14 14:39:41 +08:00
if ([FBSDKProfile currentProfile]) {
//
[FBSDKProfile loadCurrentProfileWithCompletion:
^(FBSDKProfile *profile, NSError *error) {
if (profile) {
2023-08-14 17:16:30 +08:00
[AccountInfoStorage instance].thirdUserInfo.userName = profile.name;
[AccountInfoStorage instance].thirdUserInfo.avatarUrl = profile.imageURL.absoluteString;
2023-08-29 12:13:17 +08:00
2023-08-14 14:39:41 +08:00
}
}];
}
}];
}
return self;
}
2023-07-14 18:50:55 +08:00
- (id<LoginProtocol>)getView {
return ((id<LoginProtocol>) [super getView]);
}
- (void)phoneQuickLogin:(NSString *)accessToken token:(NSString *)token {
[Api phoneQuickLogin:[self createHttpCompletion:^(BaseModel *data) {
[[AccountInfoStorage instance] saveAccountInfo:[AccountModel modelWithDictionary:data.data]];
[[self getView] loginSuccess];
} showLoading:YES] accessToken:accessToken token:token];
}
///
/// @param type
- (void)thirdLoginWithType:(ThirdLoginType)type{
2023-08-14 19:03:26 +08:00
2023-08-14 14:39:41 +08:00
SSDKPlatformType platformType;
switch (type) {
case ThirdLoginType_FB:
platformType = SSDKPlatformTypeFacebook;
break;
case ThirdLoginType_Line:
platformType = SSDKPlatformTypeLine;
break;
case ThirdLoginType_Apple:
platformType = SSDKPlatformTypeAppleAccount;
break;
case ThirdLoginType_Gmail:
platformType = SSDKPlatformTypeGooglePlus;
break;
default:
platformType = SSDKPlatformTypeAppleAccount;
break;
}
NSDictionary * settings;
if (type == SSDKPlatformTypeFacebook) {
settings = @{@"isBrowser":@(YES)};
}
[ShareSDK cancelAuthorize:platformType result:nil];
[ShareSDK authorize:platformType settings:settings onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
if (state == SSDKResponseStateSuccess) {///
ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init];
NSString * openid = @"";
NSString * access_token = user.credential.token.length > 0 ? user.credential.token : @"";
NSString * unionid = @"";
if (platformType == SSDKPlatformTypeLine) {
openid = user.credential.uid.length > 0 ? user.credential.uid : user.uid;
unionid = user.credential.uid.length > 0 ? user.credential.uid : user.uid;
userInfo.userName = user.nickname;
userInfo.avatarUrl = user.icon;
} else if (platformType == SSDKPlatformTypeFacebook) { //
openid = user.credential.uid.length > 0 ? user.credential.uid : user.uid;;
unionid = user.credential.uid.length > 0 ? user.credential.uid : user.uid;;
userInfo.userName = user.nickname;
userInfo.avatarUrl = user.icon;
} else if (platformType == SSDKPlatformTypeAppleAccount) { //
// openid = user.credential.token;
unionid = [user.credential rawData][@"user"];
NSString * familyName = [user.credential rawData][@"fullName"][@"familyName"];
NSString * givenName = [user.credential rawData][@"fullName"][@"givenName"];
if (familyName.length > 0 && givenName.length> 0) {
userInfo.userName = [NSString stringWithFormat:@"%@%@", familyName, givenName];
}
}
if (unionid == nil) {
unionid = @"";
}
openid = unionid;
userInfo.openid = openid;
userInfo.access_token = access_token;
userInfo.unionid = unionid;
///
[AccountInfoStorage instance].thirdUserInfo = userInfo;
[self loginWithThirdPartWithType:type];
} else if(state == SSDKResponseStateCancel) {///
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")];
} else if (state == SSDKResponseStateFail) {///
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
}
}];
2023-07-14 18:50:55 +08:00
}
-(void)loginWithThirdPartWithType:(ThirdLoginType)type{
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool showOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
NSString * openid = [AccountInfoStorage instance].thirdUserInfo.openid;
NSString * access_token = [AccountInfoStorage instance].thirdUserInfo.access_token;
NSString * unionid = [AccountInfoStorage instance].thirdUserInfo.unionid;
[Api loginWithThirdPart:[self createHttpCompletion:^(BaseModel * _Nonnull data) {
AccountModel * model = [AccountModel modelWithDictionary:data.data];
if (model != nil) {
[[AccountInfoStorage instance] saveAccountInfo:model];
[[self getView] loginSuccess];
[XPAdjustEvent loginEvent];
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[[NSUserDefaults standardUserDefaults]setValue:@(type) forKey:@"kLoginSuccessType"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
} fail:^(NSInteger code, NSString * _Nullable msg) {
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
} showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)type]];
2023-12-29 16:43:37 +08:00
}
-(void)thirdLoginByLine:(UIViewController *)presentingViewController {
PILineLoginManager *line = [PILineLoginManager getSharedInstance];
[XNDJTDDLoadingTool showOnlyView:kWindow];
[line loginLineFromController:presentingViewController completeWithError:^(LineLoginResultStatus loginStatus, NSString * _Nullable token, NSString * _Nullable userId, NSString * _Nullable emali, NSError * _Nullable error) {
if (loginStatus == LineLoginResultStatusSuccess) {
ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init];
NSString * openid = userId.length > 0 ? userId : @"";
NSString * access_token = token.length > 0 ? token : @"";
NSString * unionid = userId.length > 0 ? userId : @"";
userInfo.openid = openid;
userInfo.access_token = access_token;
userInfo.unionid = unionid;
[AccountInfoStorage instance].thirdUserInfo = userInfo;
[self loginWithThirdPartWithType:ThirdLoginType_Line];
} else if (loginStatus == LineLoginResultStatusCancelled) {
[XNDJTDDLoadingTool hideOnlyView:kWindow];
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")];
} else if (loginStatus == LineLoginResultStatusError) {
[XNDJTDDLoadingTool hideOnlyView:kWindow];
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
}
}];
2023-07-14 18:50:55 +08:00
}
2023-08-11 14:46:56 +08:00
-(void)thirdLoginByFBWithPresentingViewController:(UIViewController *)presentingViewController {
2023-08-14 17:16:30 +08:00
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
[FBSDKAccessToken setCurrentAccessToken:nil];
2023-08-11 14:46:56 +08:00
FBSDKLoginManager *manager = [[FBSDKLoginManager alloc] init];
2023-08-14 14:39:41 +08:00
[manager logOut];
2023-08-29 12:13:17 +08:00
[XNDJTDDLoadingTool showOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[manager logInWithPermissions:@[@"public_profile"]
fromViewController:presentingViewController
handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
if (error) {
2023-08-29 12:13:17 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
} else if (result.isCancelled) {
2023-08-29 12:13:17 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")];
} else {
2023-08-14 17:16:30 +08:00
ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init];
userInfo.openid = result.token.userID;
userInfo.access_token = result.token.tokenString;
userInfo.unionid = result.token.userID;
[AccountInfoStorage instance].thirdUserInfo = userInfo;
2023-08-29 12:13:17 +08:00
[self loginWithThirdPartWithType:ThirdLoginType_FB];
2023-08-14 14:39:41 +08:00
}
}];
2023-08-11 14:46:56 +08:00
}
2023-07-14 18:50:55 +08:00
-(void)thirdLoginByGoogleWithPresentingViewController:(UIViewController *)presentingViewController configuration:(GIDConfiguration *)configuration{
2023-09-22 20:23:33 +08:00
[GIDSignIn sharedInstance].configuration = configuration;
[GIDSignIn.sharedInstance signInWithPresentingViewController:presentingViewController completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
2023-08-14 14:39:41 +08:00
if (error != nil) {
if (error.code == kGIDSignInErrorCodeCanceled){
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")];
}else{
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
}
} else {
ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init];
2023-09-22 20:23:33 +08:00
NSString * openid = signInResult.user.userID;
NSString * access_token = signInResult.user.idToken.tokenString.length > 0 ? signInResult.user.idToken.tokenString : @"";
NSString * unionid = signInResult.user.userID;
userInfo.userName = signInResult.user.profile.name;
userInfo.avatarUrl = [[signInResult.user.profile imageURLWithDimension:60] absoluteString];
2023-08-14 14:39:41 +08:00
userInfo.openid = openid;
userInfo.access_token = access_token;
userInfo.unionid = unionid;
///
[AccountInfoStorage instance].thirdUserInfo = userInfo;
[self loginWithThirdGoogle];
}
}];
2023-09-22 20:23:33 +08:00
2023-07-14 18:50:55 +08:00
}
-(void)loginWithThirdGoogle{
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool showOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
NSString * openid = [AccountInfoStorage instance].thirdUserInfo.openid;
NSString * access_token = [AccountInfoStorage instance].thirdUserInfo.access_token;
NSString * unionid = [AccountInfoStorage instance].thirdUserInfo.unionid;
[Api loginWithThirdPart:[self createHttpCompletion:^(BaseModel * _Nonnull data) {
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
AccountModel * model = [AccountModel modelWithDictionary:data.data];
if (model != nil) {
[[AccountInfoStorage instance] saveAccountInfo:model];
[[self getView] loginSuccess];
2023-08-08 11:26:07 +08:00
[XPAdjustEvent loginEvent];
2023-08-14 14:39:41 +08:00
[[NSUserDefaults standardUserDefaults]setValue:@(ThirdLoginType_Gmail) forKey:@"kLoginSuccessType"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
2023-08-08 11:26:07 +08:00
2023-08-14 14:39:41 +08:00
}fail:^(NSInteger code, NSString * _Nullable msg) {
2023-08-14 19:03:26 +08:00
[XNDJTDDLoadingTool hideOnlyView:kWindow];
2023-08-14 14:39:41 +08:00
[[super getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
} showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)ThirdLoginType_Gmail]];
2023-07-14 18:50:55 +08:00
}
@end