JYSubViewController.m 31 KB

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