JYExpViewController.m 35 KB

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