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. NSLog(@"点击了-电子资料");
  154. [RQ_SHARE_FUNCTION miniwithUserName:RQ_COMMON_MANAGER.JSJP_XCX_NAME path:RQ_COMMON_MANAGER.JSJP_XCX_PAGE];
  155. };
  156. RQCommonCollectionItemViewModel *onlineService = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"联系客服" icon:@"联系客服"];
  157. onlineService.operation = ^{
  158. NSString *name = RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.userName)? RQ_USER_MANAGER.currentUser.userName : (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.nickName)? RQ_USER_MANAGER.currentUser.nickName : @"游客");
  159. name = [NSString stringWithFormat:@"%@[极I学员_%@]",name,RQ_APP_VERSION];
  160. [QMConnect registerSDKWithAppKey:@"74442d40-9dc6-11e7-957d-a30a08c3a2c0" userName:name userId:RQ_USER_MANAGER.currentUser._id];
  161. };
  162. // group2.itemViewModels = @[coupon, synchronizationProgress, myOrder, transferQuery];
  163. group2.itemViewModels = @[test, synchronizationProgress, transferQuery, onlineService];
  164. // group2.itemViewModels = @[test, synchronizationProgress, onlineService];
  165. /// 第四组
  166. RQProfileUserAndVipHeaderGroupViewModel *group3 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  167. group3.header = @"优惠相关";
  168. RAC(group3, itemViewModels) = [RACObserve(self, menuInfoArr) takeUntil:self.rac_willDeallocSignal];
  169. /// 第五组
  170. RQProfileUserAndVipHeaderGroupViewModel *group4 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  171. group4.header = @"其他";
  172. #if defined(DEBUG)||defined(_DEBUG)
  173. /// 调试模式
  174. RQCommonCollectionItemViewModel *debug = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"打开/关闭调试器" icon:@"assistivetouch"];
  175. debug.operation = ^{
  176. [[RQDebugTouchView sharedInstance] setHide:![RQDebugTouchView sharedInstance].isHide];
  177. [RQSharedAppDelegate.window bringSubviewToFront:[RQDebugTouchView sharedInstance]];
  178. };
  179. group4.itemViewModels = @[debug];
  180. #else
  181. /// 发布模式
  182. group4.itemViewModels = @[];
  183. #endif
  184. self.dataSource = group4.itemViewModels.count > 0? @[group0, group1, group2, group3, group4] : @[group0, group1, group2, group3];
  185. }
  186. #pragma mark 客服
  187. - (void)registerSuccess:(NSNotification *)sender {
  188. NSLog(@"注册成功");
  189. if (_isPushed) {
  190. [MBProgressHUD rq_showProgressHUD:@""];
  191. _isConnecting = NO;
  192. return;
  193. }
  194. [QMConnect sdkGetWebchatScheduleConfig:^(NSDictionary *scheduleDic) {
  195. dispatch_async(dispatch_get_main_queue(), ^{
  196. self.dictionary = scheduleDic;
  197. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  198. NSLog(@"日程管理");
  199. [self starSchedule];
  200. }else{
  201. NSLog(@"技能组");
  202. [self getPeers];
  203. }
  204. });
  205. } failBlock:^(NSString *str) {
  206. }];
  207. }
  208. - (void)registerFailure:(NSNotification *)sender {
  209. NSLog(@"注册失败::%@", sender.object);
  210. self.isConnecting = NO;
  211. [MBProgressHUD rq_hideHUD];
  212. }
  213. #pragma mark - 技能组选择
  214. - (void)getPeers {
  215. @weakify(self)
  216. [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) {
  217. dispatch_async(dispatch_get_main_queue(), ^{
  218. @strongify(self)
  219. NSLog(@"%@", peerArray);
  220. NSArray *peers = peerArray;
  221. self.isConnecting = NO;
  222. [MBProgressHUD rq_hideHUD];
  223. if (peers.count == 1 && peers.count != 0) {
  224. [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"] processType:@""];
  225. }else {
  226. [self showPeersWithAlert:peers messageStr:@"选择您咨询的类型或业务部门(对应技能组)"];
  227. }
  228. });
  229. } failureBlock:^(NSString *str) {
  230. dispatch_async(dispatch_get_main_queue(), ^{
  231. @strongify(self)
  232. [MBProgressHUD rq_hideHUD];
  233. self.isConnecting = NO;
  234. });
  235. }];
  236. }
  237. #pragma mark - 日程管理
  238. - (void)starSchedule {
  239. self.isConnecting = NO;
  240. [MBProgressHUD rq_hideHUD];
  241. if ([self.dictionary[@"scheduleId"] isEqual: @""] || [self.dictionary[@"processId"] isEqual: @""] || [self.dictionary objectForKey:@"entranceNode"] == nil || [self.dictionary objectForKey:@"leavemsgNodes"] == nil) {
  242. [QMAlert showMessage:@"对不起,由于在线咨询配置错误,暂时无法进行咨询"];
  243. }else{
  244. NSDictionary *entranceNode = self.dictionary[@"entranceNode"];
  245. NSArray *entrances = entranceNode[@"entrances"];
  246. NSLog(@" 获取到日程节点数组 ===== %@", entrances);
  247. // [self showPeersWithAlert: sdkEntrances];
  248. if (entrances.count == 1 && entrances.count != 0) {
  249. [self showChatRoomViewController:[entrances.firstObject objectForKey:@"processTo"] processType:[entrances.firstObject objectForKey:@"processType"]];
  250. }else{
  251. [self showPeersWithAlert:entrances messageStr:@"选择您咨询的日程管理类型"];
  252. }
  253. }
  254. }
  255. - (void)showPeersWithAlert: (NSArray *)peers messageStr: (NSString *)message {
  256. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert];
  257. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  258. self.isConnecting = NO;
  259. }];
  260. [alertController addAction:cancelAction];
  261. for (NSDictionary *index in peers) {
  262. UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  263. if ([self.dictionary[@"scheduleEnable"] integerValue] == 1) {
  264. [self showChatRoomViewController:[index objectForKey:@"processTo"] processType:[index objectForKey:@"processType"]];
  265. }else{
  266. [self showChatRoomViewController:[index objectForKey:@"id"] processType:@""];
  267. }
  268. }];
  269. [alertController addAction:surelAction];
  270. }
  271. [RQControllerHelper.currentViewController presentViewController:alertController animated:YES completion:nil];
  272. }
  273. #pragma mark - 跳转聊天界面
  274. - (void)showChatRoomViewController:(NSString *)peerId processType:(NSString *)processType {
  275. QMChatRoomViewController *chatRoomViewController = [[QMChatRoomViewController alloc] init];
  276. chatRoomViewController.peerId = peerId;
  277. chatRoomViewController.isPush = NO;
  278. NSString *path = defUser.userDict[@"photo"];
  279. if (!path) {
  280. path = @"";
  281. }
  282. chatRoomViewController.avaterStr = path;
  283. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  284. chatRoomViewController.isOpenSchedule = true;
  285. chatRoomViewController.scheduleId = self.dictionary[@"scheduleId"];
  286. chatRoomViewController.processId = self.dictionary[@"processId"];
  287. chatRoomViewController.currentNodeId = peerId;
  288. chatRoomViewController.processType = processType;
  289. }else{
  290. chatRoomViewController.isOpenSchedule = false;
  291. }
  292. // MyUINavigationController* nav = [[MyUINavigationController alloc] initWithRootViewController:chatRoomViewController];
  293. // [nav.view setBackgroundColor:[UIColor whiteColor]];
  294. // nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  295. // nav.navigationBar.translucent = NO;
  296. // nav.modalPresentationStyle = UIModalPresentationFullScreen;
  297. // [RQControllerHelper.currentViewController.parentViewController presentViewController:nav animated:YES completion:nil];
  298. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:chatRoomViewController];
  299. [RQControllerHelper.topViewController presentViewController:nav animated:YES completion:nil];
  300. // 此处为了统一“退出登录”返回方式 就选择了present进去
  301. }
  302. - (NSMutableAttributedString *)setSpace:(CGFloat)line kern:(NSNumber *)kern font:(UIFont *)font text:(NSString *)text {
  303. NSMutableParagraphStyle * paraStyle = [NSMutableParagraphStyle new];
  304. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  305. paraStyle.alignment = NSTextAlignmentCenter;
  306. paraStyle.lineSpacing = line;
  307. paraStyle.hyphenationFactor = 1.0;
  308. paraStyle.firstLineHeadIndent = 0.0;
  309. paraStyle.paragraphSpacingBefore = 0.0;
  310. paraStyle.headIndent = 0;
  311. paraStyle.tailIndent = 0;
  312. NSDictionary *attributes = @{
  313. NSFontAttributeName: font,
  314. NSParagraphStyleAttributeName: paraStyle,
  315. NSKernAttributeName: kern
  316. };
  317. NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
  318. return attributeStr;
  319. }
  320. - (void)dealloc {
  321. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_SUCCEED object:nil];
  322. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_ERROR_USER object:nil];
  323. }
  324. #pragma mark - LazyLoad
  325. - (NSArray *)menuInfoArr {
  326. if (!_menuInfoArr) {
  327. _menuInfoArr = @[];
  328. }
  329. return _menuInfoArr;
  330. }
  331. @end