口袋优选

KBWebPageViewController.m 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // KBWebPageViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/31.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBWebPageViewController.h"
  9. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  10. #import <AlibabaAuthSDK/ALBBSession.h>
  11. #import <AlibabaAuthSDK/ALBBSDK.h>
  12. #import "KBTaobaoAuthorView.h"
  13. #import "KBShopPushWebViewController.h"
  14. #import "JsonTool.h"
  15. @interface KBWebPageViewController ()<UIWebViewDelegate>{
  16. ActivityIndicatorView *_indicatorView;
  17. }
  18. @property (nonatomic, copy) NSString *jsString;
  19. @property (nonatomic, strong) UIButton *backButton;
  20. @property (nonatomic, strong) UIButton *closeButton;
  21. @end
  22. @implementation KBWebPageViewController
  23. - (instancetype)init {
  24. self = [super init];
  25. if (self) {
  26. }
  27. return self;
  28. }
  29. - (void)viewWillDisappear:(BOOL)animated {
  30. [super viewWillDisappear:animated];
  31. [SVProgressHUD dismiss];
  32. }
  33. - (void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. [self cleanCacheAndCookie];
  39. [self addObserForAppWillEnterForeground];
  40. [self configNavigationBar];
  41. [self configWebView];
  42. [self initHUD];
  43. [self configTaobaoAuthorView];
  44. }
  45. - (void)addObserForAppWillEnterForeground {
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(reloadWebNoti)
  48. name:UIApplicationWillEnterForegroundNotification object:nil];
  49. }
  50. /**
  51. 从后台进入前台刷新
  52. */
  53. - (void)reloadWebNoti {
  54. if ([[ALBBSession sharedInstance] isLogin]){
  55. [self openAliMyOrderWebView];
  56. }
  57. }
  58. /**
  59. 清除web缓存
  60. */
  61. - (void)cleanCacheAndCookie{
  62. //清除cookies
  63. // NSHTTPCookie *cookie;
  64. //
  65. // NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  66. //
  67. // for (cookie in [storage cookies]){
  68. //
  69. // [storage deleteCookie:cookie];
  70. //
  71. // }
  72. //清除UIWebView的缓存
  73. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  74. NSURLCache * cache = [NSURLCache sharedURLCache];
  75. [cache removeAllCachedResponses];
  76. [cache setDiskCapacity:0];
  77. [cache setMemoryCapacity:0];
  78. }
  79. - (void)configTaobaoAuthorView {
  80. [self openAliMyOrderWebView];
  81. }
  82. - (void)loadHTMLJSSting {
  83. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/backJsStr",BaseURL];
  84. [KBHttp post:url params:nil success:^(id json) {
  85. self.jsString = json[@"jsStr"];
  86. [self getGoosList];
  87. } failure:^(NSError *error) {
  88. }];
  89. }
  90. - (void)initHUD {
  91. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.webView frame:self.webView.bounds];
  92. _indicatorView = indicatorView;
  93. }
  94. - (void)configWebView {
  95. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  96. self.webView.delegate = self;
  97. [self.view addSubview:self.webView];
  98. }
  99. - (void)configNavigationBar {
  100. self.view.backgroundColor = [UIColor whiteColor];
  101. self.navigationBar.showNavigationBarBottomLine = YES;
  102. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  103. }
  104. - (void)backAction {
  105. if (self.webView.canGoBack) {
  106. [self.webView goBack];
  107. }else{
  108. [self.navigationController popViewControllerAnimated:YES];
  109. }
  110. }
  111. - (void)closeAction {
  112. [self.navigationController popViewControllerAnimated:YES];
  113. }
  114. - (void)updateNavigationBarButtons {
  115. if (self.webView.canGoBack) {
  116. [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
  117. }else {
  118. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  119. }
  120. }
  121. - (void)openAliMyOrderWebView {
  122. // [SVProgressHUD show];
  123. switch (self.openPage) {
  124. case AlibcOpenPageShopCar:
  125. {
  126. id<AlibcTradePage> page = [AlibcTradePageFactory myCartsPage];
  127. [self openWebViewByPage:page];
  128. }
  129. break;
  130. case AlibcOpenPageMyOrder:
  131. {
  132. id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
  133. [self openWebViewByPage:page];
  134. }
  135. break;
  136. default:
  137. {
  138. id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
  139. [self openWebViewByPage:page];
  140. }
  141. break;
  142. }
  143. }
  144. - (void)openWebViewByPage:(id<AlibcTradePage>)page {
  145. //淘客信息
  146. AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
  147. taoKeParams.pid = ALTK_PID;
  148. //打开方式
  149. AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
  150. showParam.openType = AlibcOpenTypeH5;
  151. [[AlibcTradeSDK sharedInstance].tradeService show:self webView:self.webView page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
  152. } tradeProcessFailedCallback:^(NSError * _Nullable error) {
  153. }];
  154. self.webView.delegate = self;
  155. }
  156. #pragma mark -------------- UIWebView delegate --------
  157. #pragma mark -- 拦截webview用户触击了一个链接
  158. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  159. {
  160. //判断是否是单击
  161. if (navigationType == UIWebViewNavigationTypeLinkClicked)
  162. {
  163. NSString *url = [request.URL absoluteString];
  164. //拦截链接跳转到货源圈的动态详情
  165. if ([url rangeOfString:@"http"].location != NSNotFound)
  166. {
  167. //跳转到你想跳转的页面
  168. KBShopPushWebViewController *web = [[KBShopPushWebViewController alloc] init];
  169. web.url = url;
  170. [self.navigationController pushViewController:web animated:YES];
  171. return NO; //返回NO,此页面的链接点击不会继续执行,只会执行跳转到你想跳转的页面
  172. }
  173. }
  174. return YES;
  175. }
  176. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  177. [self updateNavigationBarButtons];
  178. // [SVProgressHUD dismiss];
  179. NSString *jsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";
  180. NSString *HTMLSource = [self.webView stringByEvaluatingJavaScriptFromString:jsToGetHTMLSource];
  181. if (self.openPage == AlibcOpenPageShopCar) {
  182. [self loadHTMLJSSting];
  183. }
  184. [_indicatorView stopAnimating];
  185. }
  186. - (void)getGoosList {
  187. if (self.jsString.length == 0) {
  188. return;
  189. }
  190. [self.webView stringByEvaluatingJavaScriptFromString:self.jsString];
  191. NSString *goodsStr = [self.webView stringByEvaluatingJavaScriptFromString:@"getShopCarGoods();"];
  192. NSDictionary *dic = [JsonTool dictionaryWithJsonString:goodsStr];
  193. NSArray *goods = dic[@"goods"];
  194. if (goods.count > 0) {
  195. [self uploadGoodsId:goods];
  196. }else {
  197. NSDictionary *userInfo = @{@"goodsIdStr":@[]};
  198. NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo];
  199. [[NSNotificationCenter defaultCenter] postNotification:noti];
  200. }
  201. }
  202. - (void)uploadGoodsId:(NSArray *)goodArr {
  203. NSDictionary *userInfo = @{@"goodsIdStr":goodArr};
  204. NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo];
  205. [[NSNotificationCenter defaultCenter] postNotification:noti];
  206. }
  207. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  208. [SVProgressHUD dismiss];
  209. }
  210. #pragma mark ------
  211. - (UIButton *)backButton {
  212. if (!_backButton) {
  213. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  214. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  215. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  216. }
  217. return _backButton;
  218. }
  219. - (UIButton *)closeButton {
  220. if (!_closeButton) {
  221. _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  222. [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  223. [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  224. }
  225. return _closeButton;
  226. }
  227. - (NSArray *)stringToJSON:(NSString *)jsonStr {
  228. if (jsonStr) {
  229. id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
  230. if (tmp) {
  231. if([tmp isKindOfClass:[NSArray class]]) {
  232. return tmp;
  233. }else if([tmp isKindOfClass:[NSString class]]|| [tmp isKindOfClass:[NSDictionary class]]){
  234. return [NSArray arrayWithObject:tmp];
  235. } else {
  236. return nil;
  237. }
  238. }else {
  239. return nil;
  240. }
  241. } else {
  242. return nil;
  243. }
  244. }
  245. - (void)didReceiveMemoryWarning {
  246. [super didReceiveMemoryWarning];
  247. // Dispose of any resources that can be recreated.
  248. }
  249. /*
  250. #pragma mark - Navigation
  251. // In a storyboard-based application, you will often want to do a little preparation before navigation
  252. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  253. // Get the new view controller using [segue destinationViewController].
  254. // Pass the selected object to the new view controller.
  255. }
  256. */
  257. @end