JYSubViewController.m 38 KB

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