RQUserManager.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. - (NSInteger)scbdFaceCount {
  97. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.scbdFaceCount)? [RQ_USER_MANAGER.currentUser.scbdFaceCount integerValue] : 0) : NO;
  98. }
  99. /// sfzb 是否开启作弊 1开启 0不开启
  100. - (NSInteger)sfzb {
  101. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.sfzb)? [RQ_USER_MANAGER.currentUser.sfzb integerValue] : 0) : NO;
  102. }
  103. /// qzgx 是否强制更新 1开启 0不开启
  104. - (NSInteger)qzgx {
  105. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzgx)? [RQ_USER_MANAGER.currentUser.qzgx integerValue] : 0) : NO;
  106. }
  107. /// 验证等级 0 不验证 1 宽松 2 严格 3 困难
  108. - (NSInteger)verifyLevel {
  109. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.verifyLevel)? [RQ_USER_MANAGER.currentUser.verifyLevel integerValue] : 0) : NO;
  110. }
  111. /// 实操扫码验证地市 1 开启 0 关闭
  112. - (BOOL)isOpenScan {
  113. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.isOpenScan)? [RQ_USER_MANAGER.currentUser.isOpenScan boolValue] : NO) : NO;
  114. }
  115. /// qzVideo:1强制观看 0无需观看
  116. - (BOOL)isQzVideo {
  117. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.qzVideo)? [RQ_USER_MANAGER.currentUser.qzVideo boolValue] : NO) : NO;
  118. }
  119. /// 11和12类型的就是从业 其他的或者为空的就是普通
  120. - (NSInteger)busitype {
  121. return RQ_USER_MANAGER.currentUser? (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.busitype)? [RQ_USER_MANAGER.currentUser.busitype integerValue] : 0) : 0;
  122. }
  123. - (void)setTryLookCount:(NSInteger)tryLookCount {
  124. NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id];
  125. [RQ_COMMON_MANAGER saveObjectWithObject:[NSNumber numberWithInteger:tryLookCount] ForKey:key];
  126. }
  127. - (NSInteger)tryLookCount {
  128. NSString *key = [NSString stringWithFormat:@"RQTryLookCountKey-%@",RQ_USER_MANAGER.currentUser._id];
  129. id object = [RQ_COMMON_MANAGER getObjectWithKey:key];
  130. if (RQObjectIsNil(object)) {
  131. return 4;
  132. } else {
  133. NSInteger tryLookCount = [object integerValue];
  134. return tryLookCount;
  135. }
  136. }
  137. /// 是否从业考核
  138. - (BOOL)isCykh {
  139. // return (self.busitype == 11 || self.busitype == 12);
  140. return ([self.currentUser.carType isEqualToString:@"A2"] || [self.currentUser.carType isEqualToString:@"B2"] ||
  141. self.busitype == 11 || self.busitype == 12); //2023-06-06 新需求
  142. }
  143. - (void)loginUser:(RQUserModel *)user{
  144. _city_dbId = nil;
  145. /// 保存用户
  146. [self saveUser:user];
  147. /// 发送登录成功的通知
  148. [self postUserDataConfigureCompleteNotification];
  149. /// 设置别名
  150. // [SBJPushService setAlias];
  151. }
  152. /// 退出登录
  153. - (void)logoutUser{
  154. RQUserModel *currentUser = [self currentUser];
  155. currentUser.channel = RQUserLoginChannelTypeDefault;
  156. [self saveUser:currentUser];
  157. /// 删除别名
  158. // [SBJPushService deleteAlias];
  159. //
  160. // /// 删除token
  161. // [self deleteToken];
  162. RQ_VIP_Module.isVip = NO;
  163. RQ_VIP_Module.isADVip = NO;
  164. RQ_VIP_Module.isSubject1Vip = NO;
  165. RQ_VIP_Module.isSubject2Vip = NO;
  166. RQ_VIP_Module.isSubject3Vip = NO;
  167. RQ_VIP_Module.isSubject4Vip = NO;
  168. RQ_VIP_Module.isSubjectAllVip = NO;
  169. RQ_VIP_Module.videoVipModel = nil;
  170. _city_dbId = nil;
  171. NSInteger localVersion = [[RQ_APP_VERSION stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  172. [[RQ_HTTP_Service getJsjpSet] subscribeNext:^(NSArray *setArr) {
  173. [setArr.rac_sequence.signal subscribeNext:^(RQDictInfoModel *dictInfoModel) {
  174. if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_OPEN"]) {
  175. RQ_COMMON_MANAGER.JSJP_APP_AD_OPEN = [[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] isEqualToString:@"1"]? YES : NO;
  176. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_XM_AD_RATE"]) {
  177. RQ_COMMON_MANAGER.JSJP_APP_XM_AD_RATE = [dictInfoModel.dictValue integerValue];
  178. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_CYCLE"]) {
  179. RQ_COMMON_MANAGER.JSJP_APP_AD_CYCLE = [dictInfoModel.dictValue integerValue];
  180. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_iOS_PT"]) {
  181. RQ_COMMON_MANAGER.JSJP_iOS_PT = [dictInfoModel.dictValue integerValue];
  182. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_SHARE_CITY"]) {
  183. RQ_COMMON_MANAGER.JSJP_SHARE_CITY = dictInfoModel.dictValue;
  184. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_XCX_NAME"]) {
  185. RQ_COMMON_MANAGER.JSJP_XCX_NAME = dictInfoModel.dictValue;
  186. }else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_XCX_PAGE"]) {
  187. RQ_COMMON_MANAGER.JSJP_XCX_PAGE = dictInfoModel.dictValue;
  188. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Image"]) {
  189. RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Image = [dictInfoModel.dictValue boolValue];
  190. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Is_Online_Media_Video"]) {
  191. RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Video = [dictInfoModel.dictValue boolValue];
  192. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_Online_Version"]) {
  193. NSInteger customOnlineVersion = [[[NSString stringWithFormat:@"%@",dictInfoModel.dictValue] stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  194. RQ_COMMON_MANAGER.APP_SWITCH = (localVersion > customOnlineVersion);
  195. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_ICON_NEED_CHANGE"]) {
  196. RQ_COMMON_MANAGER.JSJP_APP_ICON_NEED_CHANGE = [dictInfoModel.dictValue boolValue];
  197. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_AD_RATE"]) {
  198. RQ_COMMON_MANAGER.JSJP_APP_AD_RATE = [dictInfoModel.dictValue integerValue];
  199. } else if ([dictInfoModel.dictLabel isEqualToString:@"JSJP_APP_PTSC_FBL"]) {
  200. RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL = dictInfoModel.dictValue;
  201. }
  202. }];
  203. }];
  204. /// 删除用户数据
  205. [self deleteUser:currentUser];
  206. }
  207. /// 用户信息配置完成
  208. - (void)postUserDataConfigureCompleteNotification{
  209. RQUserModel *user = [self currentUser];
  210. [RQNotificationCenter postNotificationName:RQUserDataConfigureCompleteNotification object:nil userInfo:@{RQUserDataConfigureCompleteUserInfoKey:user}];
  211. }
  212. - (void)beginObserveUser {
  213. _isObserve = YES;
  214. [[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) {
  215. return self.currentUser;
  216. }] subscribeNext:^(RQUserModel *userModel) {
  217. [self updateUserData];
  218. }];
  219. }
  220. /// 比对本地网络数据
  221. - (void)updateUserData {
  222. // [[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) {
  223. // userModel.channel = RQ_USER_MANAGER.currentUser.channel;
  224. // [self willChangeValueForKey:@"user"];
  225. // /// user模型的数据 重置,但是user的 指针地址不变
  226. // [self.currentUser mergeValuesForKeysFromModel:userModel];
  227. // [self didChangeValueForKey:@"user"];
  228. // } error:^(NSError * _Nullable error) {
  229. // [MBProgressHUD rq_showErrorTips:error];
  230. // }];
  231. }
  232. - (void)checkVersionWithComplete:(VoidBlock_Bool)complete {
  233. /// 1: open 2: close
  234. // [[RQ_HTTP_Service getConfigKeyWithPath:RQButtonSwitchOriginalPath] subscribeNext:^(NSString *isOpenStr) {
  235. // if ([isOpenStr isEqualToString:@"1"]) {
  236. // complete(YES);
  237. // } else {
  238. // complete(NO);
  239. // }
  240. // } error:^(NSError * _Nullable error) {
  241. // complete(NO);
  242. // }];
  243. }
  244. /// 获取了dict value
  245. - (id)getParamsKey:(NSString *)key {
  246. id value = nil;
  247. if(RQ_USER_MANAGER.currentUser.params){
  248. for (ParamsItem *item in RQ_USER_MANAGER.currentUser.params) {
  249. if ([item.KEY isEqualToString:key]) {
  250. value = item.VALUE;
  251. break;
  252. }
  253. }
  254. }
  255. return value;
  256. }
  257. /// 获取了 db cityid
  258. - (id)getCityidByCode:(NSString *)code
  259. {
  260. NSString *cityid = @"0";
  261. NSString *city = @"";
  262. NSArray *cityCodeArray = self.cityCodeData;
  263. if(cityCodeArray){
  264. for (int i=0; i<cityCodeArray.count; i++) {
  265. NSString *ccode = cityCodeArray[i][@"code"];
  266. NSString *cname = cityCodeArray[i][@"name"];
  267. if ([ccode isEqualToString:code]){
  268. city = cname;
  269. break;
  270. }
  271. }
  272. }
  273. if (city.length>0){
  274. cityid = [self getCityidByName:city];
  275. }
  276. return cityid;
  277. }
  278. - (id)getCityidByName:(NSString *)name
  279. {
  280. NSString *cityid = @"0";
  281. NSArray *cityArray = self.cityData;
  282. if(cityArray){
  283. NSDictionary *item = [self getRecursive:cityArray name:name];
  284. if ([item isKindOfClass:[NSDictionary class]]) {
  285. NSString *cid = item[@"id"];
  286. NSString *cname = item[@"name"];
  287. cityid = cid;
  288. }
  289. }
  290. return cityid;
  291. }
  292. - (id)getRecursive:(NSArray *)cityArray name:(NSString *)name{
  293. if(cityArray){
  294. for (int i=0; i<cityArray.count; i++) {
  295. NSString *cid = cityArray[i][@"id"];
  296. NSString *cname = cityArray[i][@"name"];
  297. NSArray *citys = cityArray[i][@"cities"];
  298. NSLog(@"cityid = %@ and cityname = %@",cid,cname);
  299. if ([cname containsString:@"市"]){
  300. if ([cname isEqualToString:name]){
  301. return cityArray[i];
  302. }
  303. }else{
  304. if (citys!=nil&&citys.count>0){
  305. id list = [self getRecursive:citys name:name];
  306. if(list){
  307. return list;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. return nil;
  314. }
  315. #pragma mark - LazyLoad
  316. - (NSString *)city_dbId{
  317. if(!_city_dbId){
  318. _city_dbId = [self getCityidByCode:RQ_USER_MANAGER.currentUser.city];
  319. // _city_dbId = [self getCityidByName:RQ_USER_MANAGER.currentUser.cityName];
  320. }
  321. return _city_dbId;
  322. }
  323. - (NSArray *)cityCodeData{
  324. if(!_cityCodeData){
  325. @autoreleasepool {
  326. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"json"];
  327. if (filePath) {
  328. NSError *error = nil;
  329. NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
  330. if (!error) {
  331. NSError *jsonError = nil;
  332. NSArray *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
  333. if (!jsonError && [jsonDictionary isKindOfClass:[NSArray class]]) {
  334. _cityCodeData = jsonDictionary;
  335. } else {
  336. NSLog(@"Error parsing JSON: %@", jsonError);
  337. }
  338. } else {
  339. NSLog(@"Error reading file: %@", error);
  340. }
  341. } else {
  342. NSLog(@"JSON file not found.");
  343. }
  344. }
  345. }
  346. return _cityCodeData;
  347. }
  348. - (NSArray *)cityData{
  349. if(!_cityData){
  350. @autoreleasepool {
  351. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DrCityInfoThree" ofType:@"json"];
  352. if (filePath) {
  353. NSError *error = nil;
  354. NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
  355. if (!error) {
  356. NSError *jsonError = nil;
  357. NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
  358. if (!jsonError && [jsonDictionary isKindOfClass:[NSDictionary class]]) {
  359. _cityData = jsonDictionary[@"provinces"];
  360. } else {
  361. NSLog(@"Error parsing JSON: %@", jsonError);
  362. }
  363. } else {
  364. NSLog(@"Error reading file: %@", error);
  365. }
  366. } else {
  367. NSLog(@"JSON file not found.");
  368. }
  369. }
  370. }
  371. return _cityData;
  372. }
  373. - (RQLoginViewModel *)loginViewModel {
  374. if (!_loginViewModel) {
  375. _loginViewModel = [[RQLoginViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:nil];
  376. }
  377. return _loginViewModel;
  378. }
  379. @end