JYSearchViewController.m 38 KB

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