Files
peko-ios/YuMi/Modules/YMLogin/Presenter/LoginPresenter.m
eggmanQQQ 69bb1d7fad #1217
新功能:反馈页
2024-07-03 21:22:40 +08:00

345 lines
16 KiB
Objective-C

//
// 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"
#import "DESEncrypt.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import "YuMi-swift.h"
#import "FeedBackConfigModel.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) {
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<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{
[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] loginThirdPartSuccess];
[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] loginThirdPartSuccess];
[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]];
}
/// 获取手机的验证码
/// @param phone 手机号
/// @param type 类型
- (void)phoneSmsCode:(NSString *)phone type:(GetSmsType)type phoneAreaCode:(NSString *)phoneAreaCode {
NSString * desPhone = [DESEncrypt encryptUseDES:phone key:KeyWithType(KeyType_PasswordEncode)];
[Api phoneSmsCode:[self createHttpCompletion:^(id _Nonnull data) {
[[self getView] phoneSmsCodeSuccess];
}] mobile:desPhone type:[NSString stringWithFormat:@"%lu", (unsigned long)type] phoneAreaCode:phoneAreaCode];
}
/// 使用手机号和验证码登录
/// @param phone 手机号
/// @param code 验证码
- (void)loginWithPhone:(NSString *)phone code:(NSString *)code phoneAreaCode:(NSString *)phoneAreaCode{
NSString * desPhone = [DESEncrypt encryptUseDES:phone key:KeyWithType(KeyType_PasswordEncode)];
[Api loginWithCode:[self createHttpCompletion:^(BaseModel * _Nonnull data) {
AccountModel * accountModel = [AccountModel modelWithDictionary:data.data];
if (accountModel && accountModel.access_token.length > 0) {
[[AccountInfoStorage instance] saveAccountInfo:accountModel];
}
[[self getView] loginWithPhoenSuccess];
} fail:^(NSInteger code, NSString * _Nullable msg) {
[[self getView] loginFailWithMsg:msg];
} errorToast:YES] phone:desPhone code:code client_secret:@"uyzjdhds" version:@"1" client_id:@"erban-client" grant_type:@"password" phoneAreaCode:phoneAreaCode];
}
/// 使用手机号和密码登录
/// @param phone 手机号
/// @param password 验证码
- (void)loginWithPhone:(NSString *)phone password:(NSString *)password {
NSString * desPassword = [DESEncrypt encryptUseDES:password key:KeyWithType(KeyType_PasswordEncode)];
NSString * desPhone = [DESEncrypt encryptUseDES:phone key:KeyWithType(KeyType_PasswordEncode)];
[Api loginWithPassword:[self createHttpCompletion:^(BaseModel * _Nonnull data) {
AccountModel * accountModel = [AccountModel modelWithDictionary:data.data];
if (accountModel && accountModel.access_token.length > 0) {
[[AccountInfoStorage instance] saveAccountInfo:accountModel];
}
[[self getView] loginSuccess];
} fail:^(NSInteger code, NSString * _Nullable msg) {
[[self getView] loginFailWithMsg:msg];
} errorToast:YES] phone:desPhone password:desPassword client_secret:@"uyzjdhds" version:@"1" client_id:@"erban-client" grant_type:@"password"];
}
///反馈
- (void)loadFeedbackConfig:(void(^)(FeedBackConfigModel *model))success
failure:(void(^)(NSString *errorMessage))failure {
[Api loadFeedbackConfig:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
FeedBackConfigModel *model =[FeedBackConfigModel modelWithDictionary:data.data];
if (success) {
success(model);
}
} else {
if (failure) {
failure(msg);
}
}
}];
}
- (void)submitFeedback:(void(^)(void))success
failure:(void(^)(NSString *errorMessage))failure
type:(NSString *)type
desc:(NSString *)desc
photoURLString:(nullable NSString *)photoURLString
contact:(nullable NSString *)contact {
[Api commitFeedback:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
if (success) {
success();
}
} else {
if (failure) {
failure(msg);
}
}
}
type:type
desc:desc
screenUrl:photoURLString ? photoURLString : @""
contact:contact ? contact : @""];
}
@end