|
@@ -0,0 +1,208 @@
|
|
|
+//
|
|
|
+// 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"]);
|
|
|
+
|
|
|
+ NSString *pwd = FORMAT(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userPassword"]);
|
|
|
+
|
|
|
+ if (phone.length != 11 || pwd.length == 0) {
|
|
|
+
|
|
|
+ if (failure) {
|
|
|
+
|
|
|
+ failure(@"程序出错");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+ //登录管理平台
|
|
|
+ [self userLoginWithPhone:phone passWord:pwd success:^(AccountSigninResponse * _Nullable response) {
|
|
|
+ //只有一家公司,不用再选择账套
|
|
|
+ if (response.companyArray.count == 1) {
|
|
|
+
|
|
|
+ if (success) {
|
|
|
+
|
|
|
+ success();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ //登录公司
|
|
|
+ [self companyLoginWithCompanyInfo:[YRUserInfo shareManager].lastCompanyInfo success:^(SwitchCompanyResponse * _Nullable response) {
|
|
|
+
|
|
|
+ if (success) {
|
|
|
+
|
|
|
+ success();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } failure:^(SwitchCompanyResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+
|
|
|
+ if (failure) {
|
|
|
+
|
|
|
+ failure(response.responseHeader.message);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } failure:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+
|
|
|
+ if (failure) {
|
|
|
+
|
|
|
+ failure(response.responseHeader.message);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ 用户账号密码登录
|
|
|
+ */
|
|
|
++(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) {
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].masChanged = YES;
|
|
|
+
|
|
|
+ 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].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) {
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].masChanged = YES;
|
|
|
+
|
|
|
+ NSDictionary *resDic = response.mj_keyValues;
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].token = response.authedToken.token;
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].userCompanyInfo = resDic;
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].currCompanyInfo = company;
|
|
|
+
|
|
|
+ [YRUserInfo shareManager].lastCompanyInfo = company;
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+{
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:FORMAT(@"%@",phoneNumber) forKey:@"userAccount"];
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:FORMAT(@"%@",pwd) forKey:@"userPassword"];
|
|
|
+}
|
|
|
+
|
|
|
+@end
|