123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- //
- // HSQWebPageViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/31.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "HSQWebPageViewController.h"
- #import <AlibcTradeSDK/AlibcTradeSDK.h>
- #import <AlibabaAuthSDK/ALBBSession.h>
- #import <AlibabaAuthSDK/ALBBSDK.h>
- #import "HSQTaobaoAuthorView.h"
- #import "KBShopPushWebViewController.h"
- #import "JsonTool.h"
- @interface HSQWebPageViewController ()<UIWebViewDelegate>{
- ActivityIndicatorView *_indicatorView;
- }
- @property (nonatomic, copy) NSString *jsString;
- @property (nonatomic, strong) UIButton *backButton;
- @property (nonatomic, strong) UIButton *closeButton;
- @end
- @implementation HSQWebPageViewController
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.webView.delegate = self;
- }
- return self;
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [SVProgressHUD dismiss];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self addObserForAppWillEnterForeground];
- [self configNavigationBar];
- [self configWebView];
- [self initHUD];
- [self configTaobaoAuthorView];
-
- }
- - (void)addObserForAppWillEnterForeground {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(reloadWebNoti)
- name:UIApplicationWillEnterForegroundNotification object:nil];
- }
- /**
- 从后台进入前台刷新
- */
- - (void)reloadWebNoti {
- if ([[ALBBSession sharedInstance] isLogin]){
- [self openAliMyOrderWebView];
- }
- }
- /**
- 清除web缓存
- */
- - (void)cleanCacheAndCookie{
-
- //清除cookies
-
- // NSHTTPCookie *cookie;
- //
- // NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- //
- // for (cookie in [storage cookies]){
- //
- // [storage deleteCookie:cookie];
- //
- // }
-
- //清除UIWebView的缓存
-
- [[NSURLCache sharedURLCache] removeAllCachedResponses];
-
- NSURLCache * cache = [NSURLCache sharedURLCache];
-
- [cache removeAllCachedResponses];
-
- [cache setDiskCapacity:0];
-
- [cache setMemoryCapacity:0];
-
- }
- - (void)configTaobaoAuthorView {
- [self openAliMyOrderWebView];
- }
- - (void)loadHTMLJSSting {
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/backJsStr",BaseURL];
- [HSQHttp post:url params:nil success:^(id json) {
- self.jsString = json[@"jsStr"];
- [self getGoosList];
- } failure:^(NSError *error) {
-
- }];
- }
- - (void)initHUD {
-
- ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.webView frame:self.webView.bounds];
- _indicatorView = indicatorView;
- }
- - (void)configWebView {
-
- [self.view addSubview:self.webView];
-
-
- }
- - (void)configNavigationBar {
-
- self.view.backgroundColor = [UIColor whiteColor];
-
- self.navigationBar.showNavigationBarBottomLine = YES;
-
-
- [self.navigationBar setCustomLeftButtons:@[self.backButton]];
-
- }
- - (void)backAction {
- if (self.webView.canGoBack) {
- [self.webView goBack];
- }else{
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- - (void)closeAction {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)updateNavigationBarButtons {
- if (self.webView.canGoBack) {
- [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
- }else {
- [self.navigationBar setCustomLeftButtons:@[self.backButton]];
- }
- }
- - (void)openAliMyOrderWebView {
- // [SVProgressHUD show];
-
- switch (self.openPage) {
- case AlibcOpenPageShopCar:
- {
- id<AlibcTradePage> page = [AlibcTradePageFactory myCartsPage];
- [self openWebViewByPage:page];
- }
-
- break;
- case AlibcOpenPageMyOrder:
- {
- id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
- [self openWebViewByPage:page];
- }
- break;
-
- default:
- {
- id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
- [self openWebViewByPage:page];
- }
- break;
- }
-
-
- }
- - (void)openWebViewByPage:(id<AlibcTradePage>)page {
- //淘客信息
- AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
- taoKeParams.pid = ALTK_PID;
- //打开方式
- AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
-
- showParam.openType = AlibcOpenTypeH5;
-
- [[AlibcTradeSDK sharedInstance].tradeService show:self webView:self.webView page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
-
- } tradeProcessFailedCallback:^(NSError * _Nullable error) {
-
- }];
- }
- #pragma mark -------------- UIWebView delegate --------
- #pragma mark -- 拦截webview用户触击了一个链接
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
- {
- //判断是否是单击
- if (navigationType == UIWebViewNavigationTypeLinkClicked)
- {
- NSString *url = [request.URL absoluteString];
-
- //拦截链接跳转到货源圈的动态详情
- if ([url rangeOfString:@"http"].location != NSNotFound)
- {
- //跳转到你想跳转的页面
- KBShopPushWebViewController *web = [[KBShopPushWebViewController alloc] init];
- web.url = url;
- [self.navigationController pushViewController:web animated:YES];
- return NO; //返回NO,此页面的链接点击不会继续执行,只会执行跳转到你想跳转的页面
- }
- }
- return YES;
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView {
- [self updateNavigationBarButtons];
- // [SVProgressHUD dismiss];
- // NSString *jsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";
- // NSString *HTMLSource = [self.webView stringByEvaluatingJavaScriptFromString:jsToGetHTMLSource];
-
- if (self.openPage == AlibcOpenPageShopCar) {
- [self loadHTMLJSSting];
- }
- [_indicatorView stopAnimating];
-
- }
- - (void)getGoosList {
- if (self.jsString.length == 0) {
- return;
- }
-
- [self.webView stringByEvaluatingJavaScriptFromString:self.jsString];
- NSString *goodsStr = [self.webView stringByEvaluatingJavaScriptFromString:@"getShopCarGoods();"];
- NSDictionary *dic = [JsonTool dictionaryWithJsonString:goodsStr];
- NSArray *goods = dic[@"goods"];
- if (goods.count > 0) {
- [self uploadGoodsId:goods];
- }else {
- NSDictionary *userInfo = @{@"goodsIdStr":@[]};
- NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo];
- [[NSNotificationCenter defaultCenter] postNotification:noti];
- }
- }
- - (void)uploadGoodsId:(NSArray *)goodArr {
-
- NSDictionary *userInfo = @{@"goodsIdStr":goodArr};
- NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo];
- [[NSNotificationCenter defaultCenter] postNotification:noti];
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
- [SVProgressHUD dismiss];
- }
- #pragma mark ------
- - (UIButton *)backButton {
- if (!_backButton) {
- _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
- [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _backButton;
- }
- - (UIButton *)closeButton {
- if (!_closeButton) {
- _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
- [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _closeButton;
- }
- - (NSArray *)stringToJSON:(NSString *)jsonStr {
- if (jsonStr) {
- id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
- if (tmp) {
- if([tmp isKindOfClass:[NSArray class]]) {
- return tmp;
- }else if([tmp isKindOfClass:[NSString class]]|| [tmp isKindOfClass:[NSDictionary class]]){
- return [NSArray arrayWithObject:tmp];
- } else {
- return nil;
- }
- }else {
- return nil;
- }
- } else {
- return nil;
- }
- }
- - (UIWebView *)webView {
- if (!_webView) {
- _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
- }
- return _webView;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|