RQProfileViewModel.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. // 发起微信支付,设置参数
  84. //需要创建这个支付对象
  85. PayReq *req = [[PayReq alloc] init];
  86. //由用户微信号和AppID组成的唯一标识,用于校验微信用户
  87. req.openID = RQ_WECHAT_MANAGER.appID;
  88. // 商家id,在注册的时候给的
  89. req.partnerId = prepareOrderModel.partnerId;
  90. // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
  91. req.prepayId = prepareOrderModel.prepayId;
  92. // 根据财付通文档填写的数据和签名
  93. req.package = prepareOrderModel.packageValue;
  94. // 随机编码,为了防止重复的,在后台生成
  95. req.nonceStr = prepareOrderModel.nonceStr;
  96. // 这个是时间戳,也是在后台生成的,为了验证支付的
  97. NSString * stamp = prepareOrderModel.timeStamp;
  98. req.timeStamp = (UInt32)stamp.intValue;
  99. // 这个签名也是后台做的
  100. req.sign = prepareOrderModel.sign;
  101. [RQWechatManager hangleWechatPayWith:req];
  102. }];
  103. } cancelAction:^{
  104. }];
  105. }
  106. };
  107. return menuInfoItem;
  108. }].toArray;
  109. [subscriber sendNext:itemArr];
  110. } error:^(NSError * _Nullable error) {
  111. [subscriber sendError:error];
  112. } completed:^{
  113. [subscriber sendCompleted];
  114. }];
  115. return [RACDisposable disposableWithBlock:^{}];
  116. }];
  117. }
  118. #pragma mark - 配置数据
  119. - (void)rq_configureData {
  120. @weakify(self)
  121. /// 第一组
  122. RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel];
  123. RQProfileUserAndVipItemViewModel *profileUserAndVipItemViewModel = [[RQProfileUserAndVipItemViewModel alloc] initViewModelWithUser:RQ_USER_MANAGER.currentUser];
  124. group0.itemViewModels = @[profileUserAndVipItemViewModel];
  125. /// 第二组
  126. RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel];
  127. group1.footerHeight = 10.f;
  128. RQProfilePracticeItemViewModel *profilePracticeItemViewModel = [[RQProfilePracticeItemViewModel alloc] init];
  129. group1.itemViewModels = @[profilePracticeItemViewModel];
  130. /// 第三组
  131. RQProfileUserAndVipHeaderGroupViewModel *group2 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  132. group2.header = @"我的工具";
  133. RQCommonCollectionItemViewModel *coupon = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"优惠券" icon:@"优惠券"];
  134. RQCommonCollectionItemViewModel *synchronizationProgress = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"同步进度" icon:@"同步进度"];
  135. synchronizationProgress.operation = ^{
  136. RQSynchronizationViewModel *synchronizationViewModel = [[RQSynchronizationViewModel alloc] initWithServices:self.services params:@{}];
  137. [self.services pushViewModel:synchronizationViewModel animated:YES];
  138. };
  139. RQCommonCollectionItemViewModel *myOrder = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"我的订单" icon:@"我的订单"];
  140. // RQCommonCollectionItemViewModel *retraining = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"复训" icon:@"复训"];
  141. RQCommonCollectionItemViewModel *transferQuery = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"转校查询" icon:@"转校"];
  142. transferQuery.operation = ^{
  143. @strongify(self)
  144. RQStudentChangeSchoolViewModel *studentChangeSchoolViewModel = [[RQStudentChangeSchoolViewModel alloc] initWithServices:self.services params:nil];
  145. [self.services pushViewModel:studentChangeSchoolViewModel animated:YES];
  146. };
  147. RQCommonCollectionItemViewModel *commonQuestion = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"常见问题" icon:@"常见问题"];
  148. commonQuestion.operation = ^{
  149. NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://support.qq.com/products/361288"]];
  150. RQCustomWebViewViewController *vc = [[RQCustomWebViewViewController alloc] init];
  151. vc.url = URL.absoluteString;
  152. vc.bodyDicArr = @[
  153. @{@"key" : @"nickname", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.userName)? @"游客" : RQ_USER_MANAGER.currentUser.userName},
  154. @{@"key" : @"avatar", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.photo)? @"" : RQ_USER_MANAGER.currentUser.photo},
  155. @{@"key" : @"openid", @"value" : RQStringIsEmpty(RQ_USER_MANAGER.currentUser.outId)? @"" : RQ_USER_MANAGER.currentUser.outId}
  156. ];
  157. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:vc];
  158. [RQControllerHelper.topViewController presentViewController:nav animated:NO completion:nil];
  159. };
  160. RQCommonCollectionItemViewModel *test = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"三力测试" icon:@"三力测试"];
  161. test.operation = ^{
  162. };
  163. RQCommonCollectionItemViewModel *onlineService = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"联系客服" icon:@"联系客服"];
  164. onlineService.operation = ^{
  165. NSString *name = RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.userName)? RQ_USER_MANAGER.currentUser.userName : (RQStringIsNotEmpty(RQ_USER_MANAGER.currentUser.nickName)? RQ_USER_MANAGER.currentUser.nickName : @"游客");
  166. name = [NSString stringWithFormat:@"%@[极I学员_%@]",name,RQ_APP_VERSION];
  167. [QMConnect registerSDKWithAppKey:@"74442d40-9dc6-11e7-957d-a30a08c3a2c0" userName:name userId:RQ_USER_MANAGER.currentUser._id];
  168. };
  169. // group2.itemViewModels = @[coupon, synchronizationProgress, myOrder, transferQuery];
  170. group2.itemViewModels = @[test, synchronizationProgress, transferQuery, onlineService];
  171. /// 第四组
  172. RQProfileUserAndVipHeaderGroupViewModel *group3 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  173. group3.header = @"优惠相关";
  174. RAC(group3, itemViewModels) = [RACObserve(self, menuInfoArr) takeUntil:self.rac_willDeallocSignal];
  175. /// 第五组
  176. RQProfileUserAndVipHeaderGroupViewModel *group4 = [RQProfileUserAndVipHeaderGroupViewModel groupViewModel];
  177. group4.header = @"其他";
  178. #if defined(DEBUG)||defined(_DEBUG)
  179. /// 调试模式
  180. RQCommonCollectionItemViewModel *debug = [RQCommonCollectionItemViewModel itemViewModelWithTitle:@"打开/关闭调试器" icon:@"assistivetouch"];
  181. debug.operation = ^{
  182. [[RQDebugTouchView sharedInstance] setHide:![RQDebugTouchView sharedInstance].isHide];
  183. [RQSharedAppDelegate.window bringSubviewToFront:[RQDebugTouchView sharedInstance]];
  184. };
  185. group4.itemViewModels = @[debug];
  186. #else
  187. /// 发布模式
  188. group4.itemViewModels = @[];
  189. #endif
  190. self.dataSource = group4.itemViewModels.count > 0? @[group0, group1, group2, group3, group4] : @[group0, group1, group2, group3];
  191. }
  192. #pragma mark 客服
  193. - (void)registerSuccess:(NSNotification *)sender {
  194. NSLog(@"注册成功");
  195. if (_isPushed) {
  196. [MBProgressHUD rq_showProgressHUD:@""];
  197. _isConnecting = NO;
  198. return;
  199. }
  200. [QMConnect sdkGetWebchatScheduleConfig:^(NSDictionary *scheduleDic) {
  201. dispatch_async(dispatch_get_main_queue(), ^{
  202. self.dictionary = scheduleDic;
  203. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  204. NSLog(@"日程管理");
  205. [self starSchedule];
  206. }else{
  207. NSLog(@"技能组");
  208. [self getPeers];
  209. }
  210. });
  211. } failBlock:^(NSString *str) {
  212. }];
  213. }
  214. - (void)registerFailure:(NSNotification *)sender {
  215. NSLog(@"注册失败::%@", sender.object);
  216. self.isConnecting = NO;
  217. [MBProgressHUD rq_hideHUD];
  218. }
  219. #pragma mark - 技能组选择
  220. - (void)getPeers {
  221. @weakify(self)
  222. [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) {
  223. dispatch_async(dispatch_get_main_queue(), ^{
  224. @strongify(self)
  225. NSLog(@"%@", peerArray);
  226. NSArray *peers = peerArray;
  227. self.isConnecting = NO;
  228. [MBProgressHUD rq_hideHUD];
  229. if (peers.count == 1 && peers.count != 0) {
  230. [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"] processType:@""];
  231. }else {
  232. [self showPeersWithAlert:peers messageStr:@"选择您咨询的类型或业务部门(对应技能组)"];
  233. }
  234. });
  235. } failureBlock:^(NSString *str) {
  236. dispatch_async(dispatch_get_main_queue(), ^{
  237. @strongify(self)
  238. [MBProgressHUD rq_hideHUD];
  239. self.isConnecting = NO;
  240. });
  241. }];
  242. }
  243. #pragma mark - 日程管理
  244. - (void)starSchedule {
  245. self.isConnecting = NO;
  246. [MBProgressHUD rq_hideHUD];
  247. if ([self.dictionary[@"scheduleId"] isEqual: @""] || [self.dictionary[@"processId"] isEqual: @""] || [self.dictionary objectForKey:@"entranceNode"] == nil || [self.dictionary objectForKey:@"leavemsgNodes"] == nil) {
  248. [QMAlert showMessage:@"对不起,由于在线咨询配置错误,暂时无法进行咨询"];
  249. }else{
  250. NSDictionary *entranceNode = self.dictionary[@"entranceNode"];
  251. NSArray *entrances = entranceNode[@"entrances"];
  252. NSLog(@" 获取到日程节点数组 ===== %@", entrances);
  253. // [self showPeersWithAlert: sdkEntrances];
  254. if (entrances.count == 1 && entrances.count != 0) {
  255. [self showChatRoomViewController:[entrances.firstObject objectForKey:@"processTo"] processType:[entrances.firstObject objectForKey:@"processType"]];
  256. }else{
  257. [self showPeersWithAlert:entrances messageStr:@"选择您咨询的日程管理类型"];
  258. }
  259. }
  260. }
  261. - (void)showPeersWithAlert: (NSArray *)peers messageStr: (NSString *)message {
  262. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert];
  263. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  264. self.isConnecting = NO;
  265. }];
  266. [alertController addAction:cancelAction];
  267. for (NSDictionary *index in peers) {
  268. UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  269. if ([self.dictionary[@"scheduleEnable"] integerValue] == 1) {
  270. [self showChatRoomViewController:[index objectForKey:@"processTo"] processType:[index objectForKey:@"processType"]];
  271. }else{
  272. [self showChatRoomViewController:[index objectForKey:@"id"] processType:@""];
  273. }
  274. }];
  275. [alertController addAction:surelAction];
  276. }
  277. [RQControllerHelper.currentViewController presentViewController:alertController animated:YES completion:nil];
  278. }
  279. #pragma mark - 跳转聊天界面
  280. - (void)showChatRoomViewController:(NSString *)peerId processType:(NSString *)processType {
  281. QMChatRoomViewController *chatRoomViewController = [[QMChatRoomViewController alloc] init];
  282. chatRoomViewController.peerId = peerId;
  283. chatRoomViewController.isPush = NO;
  284. NSString *path = defUser.userDict[@"photo"];
  285. if (!path) {
  286. path = @"";
  287. }
  288. chatRoomViewController.avaterStr = path;
  289. if ([self.dictionary[@"scheduleEnable"] intValue] == 1) {
  290. chatRoomViewController.isOpenSchedule = true;
  291. chatRoomViewController.scheduleId = self.dictionary[@"scheduleId"];
  292. chatRoomViewController.processId = self.dictionary[@"processId"];
  293. chatRoomViewController.currentNodeId = peerId;
  294. chatRoomViewController.processType = processType;
  295. }else{
  296. chatRoomViewController.isOpenSchedule = false;
  297. }
  298. // MyUINavigationController* nav = [[MyUINavigationController alloc] initWithRootViewController:chatRoomViewController];
  299. // [nav.view setBackgroundColor:[UIColor whiteColor]];
  300. // nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  301. // nav.navigationBar.translucent = NO;
  302. // nav.modalPresentationStyle = UIModalPresentationFullScreen;
  303. // [RQControllerHelper.currentViewController.parentViewController presentViewController:nav animated:YES completion:nil];
  304. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:chatRoomViewController];
  305. [RQControllerHelper.topViewController presentViewController:nav animated:YES completion:nil];
  306. // 此处为了统一“退出登录”返回方式 就选择了present进去
  307. }
  308. - (NSMutableAttributedString *)setSpace:(CGFloat)line kern:(NSNumber *)kern font:(UIFont *)font text:(NSString *)text {
  309. NSMutableParagraphStyle * paraStyle = [NSMutableParagraphStyle new];
  310. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  311. paraStyle.alignment = NSTextAlignmentCenter;
  312. paraStyle.lineSpacing = line;
  313. paraStyle.hyphenationFactor = 1.0;
  314. paraStyle.firstLineHeadIndent = 0.0;
  315. paraStyle.paragraphSpacingBefore = 0.0;
  316. paraStyle.headIndent = 0;
  317. paraStyle.tailIndent = 0;
  318. NSDictionary *attributes = @{
  319. NSFontAttributeName: font,
  320. NSParagraphStyleAttributeName: paraStyle,
  321. NSKernAttributeName: kern
  322. };
  323. NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
  324. return attributeStr;
  325. }
  326. - (void)dealloc {
  327. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_SUCCEED object:nil];
  328. [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_ERROR_USER object:nil];
  329. }
  330. #pragma mark - LazyLoad
  331. - (NSArray *)menuInfoArr {
  332. if (!_menuInfoArr) {
  333. _menuInfoArr = @[];
  334. }
  335. return _menuInfoArr;
  336. }
  337. @end