JYSubViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. //
  2. // JYSubViewController.m
  3. // JianYuIOS
  4. //
  5. // Created by apple on 2018/11/8.
  6. // Copyright © 2018年 lixianglan. All rights reserved.
  7. //
  8. #import "JYSubViewController.h"
  9. #import "JYTabBarController.h"
  10. @protocol JYSubViewControllerJSExport<JSExport>
  11. JSExportAs(loginByWeixin, -(NSString *)jsLoginByWeixin:(NSString *)str);// 微信登录 openid
  12. JSExportAs(share, -(void)jsShare:(NSString *)type title:(NSString *)title content:(NSString *)content link:(NSString *)link); // 调用分享
  13. JSExportAs(saveUserToken, -(void)jsSaveUserToken:(NSString *)token);// 登录时获取令牌 保存
  14. JSExportAs(getUserToken, -(NSString *)jsGetUserToken:(NSString *)str);// 取本地存的令牌
  15. JSExportAs(removeUserToken, -(void)jsRemoveUserToken:(NSString *)str); // 退出时清空数据
  16. JSExportAs(checkNoticePermission, -(int)jsCheckNoticePermission:(NSString *)str); // 查询手机通知是否打开
  17. JSExportAs(openSystemNotification, -(void)jsOpenSystemNotification:(NSString *)str);// 跳转到系统设置
  18. JSExportAs(getPushRid, -(NSString *)jsGetPushRid:(NSString *)str);// 返回极光id
  19. JSExportAs(getMessageList, -(NSString *)jsGetMessageList:(NSString *)openid);// 按通知接收时间倒排,取前100条
  20. JSExportAs(updateMessageState, -(void)jsUpdateMessageState:(NSString *)lxID state:(NSString *)state);// 更新状态
  21. JSExportAs(deleteMessageById, -(void)jsDeleteMessageById:(NSString *)lxID);// 删除一条消息
  22. JSExportAs(getMessage, -(NSString *)jsGetMessage:(NSString *)lxID);// 取单条数据
  23. JSExportAs(getUnReadMessageCount, -(int)jsGetUnReadMessageCount:(NSString *)openid);// 获取未读消息总数
  24. JSExportAs(updateAllUnReadMessage, -(void)jsUpdateAllUnReadMessage:(NSString *)openid);// 全部置为已读
  25. JSExportAs(lightStatusBar, -(void)jsLightStatusBar:(NSString *)status);// 导航栏文字颜色
  26. JSExportAs(openExternalLink, -(void)jsOpenExternalLink:(NSString *)url title:(NSString *)title);// 打开新控制器 加载新web
  27. JSExportAs(getVersion, -(NSString *)jsGetVersion:(NSString *)str);// 获取当前版本号
  28. JSExportAs(alert, -(void)jsAlert:(NSString *)content);// 弹框
  29. JSExportAs(isInstallWeixin, -(int)jsIsInstallWeixin:(NSString *)str);// 是否安装微信
  30. JSExportAs(changeMessageType, -(void)jsChangeMessageType:(NSString *)link);//已读操作处理
  31. JSExportAs(openActivityPage, -(void)jsOpenActivityPage:(NSString *)url rectype:(NSString *)rectype openid:(NSString *)openid);
  32. JSExportAs(getCipherText, -(NSString *)jsGGetCipherText:(NSString *)phone);
  33. JSExportAs(business, -(NSInteger)jsBusiness:(NSInteger)shooID shopType:(NSInteger )shopType);
  34. JSExportAs(loginSuccess, -(void)jsLoginSuccess:(NSString*)status);
  35. JSExportAs(backUrl, -(void)jsBackUrl:(NSString*)str);
  36. JSExportAs(hideRedSpotOnMenu, -(void)jsHideRedSpotOnMenu:(NSString*)str);
  37. @end
  38. @interface JYSubViewController ()<UIWebViewDelegate, JYSubViewControllerJSExport>
  39. {
  40. CGPoint startTouch;
  41. BOOL _isMoving;
  42. BOOL searchRefreshBool;
  43. }
  44. @property(nonatomic, strong)UIWebView *webView;
  45. @property(nonatomic, assign)BOOL updateNotify;
  46. @property(nonatomic, assign)BOOL afterOpenPushMessage;
  47. @property(nonatomic, strong)NoNetView *noNetView;
  48. @property(nonatomic, strong)NSURLRequest *errRequest;
  49. @property(nonatomic, strong)NSString *signOutContent;
  50. @end
  51. NSString *const kInitVector_1 = @"1389461544135476";
  52. size_t const kKeySize_1 = kCCKeySizeAES128;
  53. NSString *const AesKey_1 = @"mGlAgnIBB8bx2nch";
  54. @implementation JYSubViewController
  55. -(void)viewWillAppear:(BOOL)animated
  56. {
  57. [super viewWillAppear:animated];
  58. [self.navigationController setNavigationBarHidden:YES animated:animated];
  59. }
  60. - (void)viewDidLoad {
  61. [super viewDidLoad];
  62. self.view.backgroundColor = [UIColor whiteColor];
  63. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, WIDTH, HEIGHT-TAB_BAR_HEIGHT+STATUS_BAR_HEIGHT)];
  64. [self.view addSubview: self.webView];
  65. self.webView.scrollView.bounces = NO;
  66. self.webView.delegate = self;
  67. [[LXViewControllerManager shareViewControllerManager] showHudText:nil];
  68. [self webViewFirstRequest];//请求页面数据
  69. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestWebView:) name:kRequestWebViewWithUrl object:nil];
  70. //点击订阅通知
  71. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickSubNotiMethods:) name:ClickSubNotiMethods object:nil];
  72. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNotifyState) name:kUpdateNotifyState object:nil];
  73. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(afterReceiveNewMsg) name:kAfterReceiveNewMsg object:nil];
  74. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView) name:kReloadWebVIew object:nil];
  75. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginOut:) name:kLoginOut object:nil];
  76. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  77. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachChanged:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
  78. UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(paningGestureReceive:)];
  79. [recognizer delaysTouchesBegan];
  80. [self.view addGestureRecognizer:recognizer];
  81. //手势
  82. [self clearWeb];
  83. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDY:) name:@"refreshDY" object:nil];
  84. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAllVC:) name:@"refreshAllVC" object:nil];
  85. }
  86. -(void)moveViewWithX:(float)x
  87. {
  88. CGRect rect = self.view.bounds;
  89. CGFloat width = CGRectGetWidth(rect);
  90. x = x>width?width:x;
  91. x = x<0?0:x;
  92. CGRect frame = self.webView.frame;
  93. frame.origin.x = x;
  94. self.webView.frame = frame;
  95. }
  96. #pragma mark – Gesture Recognizer –
  97. -(void)paningGestureReceive:(UIPanGestureRecognizer *)recoginzer
  98. {
  99. CGRect rect = self.view.bounds;
  100. CGFloat width = CGRectGetWidth(rect);
  101. if (![self.webView canGoBack]) {
  102. NSLog(@"不可返回");
  103. return;
  104. }
  105. NSString *current_url = [self.webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
  106. NSLog(@"当前页面:%@",current_url);
  107. if([current_url containsString:@"jyapp/free/login"]) {
  108. return;
  109. }
  110. // NSString *current_url = self.webView.request.URL.absoluteString;
  111. if([current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/free/me",kHost]]||[current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/jylab/index",kHost]]||[current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/swordfish/historypush",kHost]]||[current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/jylab/mainSearch",kHost]]){
  112. return;
  113. }
  114. //https://www.jianyu360.com/jyapp/free/me
  115. //https://www.jianyu360.com/jyapp/jylab/index
  116. //https://www.jianyu360.com/jyapp/wxkeyset/keyset/index
  117. //https://www.jianyu360.com/jyapp/jylab/mainSearch
  118. CGPoint touchPoint = [recoginzer locationInView:[UIApplication sharedApplication].keyWindow];
  119. if (recoginzer.state==UIGestureRecognizerStateBegan) {
  120. _isMoving = YES;
  121. startTouch = touchPoint;
  122. } else if (recoginzer.state==UIGestureRecognizerStateEnded){
  123. if (touchPoint.x - startTouch.x > width*0.125) {
  124. [UIView animateWithDuration:0.3 animations:^{
  125. [self moveViewWithX:width];
  126. [self.webView goBack];
  127. } completion:^(BOOL finished) {
  128. _isMoving = NO;
  129. [self moveViewWithX:0];
  130. }];
  131. } else {
  132. [UIView animateWithDuration:0.3 animations:^{
  133. [self moveViewWithX:0];
  134. } completion:^(BOOL finished) {
  135. _isMoving = NO;
  136. }];
  137. }
  138. return;
  139. } else if (recoginzer.state==UIGestureRecognizerStateCancelled){
  140. [UIView animateWithDuration:0.3 animations:^{
  141. [self moveViewWithX:0];
  142. } completion:^(BOOL finished) {
  143. _isMoving = NO;
  144. }];
  145. return;
  146. }
  147. if (_isMoving) {
  148. [self moveViewWithX:touchPoint.x - startTouch.x];
  149. }
  150. }
  151. // 监控网络状态变化
  152. -(void)netReachChanged:(NSNotification *)notificaition{
  153. NSDictionary *userInfo = notificaition.userInfo;
  154. if ([userInfo[@"AFNetworkingReachabilityNotificationStatusItem"] intValue] == 0 || [userInfo[@"AFNetworkingReachabilityNotificationStatusItem"] intValue] == -1) {
  155. [self addNoNetViews];
  156. }else{
  157. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"firstLoad"] intValue] == 0) {
  158. [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"firstLoad"];
  159. [self removeNoNetViews];
  160. [self webViewFirstRequest];
  161. }
  162. }
  163. }
  164. // 再次加载页面
  165. -(void)reloadWebView{
  166. [[LXViewControllerManager shareViewControllerManager] showHudText:nil];
  167. if (!self.webView.canGoBack) {
  168. [self webViewFirstRequest];
  169. }else{
  170. [self.webView reload];
  171. }
  172. }
  173. // 首次加载web
  174. -(void)webViewFirstRequest{
  175. NSString *url = [NSString stringWithFormat:@"%@%@", kHost, @"jyapp/swordfish/historypush"];
  176. //swordfish/historypush swordfish/historypush
  177. NSLog(@"222第一次加载%@",url);
  178. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  179. kMainQueue(^{
  180. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  181. })
  182. }
  183. //收到消息退出并重新加载web
  184. -(void)webViewRequestByMsg{
  185. // NSString *url = [NSString stringWithFormat:@"%@%@", kHost, @"jyapp/free/kicked"];
  186. NSString *url = [NSString stringWithFormat:@"%@%@", kHost, @"jyapp/free/login?back=index&isios=t"];
  187. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  188. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  189. }
  190. -(void)requestWebTime:(NSDictionary *)infoDict
  191. {
  192. NSString * urlStr = [NSString stringWithFormat:@"%@",infoDict[@"url"]];
  193. if([infoDict[@"type"] isEqualToString:@"message"]) {
  194. NSString *url = [NSString stringWithFormat:@"%@",urlStr];
  195. NSArray * array = [url componentsSeparatedByString:@"=="];
  196. url = [array.firstObject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  197. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  198. [[NSNotificationCenter defaultCenter] postNotificationName:@"kNewWebControllerDismiss" object:nil];
  199. //
  200. }else {
  201. NSString *url = [NSString stringWithFormat:@"%@%@", kHost, urlStr];
  202. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  203. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  204. [[NSNotificationCenter defaultCenter] postNotificationName:@"kNewWebControllerDismiss" object:nil];
  205. }
  206. }
  207. // 点击通知 跳转到通知内url
  208. -(void)requestWebView:(NSNotification *)notification{
  209. NSDictionary * infoDict = [notification object];
  210. if (!infoDict) {
  211. return;
  212. }
  213. NSString * urlStr = [NSString stringWithFormat:@"%@",infoDict[@"url"]];
  214. if (urlStr) {
  215. if(self.tabBarController.selectedIndex != 1) {
  216. return;
  217. }else {
  218. [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshMine" object:nil];;
  219. }
  220. [[LXViewControllerManager shareViewControllerManager] showHudText:nil];
  221. self.afterOpenPushMessage = YES;
  222. [self performSelector:@selector(requestWebTime:) withObject:infoDict afterDelay:1];
  223. }
  224. }
  225. -(void)clickSubNotiMethods:(NSNotification *)notification{
  226. NSDictionary * infoDict = [notification object][@"info"];
  227. if (!infoDict) {
  228. return;
  229. }
  230. NSString * urlStr = [NSString stringWithFormat:@"%@",infoDict[@"url"]];
  231. if (urlStr) {
  232. if(self.tabBarController.selectedIndex == 1) {
  233. [self webViewFirstRequest];
  234. }
  235. }
  236. }
  237. // 收到通知消息
  238. -(void)afterReceiveNewMsg{
  239. NSLog(@"afterReceiveNewMsg");
  240. JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  241. [context evaluateScript:@"afterReceiveNewMsg()"];
  242. }
  243. // 收到退出登录消息
  244. -(void)loginOut:(NSNotification *)notification{
  245. if([notification.userInfo[@"item_index"] integerValue]!=1) {
  246. return;
  247. }
  248. self.signOutContent = notification.object;
  249. if (!self.signOutContent) {
  250. self.signOutContent = @"1";
  251. }
  252. NSString *url = [NSString stringWithFormat:@"%@%@?sign=%@", kHost, @"jyapp/free/signOut", [LXUserDefaults token]];
  253. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  254. // UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
  255. // webView.delegate = self;
  256. // [self.view addSubview:webView];
  257. // [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  258. //
  259. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  260. [self jsRemoveUserToken:nil];
  261. }
  262. - (void)hideTabBar {
  263. kMainQueue(^{
  264. if (self.tabBarController.tabBar.hidden == YES) {
  265. return;
  266. }
  267. UIView *contentView;
  268. if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
  269. contentView = [self.tabBarController.view.subviews objectAtIndex:1];
  270. else
  271. contentView = [self.tabBarController.view.subviews objectAtIndex:0];
  272. contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
  273. self.tabBarController.tabBar.hidden = YES;
  274. });
  275. }
  276. - (void)showTabBar{
  277. kMainQueue(^{
  278. if (self.tabBarController.tabBar.hidden == NO)
  279. {
  280. return;
  281. }
  282. UIView *contentView;
  283. if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
  284. contentView = [self.tabBarController.view.subviews objectAtIndex:1];
  285. else
  286. contentView = [self.tabBarController.view.subviews objectAtIndex:0];
  287. contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
  288. self.tabBarController.tabBar.hidden = NO;
  289. });
  290. }
  291. //
  292. -(void)dealTabBar
  293. {
  294. NSLog(@"刷新第二个页面");
  295. kMainQueue(^{
  296. [self webViewFirstRequest];
  297. });
  298. }
  299. -(void)judgeIsHidden
  300. {
  301. NSString *current_url = [self.webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
  302. //http://w4blmjy.qmx.top/jyapp/free/login?back=index
  303. NSLog(@"%@",current_url);
  304. if([current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/swordfish/historypush",kHost]]||[current_url isEqualToString:[NSString stringWithFormat:@"%@jyapp/free/login?back=index",kHost]]){
  305. NSLog(@"展示");
  306. [self showTabBar];
  307. self.webView.frame = CGRectMake(0, -STATUS_BAR_HEIGHT, WIDTH, HEIGHT-TAB_BAR_HEIGHT+STATUS_BAR_HEIGHT);
  308. }else {
  309. NSLog(@"隐藏");
  310. [self hideTabBar];
  311. self.webView.frame = CGRectMake(0, -STATUS_BAR_HEIGHT, WIDTH, HEIGHT+STATUS_BAR_HEIGHT);
  312. }
  313. }
  314. #pragma mark 捕获js中调用的方法
  315. //捕获js中调用的方法
  316. - (void)webViewDidFinishLoad:(UIWebView *)webView{
  317. if(searchRefreshBool) {
  318. searchRefreshBool = NO;
  319. }else {
  320. //正常的网页加载
  321. [self judgeIsHidden];//是否隐藏
  322. }
  323. [self removeNoNetViews];
  324. [[LXViewControllerManager shareViewControllerManager] hideHud];
  325. JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  326. // Disable user selection
  327. [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
  328. // Disable callout
  329. [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
  330. context[@"JyObj"] = self;
  331. [context evaluateScript:@"afterPageInit()"];
  332. if (self.afterOpenPushMessage) {
  333. self.afterOpenPushMessage = NO;
  334. // [context evaluateScript:@"afterOpenPushMessage()"];
  335. [context evaluateScript:[NSString stringWithFormat:@"afterOpenPushMessage('%d')", [self jsIsCanBack:nil]]];
  336. }
  337. NSString *url = webView.request.URL.absoluteString;
  338. [self doSomeWithLoadUrl:url];
  339. }
  340. -(void)doSomeWithLoadUrl:(NSString *)url{
  341. if (!url) {
  342. return;
  343. }
  344. if ([url containsString:@"signOut"]) { // 收到退出消息后 跳转到登录页面
  345. // [self webViewFirstRequest];
  346. [self webViewRequestByMsg];
  347. }else if ([url containsString:@"/free/login"]){ // 如果通过接收消息 跳转到登录页面 展示消息内容
  348. if (self.signOutContent && ![self.signOutContent isEqualToString:@"1"]) {
  349. [[LXViewControllerManager shareViewControllerManager] showAlertViewWithTitle:@"提示" message:self.signOutContent actions:@[[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  350. self.signOutContent = nil;
  351. }]]];
  352. }else{
  353. self.signOutContent = nil;
  354. }
  355. }
  356. }
  357. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
  358. [[LXViewControllerManager shareViewControllerManager] hideHud];
  359. NSString *url = webView.request.URL.absoluteString;
  360. [self doSomeWithLoadUrl:url];
  361. }
  362. -(void)webViewDidStartLoad:(UIWebView *)webView{
  363. // if ([[UIDevice currentDevice].systemVersion floatValue] < 10) {
  364. // [[LXViewControllerManager shareViewControllerManager] hideHud];
  365. // }
  366. }
  367. - (void)didReceiveMemoryWarning {
  368. [super didReceiveMemoryWarning];
  369. // [self clearWeb];
  370. // Dispose of any resources that can be recreated.
  371. }
  372. -(void)clearWeb{
  373. NSURLCache * cache = [NSURLCache sharedURLCache];
  374. [cache removeAllCachedResponses];
  375. [cache setDiskCapacity:0];
  376. [cache setMemoryCapacity:0];
  377. }
  378. -(void)addNoNetViews{
  379. kMainQueue(^{
  380. [self removeNoNetViews];
  381. self.noNetView = [NoNetView shareView];
  382. [self.view addSubview:self.noNetView];
  383. });
  384. }
  385. -(void)removeNoNetViews{
  386. if (self.noNetView ) {
  387. [self.noNetView removeFromSuperview];
  388. self.noNetView = nil;
  389. }
  390. }
  391. #pragma mark JS 回调
  392. // 微信登录
  393. -(NSString *)jsLoginByWeixin:(NSString *)str{
  394. if ([LXUserDefaults isInstallWeiXin] != 1) { // 没有安装微信 不走友盟
  395. [[LXViewControllerManager shareViewControllerManager] showHudOnlyText:@"微信登录失败,没有安装微信" delay:1.5];
  396. return nil;
  397. }
  398. kMainQueue(^{
  399. [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:self completion:^(id result, NSError *error) {
  400. UMSocialUserInfoResponse *resp = result;
  401. [self dealWXDic:(NSDictionary *)resp.originalResponse];
  402. [[LXViewControllerManager shareViewControllerManager] hideHud];
  403. }];
  404. });
  405. return nil;
  406. }
  407. // 处理微信登录获取到的数据
  408. -(void)dealWXDic:(NSDictionary *)dictionary{
  409. [[LXViewControllerManager shareViewControllerManager] showHudText:nil];
  410. //如果获取到用户信息,即用户点击了“登录”,则进行用户信息获取
  411. if (dictionary.count > 0) {
  412. NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dictionary];
  413. if (dic) {
  414. [dic removeObjectForKey:@"privilege"];
  415. [dic removeObjectForKey:@"language"];
  416. double currDouble = [[NSDate date] timeIntervalSince1970];
  417. [dic setObject:[NSNumber numberWithLong:(long)currDouble] forKey:@"createtime"];
  418. // 拼接标志位
  419. NSString *sign = [NSString stringWithFormat:@"city=%@&country=%@&createtime=%@&headimgurl=%@&nickname=%@&openid=%@&province=%@&sex=%@&unionid=%@", dic[@"city"], dic[@"country"], dic[@"createtime"], dic[@"headimgurl"], dic[@"nickname"], dic[@"openid"], dic[@"province"],dic[@"sex"],dic[@"unionid"]];
  420. sign = [LXMD5 md532BitLower:sign];
  421. [dic setObject:sign forKey:@"sign"];
  422. NSString *endStr = [[LXViewControllerManager shareViewControllerManager] getJsonStr:dic];
  423. NSData *data = [endStr dataUsingEncoding:NSUTF8StringEncoding];
  424. endStr = [data base64EncodedStringWithOptions:0];
  425. JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  426. NSString * textJS1 = [NSString stringWithFormat:@"loginByWeixinCallBack('%@')", endStr];
  427. JSValue *value = [context evaluateScript:textJS1];
  428. NSLog(@"%@", value);
  429. }
  430. }
  431. }
  432. // js 调用分享
  433. -(void)jsShare:(NSString *)type title:(NSString *)title content:(NSString *)content link:(NSString *)link{
  434. UMSocialPlatformType shareType;
  435. if ([type intValue] == 1) { // 微信好友
  436. shareType = UMSocialPlatformType_WechatSession;
  437. }else if ([type intValue] == 2){ /// qq好友
  438. shareType = UMSocialPlatformType_QQ;
  439. }else if ([type intValue] == 3){ // 朋友圈
  440. shareType = UMSocialPlatformType_WechatTimeLine;
  441. }else{
  442. shareType = UMSocialPlatformType_WechatSession;
  443. }
  444. kMainQueue((^{
  445. // 没有安装app 不走分享逻辑
  446. if ([type intValue] == 1 || [type intValue] == 3) {
  447. if ([LXUserDefaults isInstallWeiXin] != 1) {
  448. [[LXViewControllerManager shareViewControllerManager] showHudOnlyText:@"没有安装微信" delay:1.5];
  449. return;
  450. }
  451. }
  452. // 没有安装app 不走分享逻辑
  453. if ([type intValue] == 2) {
  454. if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ]) {
  455. [[LXViewControllerManager shareViewControllerManager] showHudOnlyText:@"没有安装QQ" delay:1.5];
  456. return;
  457. }
  458. }
  459. //创建分享消息对象
  460. UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  461. //创建网页内容对象
  462. UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:[UIImage imageNamed:@"logo"]];
  463. //设置网页地址
  464. shareObject.webpageUrl = link;
  465. messageObject.shareObject = shareObject;
  466. //调用分享接口
  467. [[UMSocialManager defaultManager] shareToPlatform:shareType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
  468. if (error) {
  469. NSLog(@"22222222%@",error.userInfo);
  470. if (error.userInfo[@"message"] && [error.userInfo[@"message"] containsString:@"Operation is cancel"]) {
  471. [[LXViewControllerManager shareViewControllerManager] showHudOnlyText:@"取消分享" delay:1];
  472. }else{
  473. [[LXViewControllerManager shareViewControllerManager] showHudOnlyText:error.userInfo[@"message"] delay:1];
  474. }
  475. NSString *textJS = [NSString stringWithFormat:@"shareCallBack('%@','%@')",type,@"0"];
  476. JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  477. [context evaluateScript:textJS];
  478. }else{
  479. if ([data isKindOfClass:[UMSocialShareResponse class]]) {
  480. UMSocialShareResponse *resp = data;
  481. NSLog(@"33333%@",resp.message);
  482. NSLog(@"44444%@",resp.originalResponse);
  483. NSString *textJS = [NSString stringWithFormat:@"shareCallBack('%@','%@')",type,@"1"];
  484. JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  485. [context evaluateScript:textJS];
  486. }else{
  487. UMSocialLogInfo(@"response data is %@",data);
  488. NSLog(@"55555%@",data);
  489. }
  490. }
  491. }];
  492. }));
  493. }
  494. // 登录时获取到令牌 并保存
  495. -(void)jsSaveUserToken:(NSString *)token{
  496. [[LXViewControllerManager shareViewControllerManager] hideHud];
  497. [LXUserDefaults saveToken:token];
  498. }
  499. // 取本地的用户令牌
  500. -(NSString *)jsGetUserToken:(NSString *)str{
  501. return [LXUserDefaults token];
  502. }
  503. // 退出时 清空用户数据
  504. -(void)jsRemoveUserToken:(NSString *)str{
  505. [LXUserDefaults clearAll];
  506. // [[LXDataBaseManager shareDataBaseManager] clearAll];
  507. }
  508. // 手机通知权限是否打开
  509. -(int)jsCheckNoticePermission:(NSString *)str{
  510. int a = [LXUserDefaults notifyState];
  511. return a;
  512. }
  513. // 跳转到系统设置
  514. -(void)jsOpenSystemNotification:(NSString *)str{
  515. self.updateNotify = YES;
  516. kMainQueue(^{
  517. [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  518. });
  519. }
  520. // 调用js方法 刷新推送权限状态
  521. -(void)updateNotifyState{
  522. if (self.updateNotify) {
  523. self.updateNotify = NO;
  524. JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  525. NSString * textJS1 = [NSString stringWithFormat:@"updateNoticeSwitch()"];
  526. [context evaluateScript:textJS1];
  527. }
  528. }
  529. // 获取推送id
  530. -(NSString *)jsGetPushRid:(NSString *)str{
  531. return [LXUserDefaults registrationID];
  532. }
  533. // 按通知取数据
  534. -(NSString *)jsGetMessageList:(NSString *)openid{
  535. NSArray *arr = [[LXDataBaseManager shareDataBaseManager] getMessageWithKey:nil openid:openid fromTable:kNewsTable start:0 length:100];
  536. if (arr.count > 0) {
  537. return [[LXViewControllerManager shareViewControllerManager] getJsonStr:arr];
  538. }
  539. return @"";
  540. }
  541. // 更新消息状态
  542. -(void)jsUpdateMessageState:(NSNumber *)lxID state:(NSNumber *)state{
  543. [[LXDataBaseManager shareDataBaseManager] changeReadedWithID:lxID state:state];
  544. }
  545. // 删除一条数据
  546. -(void)jsDeleteMessageById:(NSNumber *)lxID{
  547. [[LXDataBaseManager shareDataBaseManager] deleteMessage:lxID fromTable:kNewsTable];
  548. }
  549. // 取单条数据
  550. -(NSString *)jsGetMessage:(NSString *)lxID{
  551. return [[LXViewControllerManager shareViewControllerManager] getJsonStr:[[LXDataBaseManager shareDataBaseManager] getMessageWithKey:lxID openid:nil fromTable:kNewsTable start:0 length:0]];
  552. }
  553. // 取未读消息总数
  554. -(int)jsGetUnReadMessageCount:(NSString *)openid{
  555. NSLog(@"openid=%@",openid);
  556. // NSLog(@"%d",[[LXDataBaseManager shareDataBaseManager] getUnReadMessageCount:openid]);
  557. return [[LXDataBaseManager shareDataBaseManager] getUnReadMessageCount:openid];
  558. }
  559. // 全部置为已读
  560. -(void)jsUpdateAllUnReadMessage:(NSString *)openid{
  561. [[LXDataBaseManager shareDataBaseManager] updateAllUnReadMessage:openid];
  562. }
  563. // 改变状态栏文字颜色
  564. -(void)jsLightStatusBar:(NSString *)status{
  565. kMainQueue(^{
  566. NSInteger index = 0;
  567. if ([status intValue] == 1) {
  568. index = 1;// hei
  569. }else{
  570. index = 0;// bai
  571. }
  572. [[UIApplication sharedApplication] setStatusBarStyle:index];
  573. });
  574. }
  575. // 打开新页面 加载新web
  576. -(void)jsOpenExternalLink:(NSString *)url title:(NSString *)title{
  577. if (url) {
  578. if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable || [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) {
  579. // [self addNoNetViews];
  580. }
  581. NewWebController *ctr = [[NewWebController alloc] init];
  582. ctr.url = url;
  583. // 有title的话 会显示自定义的导航栏 没有的话不显示导航栏 显示加载进度条
  584. if (!title || [title isEqualToString:@""] || [title isEqualToString:@"undefined"]) {
  585. ctr.titleShow = nil;
  586. }else{
  587. ctr.titleShow = title;
  588. }
  589. kMainQueue(^{
  590. [self presentViewController:ctr animated:NO completion:nil];
  591. });
  592. }
  593. }
  594. // 获取当前版本号
  595. -(NSString *)jsGetVersion:(NSString *)str{
  596. return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  597. }
  598. // web是否可退回上页面
  599. -(int)jsIsCanBack:(NSString *)str{
  600. return self.webView.canGoBack ? 1:0;
  601. }
  602. // 弹框
  603. -(void)jsAlert:(NSString *)content{
  604. [[LXViewControllerManager shareViewControllerManager] showAlertViewWithMessage:content];
  605. }
  606. // 是否安装微信
  607. -(int)jsIsInstallWeixin:(NSString *)str{
  608. return [LXUserDefaults isInstallWeiXin];
  609. }
  610. -(void)jsChangeMessageType:(NSString *)link
  611. {
  612. //已读操作处理
  613. NSLog(@"已读操作处理-更改");
  614. [[LXDataBaseManager shareDataBaseManager] changeCatagoryTypeWithURL:link];
  615. }
  616. -(void)jsOpenActivityPage:(NSString *)url rectype:(NSString *)rectype openid:(NSString *)openid
  617. {
  618. NSDictionary * infoDic = @{@"url":url,
  619. @"rectype":rectype,
  620. @"openid":openid,
  621. };
  622. // NSString *textJS = [NSString stringWithFormat:@"messageAccept('%@','%@','%@')",infoDic[@"mid"],infoDic[@"rectype"],infoDic[@"openid"]];
  623. // JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  624. // [context evaluateScript:textJS];
  625. //post请求
  626. NSLog(@"%@",[NSString stringWithFormat:@"%@jyapp/free/message/receive",kHost]);
  627. [[LXNetworkManager shareLXNetworkManager]postWithURLStr:[NSString stringWithFormat:@"%@jyapp/free/message/receive",kHost] parameters:infoDic progress:^(NSProgress *progress) {
  628. NSLog(@"成功:%@",progress);
  629. } success:^(id responseObject) {
  630. NSLog(@"responseObject=%@",responseObject);
  631. } failure:^(NSError *error) {
  632. NSLog(@"error=%@",error);
  633. }];
  634. }
  635. -(NSString *)jsGGetCipherText:(NSString *)phone
  636. {
  637. NSDate *date=[NSDate date];
  638. NSDateFormatter *format1=[[NSDateFormatter alloc] init];
  639. [format1 setDateFormat:@"yyyyMMddhhmmss"];
  640. NSString *dateStr;
  641. dateStr=[format1 stringFromDate:date];
  642. NSLog(@"%@",dateStr);
  643. NSString *content = [[NSString stringWithFormat:@"%@_%@",phone,dateStr] stringByAppendingString:[NSString stringWithFormat:@"_%@",[LXMD5 md532BitLower:[NSString stringWithFormat:@"%@&%@",phone,dateStr]]]];
  644. NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
  645. NSUInteger dataLength = contentData.length;
  646. // 为结束符'\\0' +1
  647. char keyPtr[kKeySize_1 + 1];
  648. memset(keyPtr, 0, sizeof(keyPtr));
  649. [AesKey_1 getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  650. // 密文长度 <= 明文长度 + BlockSize
  651. size_t encryptSize = dataLength + kCCBlockSizeAES128;
  652. void *encryptedBytes = malloc(encryptSize);
  653. size_t actualOutSize = 0;
  654. NSData *initVector = [kInitVector_1 dataUsingEncoding:NSUTF8StringEncoding];
  655. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
  656. kCCAlgorithmAES,
  657. kCCOptionPKCS7Padding, // 系统默认使用 CBC,然后指明使用 PKCS7Padding
  658. keyPtr,
  659. kKeySize_1,
  660. initVector.bytes,
  661. contentData.bytes,
  662. dataLength,
  663. encryptedBytes,
  664. encryptSize,
  665. &actualOutSize);
  666. if (cryptStatus == kCCSuccess) {
  667. // 对加密后的数据进行 base64 编码
  668. return [[NSData dataWithBytesNoCopy:encryptedBytes length:actualOutSize] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  669. }
  670. free(encryptedBytes);
  671. return nil;
  672. }
  673. //通知刷新页面
  674. -(void)refreshDY:(NSNotification *)notificaition{
  675. [self webViewFirstRequest];
  676. }
  677. //首页登录通知刷新页面
  678. -(void)refreshAllVC:(NSNotification *)notificaition{
  679. searchRefreshBool = YES;
  680. [self webViewFirstRequest];
  681. }
  682. -(void)jsBackUrl:(NSString*)str
  683. {
  684. NSLog(@"Mine返回首页%@",str);
  685. if([str isEqualToString:@"H"]) {
  686. //测试返回首页
  687. kMainQueue(^{
  688. AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  689. JYTabBarController *tab = (JYTabBarController *)delegate.window.rootViewController;
  690. tab.selectedIndex = 0;
  691. [[NSNotificationCenter defaultCenter] postNotificationName:@"changeItems" object:nil userInfo:@{@"item_index":@"0"}];
  692. [self dealTabBar];
  693. });
  694. }
  695. }
  696. -(void)jsLoginSuccess:(NSString*)status
  697. {
  698. NSLog(@"Mine登录成功提示");
  699. if([status isEqualToString:@"S"]) {
  700. kMainQueue(^{
  701. AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  702. JYTabBarController *tab = (JYTabBarController *)delegate.window.rootViewController;
  703. tab.selectedIndex = 0;
  704. [[NSNotificationCenter defaultCenter] postNotificationName:@"changeItems" object:nil userInfo:@{@"item_index":@"5"}];
  705. });
  706. }else {
  707. }
  708. }
  709. -(void)jsHideRedSpotOnMenu:(NSString*)str
  710. {
  711. NSLog(@"dy:%@",str);
  712. kMainQueue(^{
  713. [self.tabBarController.tabBar hideBadgeOnItemIndex:1];
  714. });
  715. }
  716. /*
  717. #pragma mark - Navigation
  718. // In a storyboard-based application, you will often want to do a little preparation before navigation
  719. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  720. // Get the new view controller using [segue destinationViewController].
  721. // Pass the selected object to the new view controller.
  722. }
  723. */
  724. @end