| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- //
- // YRLoginManager.m
- // UU_Ent
- //
- // Created by liujl on 2019/5/8.
- // Copyright © 2019 UAS. All rights reserved.
- //
- #import "YRLoginManager.h"
- #import "YRUserInfo.h"
- @implementation YRLoginManager
- +(void)autoLoginsuccess:(void(^)(void))success failure:(void(^)(NSString *errorStr))failure{
-
- NSString *phone = FORMAT(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userAccount"]);
-
- phone = [NSString encodeSeciWithStr:phone];
-
- NSString *pwd = FORMAT(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userPassword"]);
-
- pwd = [NSString encodeSeciWithStr:pwd];
-
- if (phone.length != 11 || pwd.length == 0) {
-
- if (failure) {
-
- failure(@"程序出错");
-
- }
-
- return;
-
- }
- //登录管理平台
- [self userLoginWithPhone:phone passWord:pwd success:^(AccountSigninResponse * _Nullable response) {
-
- //登录公司
- [self companyLoginWithCompanyInfo:[YRUserInfo shareManager].lastCompanyInfo success:^(SwitchCompanyResponse * _Nullable response) {
-
- if (success) {
-
- success();
-
- }
-
- } failure:^(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error) {
-
- NSString *errStr = response.responseHeader.message;
-
- if (errStr.length == 0) {
-
- errStr = @"服务器异常";
-
- }
-
- if (failure) {
-
- failure(errStr);
-
- }
-
- }];
-
-
- } failure:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
-
- NSString *errStr = response.responseHeader.message;
-
- if (errStr.length == 0) {
-
- errStr = @"服务器异常";
-
- }
-
- if (failure) {
-
- failure(errStr);
-
- }
-
- }];
-
- }
- /**
- 用户账号密码登录
- */
- +(void)userLoginWithPhone:(NSString *)phoneNumber passWord:(NSString *)pwd success:(void(^)(AccountSigninResponse * _Nullable response))success failure:(void(^)(AccountSigninResponse * _Nullable response, NSError * _Nullable error))failure{
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
-
- AccountSigninRequest *request = [AccountSigninRequest message];
-
- DeviceInfo *model = [DeviceInfo new];
-
- model.os = DeviceInfo_OS_Ios;
-
- request.deviceInfo = model;
-
- request.mobile = phoneNumber;
-
- request.password = pwd;
-
- [YRUserInfo shareManager].phoneNumber = phoneNumber;
-
- [client signinWithRequest:request handler:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
-
-
- if (response.responseHeader.success) {
-
- NSDictionary *resDic = response.mj_keyValues;
-
- [YRUserInfo shareManager].token = response.authedToken.token;
-
- [YRUserInfo shareManager].userInfo = resDic;
-
- [self saveUserDataToNSUserDefaultsWithPhone:phoneNumber password:pwd];
-
- if (response.companyArray.count == 1) {
-
- //登录成功之后默认设置为自动登录
- [YRUserInfo shareManager].autoLogin = YES;
-
- [YRUserInfo shareManager].userCompanyInfo = resDic;
-
- [YRUserInfo shareManager].currCompanyInfo = response.companyArray[0].mj_keyValues;
-
- [YRUserInfo shareManager].lastCompanyInfo = response.companyArray[0].mj_keyValues;
-
- }
-
- if (success) {
-
- success(response);
-
- }
-
- }else{
-
- if (failure) {
-
- failure(response,error);
-
- }
-
- }
-
-
- }];
-
-
-
- }
- /**
- 选择公司登录
- */
- +(void)companyLoginWithCompanyInfo:(NSDictionary *)company success:(void(^)(SwitchCompanyResponse * _Nullable response))success failure:(void(^)(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error))failure{
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
-
- SwitchCompanyRequest *request = [SwitchCompanyRequest message];
-
- request.companyId = [[company objectForKey:@"id_p"] integerValue];
-
- GRPCProtoCall *call = [client RPCToswitchCompanyWithRequest:request handler:^(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error) {
-
- if (response.responseHeader.success) {
-
- NSDictionary *resDic = response.mj_keyValues;
-
- [YRUserInfo shareManager].token = response.authedToken.token;
-
- [YRUserInfo shareManager].userCompanyInfo = resDic;
-
- [YRUserInfo shareManager].currCompanyInfo = company;
-
- [YRUserInfo shareManager].lastCompanyInfo = company;
- //登录成功之后默认设置为自动登录
- [YRUserInfo shareManager].autoLogin = YES;
-
- if (success) {
-
- success(response);
-
- }
-
- }else{
-
- if (failure) {
-
- failure(response,error);
-
- }
-
-
- }
- }];
-
- call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
-
- [call start];
-
- }
- /**
- 退出登录
- */
- +(void)userLogout:(void(^)(AccountSignoutResponse * _Nullable response))success failure:(void(^)(AccountSignoutResponse * _Nullable response, NSError * _Nullable error))failure{
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
-
- AccountSignoutRequest *request = [AccountSignoutRequest message];
-
- GRPCProtoCall *call = [client RPCTosignoutWithRequest:request handler:^(AccountSignoutResponse * _Nullable response, NSError * _Nullable error) {
-
- if (response.responseHeader.success) {
-
- [YRUserInfo shareManager].autoLogin = NO;
-
- if (success) {
-
- success(response);
-
- }
-
- }else{
-
- if (failure) {
-
- failure(response,error);
-
- }
-
-
- }
-
- }];
-
- call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
-
- [call start];
-
- }
- #pragma mark --- 保存账号密码进沙盒
- +(void)saveUserDataToNSUserDefaultsWithPhone:(NSString *)phoneNumber password:(NSString *)pwd
- {
- phoneNumber = FORMAT(@"%@",[NSString codeSeciWithStr:phoneNumber]);
-
- pwd = FORMAT(@"%@",[NSString codeSeciWithStr:pwd]);
-
- [[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"userAccount"];
-
- [[NSUserDefaults standardUserDefaults] setObject:pwd forKey:@"userPassword"];
- }
- @end
|