| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // RASingleton.m
- // Apex And Drivers
- //
- // Created by Jack on 2018/6/6.
- // Copyright © 2018年 USAI. All rights reserved.
- //
- #import "RASingleton.h"
- static RASingleton *singleton;
- @implementation RASingleton {
- NSString *_secretKey;
- }
- + (instancetype)sharedInstance {
-
- static dispatch_once_t tocken;
- dispatch_once(&tocken, ^{
- singleton = [[RASingleton alloc] init];
- singleton->_secretKey = @"usai";
- });
- return singleton;
- }
- #pragma mark - User
- - (NSString *)secretKey {
- return _secretKey;
- }
- - (NSString *)encryptUser {
-
- if (!self.user) {
- return nil;
- }
- return [AESCrypt encrypt:self.user password:self.secretKey];
- }
- - (NSString *)encryptPassword {
-
- if (!self.password) {
- return nil;
- }
- return [AESCrypt encrypt:self.password password:self.secretKey];
- }
- - (void)saveUserInfo {
- if (self.user && self.password) {
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults removeObjectForKey:@"user"];
- [defaults removeObjectForKey:@"password"];
-
- [defaults setValue:self.encryptUser forKey:@"user"];
- [defaults setValue:self.encryptPassword forKey:@"password"];
- [defaults setBool:TRUE forKey:@"autologin"];
- [defaults synchronize];
- }
- }
- - (NSString *)savedUser {
- NSString * user = [AESCrypt decrypt:[[NSUserDefaults standardUserDefaults] stringForKey:@"user"] password:self.secretKey];
- return user;
- }
- - (NSString *)savedPassword {
- NSString * password = [AESCrypt decrypt:[[NSUserDefaults standardUserDefaults] stringForKey:@"password"] password:self.secretKey];
- return password;
- }
- - (void)loginUser:(NSString *)user password:(NSString *)password {
- _user = user;
- _password = password;
-
- [self saveUserInfo];
-
- [self bindUserWithNotificationToken];
- }
- - (BOOL)autoLogin {
- _user = [self savedUser];
- _password = [self savedPassword];
-
- return _user.length && _password.length;
- }
- - (void)setNotificationToken:(NSString *)notificationToken {
- _notificationToken = notificationToken;
-
- [self bindUserWithNotificationToken];
- }
- - (void)bindUserWithNotificationToken {
-
- if (_user.length && _password.length && _notificationToken.length) {
- [RADataProvider bindNitificationToken:self.notificationToken];
- }
- }
- - (void)logout {
- _user = nil;
- _password = nil;
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults removeObjectForKey:@"user"];
- [defaults removeObjectForKey:@"password"];
- [defaults synchronize];
- }
- #pragma mark - Location
- - (void)setRequiredLocation:(BOOL)requiredLocation {
- _requiredLocation = requiredLocation;
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults setBool:_requiredLocation forKey:@"requiredLocation"];
- [defaults synchronize];
-
- [self sendRequiredLocationNotification];
- }
- - (void)loadSavedReuqiredLocation { // 启动的时候
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- _requiredLocation = [defaults objectForKey:@"requiredLocation"];
-
- [self sendRequiredLocationNotification];
- }
- - (void)sendRequiredLocationNotification {
-
- if (_requiredLocation) {
- [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationStartLocation object:nil];
- } else {
- [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationStopLocation object:nil];
- }
-
- }
- @end
|