猎豆优选

LDWebPageViewController.m 8.9KB

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