登录,注销
- (void)logout { [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"usertoken"]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userid"]; [[NSUserDefaults standardUserDefaults] synchronize]; SCach *cach = [SCach shareInstance]; [cach removeFileData:@"loginMode" filePath:nil]; }
没有登录,切换 控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [LoginManager shareLoginManager]; NSString *strUid = [[LoginManager shareLoginManager] getUserId]; if(strUid){ rootvc = [[RootTabBarController alloc] init]; rootvc.view.frame = self.window.bounds; self.window.rootViewController = rootvc; } else{ [self setLoginViewController]; } [self.window makeKeyAndVisible]; return YES; }
@implementation LoginManager + (LoginManager *)shareLoginManager { static LoginManager *lm = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ lm = [[LoginManager alloc] init]; }); return lm; } - (instancetype)init { if(self = [super init]) { SCach *cach = [SCach shareInstance]; [cach valueAsynForKey:@"loginMode" isMemory:NO filePath:nil className:@"LoginMode" block:^(id reslutId) { self.logMode = reslutId; if(self.logMode.strUid) { [self setNetWorkParam:self.logMode.strUid userToke:self.logMode.strToken]; StoreMode *sm = [self.logMode.storeList objectAtIndex:0]; [self setNetWorkStoreId:sm.strId]; [self setCurrentStoreName:sm.strStoreName]; [self iosAppGetStoreList]; } }]; } return self; }
- (void)saveUserId:(NSString *)aUserId { self.strUserId = aUserId; [[NSUserDefaults standardUserDefaults] setObject:self.strUserId forKey:@"userid"]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)saveUserToken:(NSString *)aUserToken { self.strUserToken = aUserToken; [[NSUserDefaults standardUserDefaults] setObject:self.strUserToken forKey:@"usertoken"]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (NSString*)getUserId { if(!self.strUserId) { self.strUserId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userid"]; } return self.strUserId; } - (NSString*)getUserToken { if(!self.strUserToken) { self.strUserToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"userid"]; } return self.strUserToken; }
登录
- (void)login_request:(NSString *)aUserName pass:(NSString *)aPass retblock:(LOGINRESULTBLOCK)aBlock { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0]; #ifdef DEBUG [dict setValue:aUserName forKey:@"userName"]; [dict setValue:aPass forKey:@"userPassword"]; #else if(!aUserName || !aPass) { [SVProgressHUD showErrorWithStatus:@"请输入用户名或密码" duration:1.0f cover:NO offsetY:64]; return ; } [dict setValue:aUserName forKey:@"userName"]; [dict setValue:aPass forKey:@"userPassword"]; #endif [dict setValue:@"1" forKey:@"loginUserType"]; [NetManager requestWith:dict apiName:@"loginApp" method:@"POST" succ:^(NSDictionary *successDict) { NSString *msg = successDict[@"msg"]; if ([msg isEqualToString:@"success"]) { NSDictionary *resultDict = [successDict objectForKey:@"result"]; if(resultDict) { NSString *token = [resultDict objectForKey:@"token"]; [self saveUserToken:token]; NSString *uid = [resultDict objectForKey:@"uid"]; BaseIntToNSString(uid); [self saveUserId:uid]; if(!self.logMode) { self.logMode = [[LoginMode alloc] init]; } [self.logMode unPacketData:resultDict]; [self setNetWorkParam:self.strUserId userToke:self.strUserToken]; StoreMode *sm = [self.logMode.storeList objectAtIndex:0]; [self setCurrentStoreName:sm.strStoreName]; [self setNetWorkStoreId:sm.strId]; [self iosAppGetStoreList]; [[SCach shareInstance] setAsynValue:self.logMode key:@"loginMode" isMemeory:NO filePath:nil block:^(bool isResult) { }]; } aBlock(@"success"); }else aBlock(msg); } failure:^(NSDictionary *failDict, NSError *error) { aBlock(@"登陆失败"); }]; }