CNLiveUserAgreementController.m 10.7 KB
//
//  CNLiveUserAgreementController.h
//  CNLiveUserAgreementManager
//
//  Created by 153993236@qq.com on 04/13/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveUserAgreementController.h"
#import "QMUIKit.h"
#import "WKWebViewJavascriptBridge.h"

#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "XFCameraController.h"
#import "TZImagePickerController.h"
#import <CNLiveRequestBastKit/CNLiveNetworking.h>
#import "CNLiveManager.h"
#import "CNLiveEnvironment.h"

#import "CNLiveWebView.h"

@interface CNLiveUserAgreementController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate>
@property (nonatomic, strong) NSMutableArray *images;

@end

@implementation CNLiveUserAgreementController
#define wjjHelpAndFeedbackImage @"wjjHelpAndFeedbackImage"   //帮助与反馈->产品建议:添加照片按钮
#define wjjSubmitImage          @"wjjSubmitImage"            //帮助与反馈->产品建议:点击提交
#define wjjDelegateImage        @"wjjDelegateImage"          //帮助与反馈->产品建议:删除照片
#define wjjSubmitSuccessFinal   @"wjjSubmitSuccessFinal"     //反馈提交(成功/失败)后隐藏loading
#define wjjFeedbackSuccess      @"wjjFeedbackSuccess"        //帮助与反馈 提交反馈后点击完成
#define wjjJSGetNetStatusFromClient @"JSGetNetStatusFromClient" //h5主动从客户端获取网络状态
#define OCSendNetStatusToJS @"OCSendNetStatusToJS" //网络状态变化信息
#define wjjSubmitSuccess    @"wjjSubmitSuccess"    //帮助与反馈->产品建议:App->h5图片urls

- (void)createSubViews {
    [super createSubViews];
    UIColor *color = [UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0];
    self.webView.progressViewColor = color;
}
#pragma mark - 交互
- (void)interaction {
    [super interaction];
    __weak typeof(self)weakSelf = self;
    
    // 帮助与反馈->产品建议:添加照片按钮
    [self.bridge registerHandler:wjjHelpAndFeedbackImage handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        [weakSelf uploadPictures];
        responseCallback(data);
    }];
    
    // 帮助与反馈->产品建议:删除照片
    [self.bridge registerHandler:wjjDelegateImage handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString *index = [data objectForKey:@"index"];
            [weakSelf.images removeObjectAtIndex:[index integerValue]];
        }
        responseCallback(data);
    }];
    
    //帮助与反馈->产品建议:点击提交
    [self.bridge registerHandler:wjjSubmitImage handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        NSString *text = @"";
        NSString *mobile = @"";
        NSString *modeltype = @"";
        if ([data isKindOfClass:[NSDictionary class]]) {
            text = [data objectForKey:@"text"];
            mobile = [data objectForKey:@"mobile"];
            modeltype = [data objectForKey:@"modeltype"];
        }
        if (weakSelf.images.count > 0) {
            [weakSelf getToken];
        } else {
            NSArray *array = [NSArray array];
            id data = @{ @"images": array };
            [QMUITips showLoading:@"正在提交" inView:weakSelf.view];
            // 帮助与反馈->产品建议App->h5图片urls
            [weakSelf.bridge callHandler:wjjSubmitSuccess data:data responseCallback:^(id responseData) {
                NSLog(@"ObjC received response: %@", responseData);
            }];
        }
        responseCallback(data);
    }];
    
    // 反馈提交(成功/失败)后隐藏loading
    [self.bridge registerHandler:wjjSubmitSuccessFinal handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        [QMUITips hideAllTipsInView:weakSelf.view];
        responseCallback(data);
    }];
    
    // 帮助与反馈 提交反馈后点击完成
    [self.bridge registerHandler:wjjFeedbackSuccess handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        [weakSelf.navigationController popViewControllerAnimated:YES];
        responseCallback(data);
    }];
    
    // h5主动从客户端获取网络状态
    [self.bridge registerHandler:wjjJSGetNetStatusFromClient handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC Echo called with: %@", data);
        // 向h5传递网路状态信息
        NSInteger networkStatus = 0;
        if (![CNLiveNetworking isNetworking]) {
            networkStatus = 0;
        }
        else if ([CNLiveNetworking isWWANNetwork]) {
            networkStatus = 1;
        }
        else if ([CNLiveNetworking isWiFiNetwork]) {
            networkStatus = 2;
        }
        id answerData = @{ @"netStatus": [NSString stringWithFormat:@"%ld",(long)networkStatus] };
        [weakSelf.bridge callHandler:OCSendNetStatusToJS data:answerData responseCallback:^(id responseData) {
            NSLog(@"ObjC received response: %@", responseData);
        }];
        responseCallback(data);
    }];
}

#pragma mark - Setter方法
- (void)setUrl:(NSString *)url{
    if(!url||[url isEqualToString:@""]) return;
    [super setUrl:[NSString stringWithFormat:@"%@",url]];
}

#pragma mark - CNLiveWebViewDelegate
- (void)webView:(CNLiveWebView *)webView didFinishLoadWithURL:(NSURL *)url {
    if(self.title.length == 0){
        self.title = webView.title;
    }
}

- (void)webView:(CNLiveWebView *)webView didFailLoadWithError:(NSError *)error {
    
}

#pragma mark - 添加图片
- (void)uploadPictures{
    __weak typeof(self) weakSelf = self;
    QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"拍照" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(!strongSelf) return ;
        __strong typeof(strongSelf) weakSelf = strongSelf;
        [CNLiveAuthorizationManager getAVAuthorizationStatus:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if(!strongSelf) return ;
            XFCameraController *cameraController = [[XFCameraController alloc] init];
            cameraController.isCanShootVideo = NO;
            cameraController.modalPresentationStyle = UIModalPresentationFullScreen;
            __weak XFCameraController *weakCameraController = cameraController;
            __weak typeof(strongSelf) weakSelf = strongSelf;
            cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
                __strong typeof(weakSelf) strongSelf = weakSelf;
                if(!strongSelf) return ;
                [strongSelf.images addObject:image];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakCameraController dismissViewControllerAnimated:YES completion:nil];
                });
            };
            [strongSelf presentViewController:cameraController animated:YES completion:nil];
        }];

    }];
    QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"从手机相册中选择" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(!strongSelf) return ;
        __strong typeof(strongSelf) weakSelf = strongSelf;
        [CNLiveAuthorizationManager getPHAuthorizationStatus:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if(!strongSelf) return ;
            TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:strongSelf];
            picker.modalPresentationStyle = UIModalPresentationFullScreen;
            picker.allowPickingVideo = NO;
            picker.allowPreview = NO;
            __weak typeof(strongSelf) weakSelf = strongSelf;
            [picker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                __strong typeof(weakSelf) strongSelf = weakSelf;
                if(!strongSelf) return ;
                [strongSelf.images addObject:[photos firstObject]];
            }];
            [strongSelf presentViewController:picker animated:YES completion:nil];
        }];

    }];
    QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
          __strong typeof(weakSelf) strongSelf = weakSelf;
          if(!strongSelf) return ;

      }];
           
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:cancel];
    
    NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
    titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0];
    
    NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
    cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];

    action1.buttonAttributes = titleAttributs;
    action2.buttonAttributes = titleAttributs;
    cancel.buttonAttributes = cancelAttributs;

    [alertController showWithAnimated:YES];

}

// 获取图片的token
- (void)getToken {
    NSDictionary *param = @{@"appId":AppId,
                            @"verb":@"POST",
                            @"storageType":@"2",
                            @"bucket":API_BucketName
                            };
    [QMUITips showLoading:@"正在提交" inView:self.view];
    [CNLiveNetworking setAllowRequestDefaultArgument:NO];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNUploadAuthForApp Param:param CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"]];
        NSDictionary *dic = (NSDictionary *)data;
        if (!error && dic[@"token"]) {
            //            [self QNStartUploadWithtoken:dic[@"token"]];
        }else {
            [QMUITips hideAllToastInView:self.view animated:YES];
            [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:1.5];
        }
    }];
}

@end