JYExpViewController.m 34 KB

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