RQUserManager.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // RQUserManager.m
  3. // BuckDriverStudent
  4. //
  5. // Created by 张嵘 on 2019/10/24.
  6. // Copyright © 2019 RONG. All rights reserved.
  7. //
  8. #import "RQUserManager.h"
  9. //#import "RQHTTPService+RQUpdateUser.h"
  10. #import "RQLoginViewModel.h"
  11. #include <CommonCrypto/CommonCrypto.h>
  12. /// 用户信息的名称
  13. static NSString * const RQUserDataFileName = @"senba_empty_user.data";
  14. /// 用户数据配置完成
  15. NSString *const RQUserDataConfigureCompleteNotification = @"RQUserDataConfigureCompleteNotification";
  16. /// 用户数据配置完成,取出userInfo 数据的的key
  17. NSString *const RQUserDataConfigureCompleteUserInfoKey = @"RQUserDataConfigureCompleteUserInfoKey";
  18. @interface RQUserManager ()
  19. /// currentLoginUser
  20. @property (nonatomic, readwrite, strong) RQUserModel *currentUser;
  21. @property (nonatomic, readwrite, assign) BOOL isObserve;
  22. @property (nonatomic, readwrite, strong) RQLoginViewModel *loginViewModel;
  23. @property (nonatomic, strong) NSArray *cityData;
  24. @property (nonatomic, strong) NSArray *cityCodeData;
  25. @end
  26. @implementation RQUserManager
  27. static id rq_userManager = nil;
  28. #pragma mark - init
  29. + (instancetype)sharedInstance {
  30. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. rq_userManager = [[self alloc] init];
  33. });
  34. return rq_userManager;
  35. }
  36. - (instancetype)init {
  37. if (self = [super init]) {
  38. }
  39. return self;
  40. }
  41. #pragma mark - Private Method
  42. - (void)saveUser:(RQUserModel *)user {
  43. /// 记录用户数据
  44. self.currentUser = user;
  45. /// 保存
  46. BOOL status = [NSKeyedArchiver archiveRootObject:user toFile:RQFilePathFromWeChatDoc(RQUserDataFileName)];
  47. NSLog(@"Save login user data, the status is %@",status?@"Success...":@"Failure...");
  48. if (_currentUser && !_isObserve) {
  49. [self beginObserveUser];
  50. }
  51. }
  52. - (void)deleteUser:(RQUserModel *)user{
  53. /// 删除
  54. self.currentUser = nil;
  55. BOOL status = [RQFileManager removeFile:RQFilePathFromWeChatDoc(RQUserDataFileName)];
  56. NSLog(@"Delete login user data , the status is %@",status?@"Success...":@"Failure...");
  57. self.currentUser = nil;
  58. _isObserve = !status;
  59. }
  60. - (RQUserModel *)currentUser {
  61. if (!_currentUser) {
  62. _currentUser = [NSKeyedUnarchiver unarchiveObjectWithFile:RQFilePathFromWeChatDoc(RQUserDataFileName) exception:nil];
  63. if (_currentUser && !_isObserve) {
  64. [self beginObserveUser];
  65. }
  66. }
  67. return _currentUser;
  68. }
  69. /// 是否登录
  70. - (BOOL)isLogin {
  71. if (RQObjectIsNil(self.currentUser)) {
  72. return NO;
  73. } else {
  74. return YES;
  75. }
  76. }
  77. /// 是否需要登录
  78. - (BOOL)isShouldLogin {
  79. if (RQObjectIsNil(self.currentUser)) {
  80. if (![RQControllerHelper.currentViewController isKindOfClass:[RQLoginViewController class]]) {
  81. [RQ_APPDELEGATE.services pushViewModel:self.loginViewModel animated:YES];
  82. }
  83. return NO;
  84. } else {
  85. return YES;
  86. }
  87. }
  88. /// 是否开启活体检测 0-不开启 1-开启
  89. - (NSInteger)isycbd {
  90. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.isycbd)? [RQ_USER_MANAGER.currentUser.isycbd integerValue] : 0) : NO;
  91. }
  92. /// 活体检测比对动作数量
  93. - (NSInteger)ycbdFaceCount {
  94. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.ycbdFaceCount)? [RQ_USER_MANAGER.currentUser.ycbdFaceCount integerValue] : 0) : NO;
  95. }
  96. /// sfzb 是否开启作弊 1开启 0不开启
  97. - (NSInteger)sfzb {
  98. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.sfzb)? [RQ_USER_MANAGER.currentUser.sfzb integerValue] : 0) : NO;
  99. }
  100. /// qzgx 是否强制更新 1开启 0不开启
  101. - (NSInteger)qzgx {
  102. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzgx)? [RQ_USER_MANAGER.currentUser.qzgx integerValue] : 0) : NO;
  103. }
  104. /// 验证等级 0 不验证 1 宽松 2 严格 3 困难
  105. - (NSInteger)verifyLevel {
  106. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.verifyLevel)? [RQ_USER_MANAGER.currentUser.verifyLevel integerValue] : 0) : NO;
  107. }
  108. /// 实操扫码验证地市 1 开启 0 关闭
  109. - (BOOL)isOpenScan {
  110. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.isOpenScan)? [RQ_USER_MANAGER.currentUser.isOpenScan boolValue] : NO) : NO;
  111. }
  112. /// qzVideo:1强制观看 0无需观看
  113. - (BOOL)isQzVideo {
  114. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzVideo)? [RQ_USER_MANAGER.currentUser.qzVideo boolValue] : NO) : NO;
  115. }
  116. /// 11和12类型的就是从业 其他的或者为空的就是普通
  117. - (NSInteger)busitype {
  118. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.busitype)? [RQ_USER_MANAGER.currentUser.busitype integerValue] : 0) : 0;
  119. }
  120. - (void)setTryLookCount:(NSInteger)tryLookCount {
  121. NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id];
  122. [RQ_COMMON_MANAGER saveObjectWithObject:[NSNumber numberWithInteger:tryLookCount] ForKey:key];
  123. }
  124. - (NSInteger)tryLookCount {
  125. NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id];
  126. id object = [RQ_COMMON_MANAGER getObjectWithKey:key];
  127. if (RQObjectIsNil(object)) {
  128. return 4;
  129. } else {
  130. NSInteger tryLookCount = [object integerValue];
  131. return tryLookCount;
  132. }
  133. }
  134. /// 是否从业考核
  135. - (BOOL)isCykh {
  136. // return (self.busitype == 11 || self.busitype == 12);
  137. return ([self.currentUser.carType isEqualToString:@"A2"] || [self.currentUser.carType isEqualToString:@"B2"] ||
  138. self.busitype == 11 || self.busitype == 12); //2023-06-06 新需求
  139. }
  140. - (void)loginUser:(RQUserModel *)user{
  141. _city_dbId = nil;
  142. /// 保存用户
  143. [self saveUser:user];
  144. /// 发送登录成功的通知
  145. [self postUserDataConfigureCompleteNotification];
  146. /// 设置别名
  147. // [SBJPushService setAlias];
  148. }
  149. /// 退出登录
  150. - (void)logoutUser{
  151. RQUserModel *currentUser = [self currentUser];
  152. currentUser.channel = RQUserLoginChannelTypeDefault;
  153. [self saveUser:currentUser];
  154. /// 删除别名
  155. // [SBJPushService deleteAlias];
  156. //
  157. // /// 删除token
  158. // [self deleteToken];
  159. RQ_VIP_Module.isVip = NO;
  160. RQ_VIP_Module.isADVip = NO;
  161. RQ_VIP_Module.isSubject1Vip = NO;
  162. RQ_VIP_Module.isSubject2Vip = NO;
  163. RQ_VIP_Module.isSubject3Vip = NO;
  164. RQ_VIP_Module.isSubject4Vip = NO;
  165. RQ_VIP_Module.isSubjectAllVip = NO;
  166. RQ_VIP_Module.videoVipModel = nil;
  167. _city_dbId = nil;
  168. NSInteger localVersion = [[RQ_APP_VERSION stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  169. [[RQ_HTTP_Service getJsjpSet] subscribeNext:^(NSArray *setArr) {
  170. [setArr.rac_sequence.signal subscribeNext:^(RQDictInfoModel *dictInfoModel) {
  171. if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_OPEN"]) {
  172. RQ_COMMON_MANAGER.JSJP_APP_AD_OPEN = [[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] isEqualToString:@"1"]? YES : NO;
  173. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_XM_AD_RATE"]) {
  174. RQ_COMMON_MANAGER.JSJP_APP_XM_AD_RATE = [dictInfoModel.dictValue integerValue];
  175. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_CYCLE"]) {
  176. RQ_COMMON_MANAGER.JSJP_APP_AD_CYCLE = [dictInfoModel.dictValue integerValue];
  177. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Image"]) {
  178. RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Image = [dictInfoModel.dictValue boolValue];
  179. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Video"]) {
  180. RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Video = [dictInfoModel.dictValue boolValue];
  181. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Online_Version"]) {
  182. NSInteger customOnlineVersion = [[[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  183. RQ_COMMON_MANAGER.APP_SWITCH = (localVersion >= customOnlineVersion);
  184. RQ_COMMON_MANAGER.APP_SWITCH = NO;
  185. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_ICON_NEED_CHANGE"]) {
  186. RQ_COMMON_MANAGER.JSJP_APP_ICON_NEED_CHANGE = [dictInfoModel.dictValue boolValue];
  187. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_RATE"]) {
  188. RQ_COMMON_MANAGER.JSJP_APP_AD_RATE = [dictInfoModel.dictValue integerValue];
  189. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_PTSC_FBL"]) {
  190. RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL = dictInfoModel.dictValue;
  191. }
  192. }];
  193. }];
  194. /// 删除用户数据
  195. [self deleteUser:currentUser];
  196. }
  197. /// 用户信息配置完成
  198. - (void)postUserDataConfigureCompleteNotification{
  199. RQUserModel *user = [self currentUser];
  200. [RQNotificationCenter postNotificationName:RQUserDataConfigureCompleteNotification object:nil userInfo:@{RQUserDataConfigureCompleteUserInfoKey:user}];
  201. }
  202. - (void)beginObserveUser {
  203. _isObserve = YES;
  204. [[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) {
  205. return self.currentUser;
  206. }] subscribeNext:^(RQUserModel *userModel) {
  207. [self updateUserData];
  208. }];
  209. }
  210. /// 比对本地网络数据
  211. - (void)updateUserData {
  212. // [[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) {
  213. // userModel.channel = RQ_USER_MANAGER.currentUser.channel;
  214. // [self willChangeValueForKey:@"user"];
  215. // /// user模型的数据 重置,但是user的 指针地址不变
  216. // [self.currentUser mergeValuesForKeysFromModel:userModel];
  217. // [self didChangeValueForKey:@"user"];
  218. // } error:^(NSError * _Nullable error) {
  219. // [MBProgressHUD rq_showErrorTips:error];
  220. // }];
  221. }
  222. - (void)checkVersionWithComplete:(VoidBlock_Bool)complete {
  223. /// 1: open 2: close
  224. // [[RQ_HTTP_Service getConfigKeyWithPath:RQButtonSwitchOriginalPath] subscribeNext:^(NSString *isOpenStr) {
  225. // if ([isOpenStr isEqualToString:@"1"]) {
  226. // complete(YES);
  227. // } else {
  228. // complete(NO);
  229. // }
  230. // } error:^(NSError * _Nullable error) {
  231. // complete(NO);
  232. // }];
  233. }
  234. /// 获取了dict value
  235. - (id)getParamsKey:(NSString *)key {
  236. id value = nil;
  237. if(RQ_USER_MANAGER.currentUser.params){
  238. for (ParamsItem *item in RQ_USER_MANAGER.currentUser.params) {
  239. if ([item.KEY isEqualToString:key]) {
  240. value = item.VALUE;
  241. break;
  242. }
  243. }
  244. }
  245. return value;
  246. }
  247. /// 获取了 db cityid
  248. - (id)getCityidByCode:(NSString *)code
  249. {
  250. NSString *cityid = @"0";
  251. NSString *city = @"";
  252. NSArray *cityCodeArray = self.cityCodeData;
  253. if(cityCodeArray){
  254. for (int i=0; i<cityCodeArray.count; i++) {
  255. NSString *ccode = cityCodeArray[i][@"code"];
  256. NSString *cname = cityCodeArray[i][@"name"];
  257. if ([ccode isEqualToString:code]){
  258. city = cname;
  259. break;
  260. }
  261. }
  262. }
  263. if (city.length>0){
  264. cityid = [self getCityidByName:city];
  265. }
  266. return cityid;
  267. }
  268. - (id)getCityidByName:(NSString *)name
  269. {
  270. NSString *cityid = @"0";
  271. NSArray *cityArray = self.cityData;
  272. if(cityArray){
  273. NSDictionary *item = [self getRecursive:cityArray name:name];
  274. if ([item isKindOfClass:[NSDictionary class]]) {
  275. NSString *cid = item[@"id"];
  276. NSString *cname = item[@"name"];
  277. cityid = cid;
  278. }
  279. }
  280. return cityid;
  281. }
  282. - (id)getRecursive:(NSArray *)cityArray name:(NSString *)name{
  283. if(cityArray){
  284. for (int i=0; i<cityArray.count; i++) {
  285. NSString *cid = cityArray[i][@"id"];
  286. NSString *cname = cityArray[i][@"name"];
  287. NSArray *citys = cityArray[i][@"cities"];
  288. NSLog(@"cityid = %@ and cityname = %@",cid,cname);
  289. if ([cname containsString:@"市"]){
  290. if ([cname isEqualToString:name]){
  291. return cityArray[i];
  292. }
  293. }else{
  294. if (citys!=nil&&citys.count>0){
  295. id list = [self getRecursive:citys name:name];
  296. if(list){
  297. return list;
  298. }
  299. }
  300. }
  301. }
  302. }
  303. return nil;
  304. }
  305. #pragma mark - LazyLoad
  306. - (NSString *)city_dbId{
  307. if(!_city_dbId){
  308. _city_dbId = [self getCityidByCode:RQ_USER_MANAGER.currentUser.city];
  309. // _city_dbId = [self getCityidByName:RQ_USER_MANAGER.currentUser.cityName];
  310. }
  311. return _city_dbId;
  312. }
  313. - (NSArray *)cityCodeData{
  314. if(!_cityCodeData){
  315. @autoreleasepool {
  316. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"json"];
  317. if (filePath) {
  318. NSError *error = nil;
  319. NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
  320. if (!error) {
  321. NSError *jsonError = nil;
  322. NSArray *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
  323. if (!jsonError && [jsonDictionary isKindOfClass:[NSArray class]]) {
  324. _cityCodeData = jsonDictionary;
  325. } else {
  326. NSLog(@"Error parsing JSON: %@", jsonError);
  327. }
  328. } else {
  329. NSLog(@"Error reading file: %@", error);
  330. }
  331. } else {
  332. NSLog(@"JSON file not found.");
  333. }
  334. }
  335. }
  336. return _cityCodeData;
  337. }
  338. - (NSArray *)cityData{
  339. if(!_cityData){
  340. @autoreleasepool {
  341. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DrCityInfoThree" ofType:@"json"];
  342. if (filePath) {
  343. NSError *error = nil;
  344. NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
  345. if (!error) {
  346. NSError *jsonError = nil;
  347. NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
  348. if (!jsonError && [jsonDictionary isKindOfClass:[NSDictionary class]]) {
  349. _cityData = jsonDictionary[@"provinces"];
  350. } else {
  351. NSLog(@"Error parsing JSON: %@", jsonError);
  352. }
  353. } else {
  354. NSLog(@"Error reading file: %@", error);
  355. }
  356. } else {
  357. NSLog(@"JSON file not found.");
  358. }
  359. }
  360. }
  361. return _cityData;
  362. }
  363. - (RQLoginViewModel *)loginViewModel {
  364. if (!_loginViewModel) {
  365. _loginViewModel = [[RQLoginViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:nil];
  366. }
  367. return _loginViewModel;
  368. }
  369. @end