YRLoginManager.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // YRLoginManager.m
  3. // UU_Ent
  4. //
  5. // Created by liujl on 2019/5/8.
  6. // Copyright © 2019 UAS. All rights reserved.
  7. //
  8. #import "YRLoginManager.h"
  9. #import "YRUserInfo.h"
  10. @implementation YRLoginManager
  11. +(void)autoLoginsuccess:(void(^)(void))success failure:(void(^)(NSString *errorStr))failure{
  12. NSString *phone = FORMAT(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userAccount"]);
  13. phone = [NSString encodeSeciWithStr:phone];
  14. NSString *pwd = FORMAT(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userPassword"]);
  15. pwd = [NSString encodeSeciWithStr:pwd];
  16. if (phone.length != 11 || pwd.length == 0) {
  17. if (failure) {
  18. failure(@"程序出错");
  19. }
  20. return;
  21. }
  22. //登录管理平台
  23. [self userLoginWithPhone:phone passWord:pwd success:^(AccountSigninResponse * _Nullable response) {
  24. //登录公司
  25. [self companyLoginWithCompanyInfo:[YRUserInfo shareManager].lastCompanyInfo success:^(SwitchCompanyResponse * _Nullable response) {
  26. if (success) {
  27. success();
  28. }
  29. } failure:^(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error) {
  30. NSString *errStr = response.responseHeader.message;
  31. if (errStr.length == 0) {
  32. errStr = @"服务器异常";
  33. }
  34. if (failure) {
  35. failure(errStr);
  36. }
  37. }];
  38. } failure:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
  39. NSString *errStr = response.responseHeader.message;
  40. if (errStr.length == 0) {
  41. errStr = @"服务器异常";
  42. }
  43. if (failure) {
  44. failure(errStr);
  45. }
  46. }];
  47. }
  48. /**
  49. 用户账号密码登录
  50. */
  51. +(void)userLoginWithPhone:(NSString *)phoneNumber passWord:(NSString *)pwd success:(void(^)(AccountSigninResponse * _Nullable response))success failure:(void(^)(AccountSigninResponse * _Nullable response, NSError * _Nullable error))failure{
  52. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  53. AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
  54. AccountSigninRequest *request = [AccountSigninRequest message];
  55. DeviceInfo *model = [DeviceInfo new];
  56. model.os = DeviceInfo_OS_Ios;
  57. request.deviceInfo = model;
  58. request.mobile = phoneNumber;
  59. request.password = pwd;
  60. [YRUserInfo shareManager].phoneNumber = phoneNumber;
  61. [client signinWithRequest:request handler:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
  62. if (response.responseHeader.success) {
  63. NSDictionary *resDic = response.mj_keyValues;
  64. [YRUserInfo shareManager].token = response.authedToken.token;
  65. [YRUserInfo shareManager].userInfo = resDic;
  66. [self saveUserDataToNSUserDefaultsWithPhone:phoneNumber password:pwd];
  67. if (response.companyArray.count == 1) {
  68. //登录成功之后默认设置为自动登录
  69. [YRUserInfo shareManager].autoLogin = YES;
  70. [YRUserInfo shareManager].userCompanyInfo = resDic;
  71. [YRUserInfo shareManager].currCompanyInfo = response.companyArray[0].mj_keyValues;
  72. [YRUserInfo shareManager].lastCompanyInfo = response.companyArray[0].mj_keyValues;
  73. }
  74. if (success) {
  75. success(response);
  76. }
  77. }else{
  78. if (failure) {
  79. failure(response,error);
  80. }
  81. }
  82. }];
  83. }
  84. /**
  85. 选择公司登录
  86. */
  87. +(void)companyLoginWithCompanyInfo:(NSDictionary *)company success:(void(^)(SwitchCompanyResponse * _Nullable response))success failure:(void(^)(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error))failure{
  88. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  89. AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
  90. SwitchCompanyRequest *request = [SwitchCompanyRequest message];
  91. request.companyId = [[company objectForKey:@"id_p"] integerValue];
  92. GRPCProtoCall *call = [client RPCToswitchCompanyWithRequest:request handler:^(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error) {
  93. if (response.responseHeader.success) {
  94. NSDictionary *resDic = response.mj_keyValues;
  95. [YRUserInfo shareManager].token = response.authedToken.token;
  96. [YRUserInfo shareManager].userCompanyInfo = resDic;
  97. [YRUserInfo shareManager].currCompanyInfo = company;
  98. [YRUserInfo shareManager].lastCompanyInfo = company;
  99. //登录成功之后默认设置为自动登录
  100. [YRUserInfo shareManager].autoLogin = YES;
  101. if (success) {
  102. success(response);
  103. }
  104. }else{
  105. if (failure) {
  106. failure(response,error);
  107. }
  108. }
  109. }];
  110. call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
  111. [call start];
  112. }
  113. /**
  114. 退出登录
  115. */
  116. +(void)userLogout:(void(^)(AccountSignoutResponse * _Nullable response))success failure:(void(^)(AccountSignoutResponse * _Nullable response, NSError * _Nullable error))failure{
  117. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  118. AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
  119. AccountSignoutRequest *request = [AccountSignoutRequest message];
  120. GRPCProtoCall *call = [client RPCTosignoutWithRequest:request handler:^(AccountSignoutResponse * _Nullable response, NSError * _Nullable error) {
  121. if (response.responseHeader.success) {
  122. [YRUserInfo shareManager].autoLogin = NO;
  123. if (success) {
  124. success(response);
  125. }
  126. }else{
  127. if (failure) {
  128. failure(response,error);
  129. }
  130. }
  131. }];
  132. call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
  133. [call start];
  134. }
  135. #pragma mark --- 保存账号密码进沙盒
  136. +(void)saveUserDataToNSUserDefaultsWithPhone:(NSString *)phoneNumber password:(NSString *)pwd
  137. {
  138. phoneNumber = FORMAT(@"%@",[NSString codeSeciWithStr:phoneNumber]);
  139. pwd = FORMAT(@"%@",[NSString codeSeciWithStr:pwd]);
  140. [[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"userAccount"];
  141. [[NSUserDefaults standardUserDefaults] setObject:pwd forKey:@"userPassword"];
  142. }
  143. @end