线上所有马甲包模板,与《猎豆》同UI。域名zhuadd

HSQWebPageViewController.m 9.5KB

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