JYSubViewController.m 32 KB

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