// // LoginPresenter.m // YUMI // // Created by zu on 2021/9/1. // #import "LoginPresenter.h" ///Third #import #import ///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" #import #import @implementation LoginPresenter - (void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self]; } - (instancetype)init{ self = [super init]; if(self){ [FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; [[NSNotificationCenter defaultCenter] addObserverForName:FBSDKProfileDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock: ^(NSNotification *notification) { if ([FBSDKProfile currentProfile]) { //获取当前用户名 [FBSDKProfile loadCurrentProfileWithCompletion: ^(FBSDKProfile *profile, NSError *error) { if (profile) { ThirdUserInfo * userInfo = [[ThirdUserInfo alloc] init]; NSString * openid = profile.userID; NSString * access_token = profile.userID; NSString * unionid = profile.userID; userInfo.openid = openid; userInfo.access_token = access_token; userInfo.unionid = unionid; userInfo.userName = profile.name; userInfo.avatarUrl = profile.imageURL.absoluteString; [AccountInfoStorage instance].thirdUserInfo = userInfo; [self loginWithThirdPartWithType:ThirdLoginType_FB]; } }]; } }]; } return self; } - (id)getView { return ((id) [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]; [XPAdjustEvent loginEvent]; [[NSUserDefaults standardUserDefaults]setValue:@(type) forKey:@"kLoginSuccessType"]; [[NSUserDefaults standardUserDefaults]synchronize]; } } 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)thirdLoginByFBWithPresentingViewController:(UIViewController *)presentingViewController { FBSDKLoginManager *manager = [[FBSDKLoginManager alloc] init]; [manager logOut]; [manager logInWithPermissions:@[@"public_profile"] fromViewController:presentingViewController handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) { if (error) { [[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")]; } else if (result.isCancelled) { [[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")]; } else { NSLog(@"Logged in"); } }]; } -(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]; [XPAdjustEvent loginEvent]; [[NSUserDefaults standardUserDefaults]setValue:@(ThirdLoginType_Gmail) forKey:@"kLoginSuccessType"]; [[NSUserDefaults standardUserDefaults]synchronize]; } }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