JYMineViewController.m 36 KB

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