// // 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 #import "YuMi-swift.h" @implementation LoginPresenter - (void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self]; } - (instancetype)init{ self = [super init]; if(self){ @weakify(self); [[NSNotificationCenter defaultCenter] addObserverForName:FBSDKProfileDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock: ^(NSNotification *notification) { @strongify(self); if ([FBSDKProfile currentProfile]) { //获取当前用户名 [FBSDKProfile loadCurrentProfileWithCompletion: ^(FBSDKProfile *profile, NSError *error) { if (profile) { [AccountInfoStorage instance].thirdUserInfo.userName = profile.name; [AccountInfoStorage instance].thirdUserInfo.avatarUrl = profile.imageURL.absoluteString; } }]; } }]; } 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{ [XNDJTDDLoadingTool showOnlyView:kWindow]; 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]; [XNDJTDDLoadingTool hideOnlyView:kWindow]; [[NSUserDefaults standardUserDefaults]setValue:@(type) forKey:@"kLoginSuccessType"]; [[NSUserDefaults standardUserDefaults]synchronize]; } } fail:^(NSInteger code, NSString * _Nullable msg) { [XNDJTDDLoadingTool hideOnlyView:kWindow]; [[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")]; } showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)type]]; } -(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")]; } }]; } -(void)thirdLoginByFBWithPresentingViewController:(UIViewController *)presentingViewController { [FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; [FBSDKAccessToken setCurrentAccessToken:nil]; FBSDKLoginManager *manager = [[FBSDKLoginManager alloc] init]; [manager logOut]; [XNDJTDDLoadingTool showOnlyView:kWindow]; [manager logInWithPermissions:@[@"public_profile"] fromViewController:presentingViewController handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) { if (error) { [XNDJTDDLoadingTool hideOnlyView:kWindow]; [[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")]; } else if (result.isCancelled) { [XNDJTDDLoadingTool hideOnlyView:kWindow]; [[self getView] showErrorToast:YMLocalizedString(@"LoginPresenter0")]; } else { 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; [self loginWithThirdPartWithType:ThirdLoginType_FB]; } }]; } -(void)thirdLoginByGoogleWithPresentingViewController:(UIViewController *)presentingViewController configuration:(GIDConfiguration *)configuration{ [GIDSignIn sharedInstance].configuration = configuration; [GIDSignIn.sharedInstance signInWithPresentingViewController:presentingViewController completion:^(GIDSignInResult * _Nullable signInResult, 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 = 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]; userInfo.openid = openid; userInfo.access_token = access_token; userInfo.unionid = unionid; ///保存一下第三方的值 [AccountInfoStorage instance].thirdUserInfo = userInfo; [self loginWithThirdGoogle]; } }]; } -(void)loginWithThirdGoogle{ [XNDJTDDLoadingTool showOnlyView:kWindow]; 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) { [XNDJTDDLoadingTool hideOnlyView:kWindow]; 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) { [XNDJTDDLoadingTool hideOnlyView:kWindow]; [[super getView] showErrorToast:YMLocalizedString(@"LoginPresenter1")]; } showLoading:YES] openid:openid unionid:unionid access_token:access_token type:[NSString stringWithFormat:@"%lu", (unsigned long)ThirdLoginType_Gmail]]; } @end