RQProfileViewModel.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // RQProfileViewModel.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/21.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQProfileViewModel.h"
  9. #import "RQCustomWebViewViewController.h"
  10. #import "QMChatRoomViewController.h"
  11. #import <QMLineSDK/QMLineSDK.h>
  12. #import "QMAlert.h"
  13. @interface RQProfileViewModel ()
  14. @property (nonatomic, readwrite , copy) NSArray *menuInfoArr;
  15. @property (nonatomic, readwrite, strong) RACCommand *requestMenuInfoCommand;
  16. //客服
  17. @property (nonatomic, assign) BOOL isPushed; // 控制跳转
  18. @property (nonatomic, assign) BOOL isConnecting; // 控制多次注册
  19. @property (nonatomic, copy) NSDictionary * dictionary; //客服一些配置
  20. @end
  21. @implementation RQProfileViewModel
  22. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params{
  23. if (self = [super initWithServices:services params:params]) {
  24. }
  25. return self;
  26. }
  27. - (void)initialize {
  28. [super initialize];
  29. @weakify(self);
  30. self.title = @"";
  31. self.prefersNavigationBarBottomLineHidden = YES;
  32. self.prefersNavigationBarHidden = YES;
  33. [self rq_configureData];
  34. // RAC(self, menuInfoArr) = self.requestMenuInfoCommand.executionSignals.switchToLatest;
  35. // /// 过滤错误信息
  36. // [[self.requestMenuInfoCommand.errors
  37. // filter:^(NSError *error) {
  38. // return YES;
  39. // }] subscribe:self.errors];
  40. [[self.requestMenuInfoCommand execute:nil] subscribeNext:^(id _Nullable x) {
  41. @strongify(self)
  42. self.menuInfoArr = x;
  43. } completed:^{
  44. }];
  45. [RQ_Profile_Module getVipInfo];
  46. //客服
  47. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerSuccess:) name:CUSTOM_LOGIN_SUCCEED object:nil];
  48. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerFailure:) name:CUSTOM_LOGIN_ERROR_USER object:nil];
  49. }
  50. - (RACCommand *)requestMenuInfoCommand {
  51. @weakify(self)
  52. return [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
  53. @strongify(self)
  54. return [[self requestMenuInfoSignal] takeUntil:self.rac_willDeallocSignal];
  55. }];
  56. }
  57. - (RACSignal *)requestMenuInfoSignal {
  58. return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  59. [[RQ_HTTP_Service getMenuInfo] subscribeNext:^(NSArray *arr) {
  60. NSArray *itemArr = [arr.rac_sequence.signal map:^id _Nullable(RQMenuInfoModel *menuInfoModel) {
  61. RQCommonCollectionItemViewModel *menuInfoItem = [RQCommonCollectionItemViewModel itemViewModelWithTitle:menuInfoModel.MENU_NAME icon:menuInfoModel.MENU_ICON];
  62. menuInfoItem.operation = ^{
  63. if ([menuInfoModel.MENU_TYPE isEqualToString:@"1"]) {
  64. [RQ_SHARE_FUNCTION gotoWebViewWithUrlStr:menuInfoModel.MENU_URL];
  65. } else if ([menuInfoModel.MENU_TYPE isEqualToString:@"2"]) {
  66. [RQ_SHARE_FUNCTION miniwithUserName:menuInfoModel.MENU_URL path:menuInfoModel.MENU_XCX_URL];
  67. } else if ([menuInfoModel.MENU_TYPE isEqualToString:@"3"]) {
  68. if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:menuInfoModel.MENU_URL]]) {
  69. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:menuInfoModel.MENU_URL] options:@{} completionHandler:^(BOOL success) {}];
  70. } else {
  71. if (![menuInfoModel.MENU_WEB_URL isEqualToString:@""]) {
  72. [RQ_SHARE_FUNCTION gotoWebViewWithUrlStr:menuInfoModel.MENU_WEB_URL];
  73. } else {
  74. NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",menuInfoModel.MENU_IOS];
  75. NSURL *url = [NSURL URLWithString:urlStr];
  76. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
  77. }];
  78. }
  79. }
  80. } else if ([menuInfoModel.MENU_TYPE isEqualToString:@"4"]) {
  81. // [NSObject rq_showAlertViewWithTitle:@"温馨提示" message:@"是否捐赠少量资金" confirmTitle:@"是" cancelTitle:@"否" confirmAction:^{
  82. // [[RQ_HTTP_Service postPrepareOrderWithDictCode:35 userId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQPrepareOrderModel *prepareOrderModel) {
  83. // PayReq *req = [[PayReq alloc] init];
  84. // req.openID = RQ_WECHAT_MANAGER.appID;
  85. // req.partnerId = prepareOrderModel.partnerId;
  86. // req.prepayId = prepareOrderModel.prepayId;
  87. // req.package = prepareOrderModel.packageValue;
  88. // req.nonceStr = prepareOrderModel.nonceStr;
  89. // NSString * stamp = prepareOrderModel.timeStamp;
  90. // req.timeStamp = (UInt32)stamp.intValue;
  91. // req.sign = prepareOrderModel.sign;
  92. //// [RQWechatManager hangleWechatPayWith:req];
  93. // }];
  94. // } cancelAction:^{
  95. // }];
  96. }
  97. };
  98. return menuInfoItem;
  99. }].toArray;
  100. [subscriber sendNext:itemArr];
  101. } error:^(NSError * _Nullable error) {
  102. [subscriber sendError:error];
  103. } completed:^{
  104. [subscriber sendCompleted];
  105. }];
  106. return [RACDisposable disposableWithBlock:^{}];
  107. }];
  108. }
  109. #pragma mark - 配置数据
  110. - (void)rq_configureData {
  111. @weakify(self)
  112. /// 第一组
  113. RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel];
  114. RQProfileUserAndVipItemViewModel *profileUserAndVipItemViewModel = [[RQProfileUserAndVipItemViewModel alloc] initViewModelWithUser:RQ_USER_MANAGER.currentUser];
  115. group0.itemViewModels = @[profileUserAndVipItemViewModel];
  116. /// 第二组
  117. RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel];
  118. group1.footerHeight = 10.f;
  119. RQProfilePracticeItemViewModel *profilePracticeItemViewModel = [[RQProfilePracticeItemViewModel alloc] init];
  120. group1.itemViewModels = @[profilePracticeItemViewModel];
  121. /// 第三组
  122. RQProfileUserAndVipHeaderGroupViewModel *group2 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  123. group2.header = @"我的工具";
  124. RQCommonCollectionItemViewModel *coupon = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"优惠券" icon:@"优惠券"];
  125. RQCommonCollectionItemViewModel *synchronizationProgress = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"同步进度" icon:@"同步进度"];
  126. synchronizationProgress.operation = ^{
  127. RQSynchronizationViewModel *synchronizationViewModel = [[RQSynchronizationViewModel alloc] initWithServices:self.services params:@{}];
  128. [self.services pushViewModel:synchronizationViewModel animated:YES];
  129. };
  130. RQCommonCollectionItemViewModel *myOrder = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"我的订单" icon:@"我的订单"];
  131. // RQCommonCollectionItemViewModel *retraining = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"复训" icon:@"复训"];
  132. RQCommonCollectionItemViewModel *transferQuery = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"转校查询" icon:@"转校"];
  133. transferQuery.operation = ^{
  134. @strongify(self)
  135. RQStudentChangeSchoolViewModel *studentChangeSchoolViewModel = [[RQStudentChangeSchoolViewModel alloc] initWithServices:self.services params:nil];
  136. [self.services pushViewModel:studentChangeSchoolViewModel animated:YES];
  137. };
  138. RQCommonCollectionItemViewModel *commonQuestion = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"常见问题" icon:@"常见问题"];
  139. commonQuestion.operation = ^{
  140. NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://support.qq.com/products/361288"]];
  141. RQCustomWebViewViewController *vc = [[RQCustomWebViewViewController alloc] init];
  142. vc.url = URL.absoluteString;
  143. vc.bodyDicArr = @[
  144. @{@"key" : @"nickname", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.userName)? @"游客" : RQ_USER_MANAGER.currentUser.userName},
  145. @{@"key" : @"avatar", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.photo)? @"" : RQ_USER_MANAGER.currentUser.photo},
  146. @{@"key" : @"openid", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.outId)? @"" : RQ_USER_MANAGER.currentUser.outId}
  147. ];
  148. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:vc];
  149. [RQControllerHelper.topViewController presentViewController:nav animated:NO completion:nil];
  150. };
  151. RQCommonCollectionItemViewModel *test = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"三力测试" icon:@"三力测试"];
  152. test.operation = ^{
  153. };
  154. RQCommonCollectionItemViewModel *onlineService = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"联系客服" icon:@"联系客服"];
  155. onlineService.operation = ^{
  156. NSString *name = RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.userName)? RQ_USER_MANAGER.currentUser.userName : (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.nickName)? RQ_USER_MANAGER.currentUser.nickName : @"游客");
  157. name = [NSString stringWithFormat:@"%@[极I学员_%@]",name,RQ_APP_VERSION];
  158. [QMConnect registerSDKWithAppKey:@"74442d40-9dc6-11e7-957d-a30a08c3a2c0" userName:name userId:RQ_USER_MANAGER.currentUser._id];
  159. };
  160. // group2.itemViewModels = @[coupon, synchronizationProgress, myOrder, transferQuery];
  161. group2.itemViewModels = @[test, synchronizationProgress, transferQuery, onlineService];
  162. // group2.itemViewModels = @[test, synchronizationProgress, onlineService];
  163. /// 第四组
  164. RQProfileUserAndVipHeaderGroupViewModel *group3 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  165. group3.header = @"优惠相关";
  166. RAC(group3, itemViewModels) = [RACObserve(self, menuInfoArr) takeUntil:self.rac_willDeallocSignal];
  167. /// 第五组
  168. RQProfileUserAndVipHeaderGroupViewModel *group4 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  169. group4.header = @"其他";
  170. #if defined(DEBUG)||defined(_DEBUG)
  171. /// 调试模式
  172. RQCommonCollectionItemViewModel *debug = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"打开/关闭调试器" icon:@"assistivetouch"];
  173. debug.operation = ^{
  174. [[RQDebugTouchView sharedInstance] setHide:![RQDebugTouchView sharedInstance].isHide];
  175. [RQSharedAppDelegate.window bringSubviewToFront:[RQDebugTouchView sharedInstance]];
  176. };
  177. group4.itemViewModels = @[debug];
  178. #else
  179. /// 发布模式
  180. group4.itemViewModels = @[];
  181. #endif
  182. self.dataSource = group4.itemViewModels.count > 0? @[group0, group1, group2, group3, group4] : @[group0, group1, group2, group3];
  183. }
  184. #pragma mark 客服
  185. - (void)registerSuccess:(NSNotification *)sender {
  186. NSLog(@"注册成功");
  187. if (_isPushed) {
  188. [MBProgressHUD rq_showProgressHUD:@""];
  189. _isConnecting = NO;
  190. return;
  191. }
  192. [QMConnect sdkGetWebchatScheduleConfig:^(NSDictionary *scheduleDic) {
  193. dispatch_async(dispatch_get_main_queue(), ^{
  194. self.dictionary = scheduleDic;
  195. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  196. NSLog(@"日程管理");
  197. [self starSchedule];
  198. }else{
  199. NSLog(@"技能组");
  200. [self getPeers];
  201. }
  202. });
  203. } failBlock:^(NSString *str) {
  204. }];
  205. }
  206. - (void)registerFailure:(NSNotification *)sender {
  207. NSLog(@"注册失败::%@", sender.object);
  208. self.isConnecting = NO;
  209. [MBProgressHUD rq_hideHUD];
  210. }
  211. #pragma mark - 技能组选择
  212. - (void)getPeers {
  213. @weakify(self)
  214. [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) {
  215. dispatch_async(dispatch_get_main_queue(), ^{
  216. @strongify(self)
  217. NSLog(@"%@", peerArray);
  218. NSArray *peers = peerArray;
  219. self.isConnecting = NO;
  220. [MBProgressHUD rq_hideHUD];
  221. if (peers.count == 1 && peers.count != 0) {
  222. [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"] processType:@""];
  223. }else {
  224. [self showPeersWithAlert:peers messageStr:@"选择您咨询的类型或业务部门(对应技能组)"];
  225. }
  226. });
  227. } failureBlock:^(NSString *str) {
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. @strongify(self)
  230. [MBProgressHUD rq_hideHUD];
  231. self.isConnecting = NO;
  232. });
  233. }];
  234. }
  235. #pragma mark - 日程管理
  236. - (void)starSchedule {
  237. self.isConnecting = NO;
  238. [MBProgressHUD rq_hideHUD];
  239. if ([self.dictionary[@"scheduleId"] isEqual: @""] || [self.dictionary[@"processId"] isEqual: @""] || [self.dictionary objectForKey:@"entranceNode"] == nil || [self.dictionary objectForKey:@"leavemsgNodes"] == nil) {
  240. [QMAlert showMessage:@"对不起,由于在线咨询配置错误,暂时无法进行咨询"];
  241. }else{
  242. NSDictionary *entranceNode = self.dictionary[@"entranceNode"];
  243. NSArray *entrances = entranceNode[@"entrances"];
  244. NSLog(@" 获取到日程节点数组 ===== %@", entrances);
  245. // [self showPeersWithAlert: sdkEntrances];
  246. if (entrances.count == 1 && entrances.count != 0) {
  247. [self showChatRoomViewController:[entrances.firstObject objectForKey:@"processTo"] processType:[entrances.firstObject objectForKey:@"processType"]];
  248. }else{
  249. [self showPeersWithAlert:entrances messageStr:@"选择您咨询的日程管理类型"];
  250. }
  251. }
  252. }
  253. - (void)showPeersWithAlert: (NSArray *)peers messageStr: (NSString *)message {
  254. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert];
  255. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  256. self.isConnecting = NO;
  257. }];
  258. [alertController addAction:cancelAction];
  259. for (NSDictionary *index in peers) {
  260. UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  261. if ([self.dictionary[@"scheduleEnable"] integerValue] == 1) {
  262. [self showChatRoomViewController:[index objectForKey:@"processTo"] processType:[index objectForKey:@"processType"]];
  263. }else{
  264. [self showChatRoomViewController:[index objectForKey:@"id"] processType:@""];
  265. }
  266. }];
  267. [alertController addAction:surelAction];
  268. }
  269. [RQControllerHelper.currentViewController presentViewController:alertController animated:YES completion:nil];
  270. }
  271. #pragma mark - 跳转聊天界面
  272. - (void)showChatRoomViewController:(NSString *)peerId processType:(NSString *)processType {
  273. QMChatRoomViewController *chatRoomViewController = [[QMChatRoomViewController alloc] init];
  274. chatRoomViewController.peerId = peerId;
  275. chatRoomViewController.isPush = NO;
  276. NSString *path = defUser.userDict[@"photo"];
  277. if (!path) {
  278. path = @"";
  279. }
  280. chatRoomViewController.avaterStr = path;
  281. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  282. chatRoomViewController.isOpenSchedule = true;
  283. chatRoomViewController.scheduleId = self.dictionary[@"scheduleId"];
  284. chatRoomViewController.processId = self.dictionary[@"processId"];
  285. chatRoomViewController.currentNodeId = peerId;
  286. chatRoomViewController.processType = processType;
  287. }else{
  288. chatRoomViewController.isOpenSchedule = false;
  289. }
  290. // MyUINavigationController* nav = [[MyUINavigationController alloc] initWithRootViewController:chatRoomViewController];
  291. // [nav.view setBackgroundColor:[UIColor whiteColor]];
  292. // nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  293. // nav.navigationBar.translucent = NO;
  294. // nav.modalPresentationStyle = UIModalPresentationFullScreen;
  295. // [RQControllerHelper.currentViewController.parentViewController presentViewController:nav animated:YES completion:nil];
  296. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:chatRoomViewController];
  297. [RQControllerHelper.topViewController presentViewController:nav animated:YES completion:nil];
  298. // 此处为了统一“退出登录”返回方式 就选择了present进去
  299. }
  300. - (NSMutableAttributedString *)setSpace:(CGFloat)line kern:(NSNumber *)kern font:(UIFont *)font text:(NSString *)text {
  301. NSMutableParagraphStyle * paraStyle = [NSMutableParagraphStyle new];
  302. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  303. paraStyle.alignment = NSTextAlignmentCenter;
  304. paraStyle.lineSpacing = line;
  305. paraStyle.hyphenationFactor = 1.0;
  306. paraStyle.firstLineHeadIndent = 0.0;
  307. paraStyle.paragraphSpacingBefore = 0.0;
  308. paraStyle.headIndent = 0;
  309. paraStyle.tailIndent = 0;
  310. NSDictionary *attributes = @{
  311. NSFontAttributeName: font,
  312. NSParagraphStyleAttributeName: paraStyle,
  313. NSKernAttributeName: kern
  314. };
  315. NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
  316. return attributeStr;
  317. }
  318. - (void)dealloc {
  319. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_SUCCEED object:nil];
  320. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_ERROR_USER object:nil];
  321. }
  322. #pragma mark - LazyLoad
  323. - (NSArray *)menuInfoArr {
  324. if (!_menuInfoArr) {
  325. _menuInfoArr = @[];
  326. }
  327. return _menuInfoArr;
  328. }
  329. @end