// // RQUserManager.m // BuckDriverStudent // // Created by 张嵘 on 2019/10/24. // Copyright © 2019 RONG. All rights reserved. // #import "RQUserManager.h" //#import "RQHTTPService+RQUpdateUser.h" #import "RQLoginViewModel.h" #include /// 用户信息的名称 static NSString * const RQUserDataFileName = @"senba_empty_user.data"; /// 用户数据配置完成 NSString *const RQUserDataConfigureCompleteNotification = @"RQUserDataConfigureCompleteNotification"; /// 用户数据配置完成,取出userInfo 数据的的key NSString *const RQUserDataConfigureCompleteUserInfoKey = @"RQUserDataConfigureCompleteUserInfoKey"; @interface RQUserManager () /// currentLoginUser @property (nonatomic, readwrite, strong) RQUserModel *currentUser; @property (nonatomic, readwrite, assign) BOOL isObserve; @property (nonatomic, readwrite, strong) RQLoginViewModel *loginViewModel; @property (nonatomic, strong) NSArray *cityData; @property (nonatomic, strong) NSArray *cityCodeData; @end @implementation RQUserManager static id rq_userManager = nil; #pragma mark - init + (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ rq_userManager = [[self alloc] init]; }); return rq_userManager; } - (instancetype)init { if (self = [super init]) { } return self; } #pragma mark - Private Method - (void)saveUser:(RQUserModel *)user { /// 记录用户数据 self.currentUser = user; /// 保存 BOOL status = [NSKeyedArchiver archiveRootObject:user toFile:RQFilePathFromWeChatDoc(RQUserDataFileName)]; NSLog(@"Save login user data, the status is %@",status?@"Success...":@"Failure..."); if (_currentUser && !_isObserve) { [self beginObserveUser]; } } - (void)deleteUser:(RQUserModel *)user{ /// 删除 self.currentUser = nil; BOOL status = [RQFileManager removeFile:RQFilePathFromWeChatDoc(RQUserDataFileName)]; NSLog(@"Delete login user data , the status is %@",status?@"Success...":@"Failure..."); self.currentUser = nil; _isObserve = !status; } - (RQUserModel *)currentUser { if (!_currentUser) { _currentUser = [NSKeyedUnarchiver unarchiveObjectWithFile:RQFilePathFromWeChatDoc(RQUserDataFileName) exception:nil]; if (_currentUser && !_isObserve) { [self beginObserveUser]; } } return _currentUser; } /// 是否登录 - (BOOL)isLogin { if (RQObjectIsNil(self.currentUser)) { return NO; } else { return YES; } } /// 是否需要登录 - (BOOL)isShouldLogin { if (RQObjectIsNil(self.currentUser)) { if (![RQControllerHelper.currentViewController isKindOfClass:[RQLoginViewController class]]) { [RQ_APPDELEGATE.services pushViewModel:self.loginViewModel animated:YES]; } return NO; } else { return YES; } } /// 是否开启活体检测 0-不开启 1-开启 - (NSInteger)isycbd { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.isycbd)? [RQ_USER_MANAGER.currentUser.isycbd integerValue] : 0) : NO; } /// 活体检测比对动作数量 - (NSInteger)ycbdFaceCount { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.ycbdFaceCount)? [RQ_USER_MANAGER.currentUser.ycbdFaceCount integerValue] : 0) : NO; } /// sfzb 是否开启作弊 1开启 0不开启 - (NSInteger)sfzb { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.sfzb)? [RQ_USER_MANAGER.currentUser.sfzb integerValue] : 0) : NO; } /// qzgx 是否强制更新 1开启 0不开启 - (NSInteger)qzgx { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzgx)? [RQ_USER_MANAGER.currentUser.qzgx integerValue] : 0) : NO; } /// 验证等级 0 不验证 1 宽松 2 严格 3 困难 - (NSInteger)verifyLevel { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.verifyLevel)? [RQ_USER_MANAGER.currentUser.verifyLevel integerValue] : 0) : NO; } /// 实操扫码验证地市 1 开启 0 关闭 - (BOOL)isOpenScan { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.isOpenScan)? [RQ_USER_MANAGER.currentUser.isOpenScan boolValue] : NO) : NO; } /// qzVideo:1强制观看 0无需观看 - (BOOL)isQzVideo { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzVideo)? [RQ_USER_MANAGER.currentUser.qzVideo boolValue] : NO) : NO; } /// 11和12类型的就是从业 其他的或者为空的就是普通 - (NSInteger)busitype { return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.busitype)? [RQ_USER_MANAGER.currentUser.busitype integerValue] : 0) : 0; } - (void)setTryLookCount:(NSInteger)tryLookCount { NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id]; [RQ_COMMON_MANAGER saveObjectWithObject:[NSNumber numberWithInteger:tryLookCount] ForKey:key]; } - (NSInteger)tryLookCount { NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id]; id object = [RQ_COMMON_MANAGER getObjectWithKey:key]; if (RQObjectIsNil(object)) { return 4; } else { NSInteger tryLookCount = [object integerValue]; return tryLookCount; } } /// 是否从业考核 - (BOOL)isCykh { // return (self.busitype == 11 || self.busitype == 12); return ([self.currentUser.carType isEqualToString:@"A2"] || [self.currentUser.carType isEqualToString:@"B2"] || self.busitype == 11 || self.busitype == 12); //2023-06-06 新需求 } - (void)loginUser:(RQUserModel *)user{ _city_dbId = nil; /// 保存用户 [self saveUser:user]; /// 发送登录成功的通知 [self postUserDataConfigureCompleteNotification]; /// 设置别名 // [SBJPushService setAlias]; } /// 退出登录 - (void)logoutUser{ RQUserModel *currentUser = [self currentUser]; currentUser.channel = RQUserLoginChannelTypeDefault; [self saveUser:currentUser]; /// 删除别名 // [SBJPushService deleteAlias]; // // /// 删除token // [self deleteToken]; RQ_VIP_Module.isVip = NO; RQ_VIP_Module.isADVip = NO; RQ_VIP_Module.isSubject1Vip = NO; RQ_VIP_Module.isSubject2Vip = NO; RQ_VIP_Module.isSubject3Vip = NO; RQ_VIP_Module.isSubject4Vip = NO; RQ_VIP_Module.isSubjectAllVip = NO; RQ_VIP_Module.videoVipModel = nil; _city_dbId = nil; NSInteger localVersion = [[RQ_APP_VERSION stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue]; [[RQ_HTTP_Service getJsjpSet] subscribeNext:^(NSArray *setArr) { [setArr.rac_sequence.signal subscribeNext:^(RQDictInfoModel *dictInfoModel) { if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_OPEN"]) { RQ_COMMON_MANAGER.JSJP_APP_AD_OPEN = [[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] isEqualToString:@"1"]? YES : NO; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_CYCLE"]) { RQ_COMMON_MANAGER.JSJP_APP_AD_CYCLE = [dictInfoModel.dictValue integerValue]; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Image"]) { RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Image = [dictInfoModel.dictValue boolValue]; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Video"]) { RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Video = [dictInfoModel.dictValue boolValue]; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Online_Version"]) { NSInteger customOnlineVersion = [[[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue]; RQ_COMMON_MANAGER.APP_SWITCH = (localVersion >= customOnlineVersion); RQ_COMMON_MANAGER.APP_SWITCH = NO; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_ICON_NEED_CHANGE"]) { RQ_COMMON_MANAGER.JSJP_APP_ICON_NEED_CHANGE = [dictInfoModel.dictValue boolValue]; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_RATE"]) { RQ_COMMON_MANAGER.JSJP_APP_AD_RATE = [dictInfoModel.dictValue integerValue]; } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_PTSC_FBL"]) { RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL = dictInfoModel.dictValue; } }]; }]; /// 删除用户数据 [self deleteUser:currentUser]; } /// 用户信息配置完成 - (void)postUserDataConfigureCompleteNotification{ RQUserModel *user = [self currentUser]; [RQNotificationCenter postNotificationName:RQUserDataConfigureCompleteNotification object:nil userInfo:@{RQUserDataConfigureCompleteUserInfoKey:user}]; } - (void)beginObserveUser { _isObserve = YES; [[RACSignal combineLatest:@[[RACObserve(self.currentUser, nickName) distinctUntilChanged], [RACObserve(self.currentUser, userName) distinctUntilChanged], [RACObserve(self.currentUser, sex) distinctUntilChanged], [RACObserve(self.currentUser, birthday) distinctUntilChanged], [RACObserve(self.currentUser, carType) distinctUntilChanged], [RACObserve(self.currentUser, email) distinctUntilChanged], [RACObserve(self.currentUser, address) distinctUntilChanged], [RACObserve(self.currentUser, password) distinctUntilChanged]] reduce:^(NSString *nickName, NSString *userName, NSString *sex, NSString *birthday, NSString *carType, NSString *email, NSString *address, NSString *password) { return self.currentUser; }] subscribeNext:^(RQUserModel *userModel) { [self updateUserData]; }]; } /// 比对本地网络数据 - (void)updateUserData { // [[RQ_HTTP_Service updateUserWithID:self.currentUser.id userName:self.currentUser.userName sex:self.currentUser.sex birthday:self.currentUser.birthday email:self.currentUser.email address:self.currentUser.address nickName:self.currentUser.nickName password:self.currentUser.password photoContent:self.currentUser.photoContent carType:self.currentUser.carType idCard:self.currentUser.idCard] subscribeNext:^(RQUserModel *userModel) { // userModel.channel = RQ_USER_MANAGER.currentUser.channel; // [self willChangeValueForKey:@"user"]; // /// user模型的数据 重置,但是user的 指针地址不变 // [self.currentUser mergeValuesForKeysFromModel:userModel]; // [self didChangeValueForKey:@"user"]; // } error:^(NSError * _Nullable error) { // [MBProgressHUD rq_showErrorTips:error]; // }]; } - (void)checkVersionWithComplete:(VoidBlock_Bool)complete { /// 1: open 2: close // [[RQ_HTTP_Service getConfigKeyWithPath:RQButtonSwitchOriginalPath] subscribeNext:^(NSString *isOpenStr) { // if ([isOpenStr isEqualToString:@"1"]) { // complete(YES); // } else { // complete(NO); // } // } error:^(NSError * _Nullable error) { // complete(NO); // }]; } /// 获取了dict value - (id)getParamsKey:(NSString *)key { id value = nil; if(RQ_USER_MANAGER.currentUser.params){ for (ParamsItem *item in RQ_USER_MANAGER.currentUser.params) { if ([item.KEY isEqualToString:key]) { value = item.VALUE; break; } } } return value; } /// 获取了 db cityid - (id)getCityidByCode:(NSString *)code { NSString *cityid = @"0"; NSString *city = @""; NSArray *cityCodeArray = self.cityCodeData; if(cityCodeArray){ for (int i=0; i0){ cityid = [self getCityidByName:city]; } return cityid; } - (id)getCityidByName:(NSString *)name { NSString *cityid = @"0"; NSArray *cityArray = self.cityData; if(cityArray){ NSDictionary *item = [self getRecursive:cityArray name:name]; if ([item isKindOfClass:[NSDictionary class]]) { NSString *cid = item[@"id"]; NSString *cname = item[@"name"]; cityid = cid; } } return cityid; } - (id)getRecursive:(NSArray *)cityArray name:(NSString *)name{ if(cityArray){ for (int i=0; i0){ id list = [self getRecursive:citys name:name]; if(list){ return list; } } } } } return nil; } #pragma mark - LazyLoad - (NSString *)city_dbId{ if(!_city_dbId){ _city_dbId = [self getCityidByCode:RQ_USER_MANAGER.currentUser.city]; // _city_dbId = [self getCityidByName:RQ_USER_MANAGER.currentUser.cityName]; } return _city_dbId; } - (NSArray *)cityCodeData{ if(!_cityCodeData){ @autoreleasepool { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"json"]; if (filePath) { NSError *error = nil; NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; if (!error) { NSError *jsonError = nil; NSArray *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError]; if (!jsonError && [jsonDictionary isKindOfClass:[NSArray class]]) { _cityCodeData = jsonDictionary; } else { NSLog(@"Error parsing JSON: %@", jsonError); } } else { NSLog(@"Error reading file: %@", error); } } else { NSLog(@"JSON file not found."); } } } return _cityCodeData; } - (NSArray *)cityData{ if(!_cityData){ @autoreleasepool { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DrCityInfoThree" ofType:@"json"]; if (filePath) { NSError *error = nil; NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; if (!error) { NSError *jsonError = nil; NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError]; if (!jsonError && [jsonDictionary isKindOfClass:[NSDictionary class]]) { _cityData = jsonDictionary[@"provinces"]; } else { NSLog(@"Error parsing JSON: %@", jsonError); } } else { NSLog(@"Error reading file: %@", error); } } else { NSLog(@"JSON file not found."); } } } return _cityData; } - (RQLoginViewModel *)loginViewModel { if (!_loginViewModel) { _loginViewModel = [[RQLoginViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:nil]; } return _loginViewModel; } @end