JYMineViewController.m 36 KB

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