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

176 lines
6.4 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"
@implementation LoginPresenter
- (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{
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")];
}
}];
}
-(void)loginWithThirdPartWithType:(ThirdLoginType)type{
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];
}
} fail:^(NSInteger code, NSString * _Nullable msg) {
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
} showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)type]];
}
-(void)thirdLoginByGoogleWithPresentingViewController:(UIViewController *)presentingViewController configuration:(GIDConfiguration *)configuration{
[GIDSignIn.sharedInstance signInWithConfiguration:configuration
presentingViewController:presentingViewController
callback:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error != nil) {
if (error.code == kGIDSignInErrorCodeCanceled){
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")];
}else{
[[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
}
} else {
ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init];
NSString * openid = user.userID;
NSString * access_token = user.authentication.idToken.length > 0 ? user.authentication.idToken : @"";
NSString * unionid = user.userID;
userInfo.userName = user.profile.name;
userInfo.avatarUrl = [[user.profile imageURLWithDimension:60] absoluteString];
userInfo.openid = openid;
userInfo.access_token = access_token;
userInfo.unionid = unionid;
///
[AccountInfoStorage instance].thirdUserInfo = userInfo;
[self loginWithThirdGoogle];
}
}];
}
-(void)loginWithThirdGoogle{
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];
}
}fail:^(NSInteger code, NSString * _Nullable msg) {
[[super getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")];
} showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)ThirdLoginType_Gmail]];
}
@end