123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- //
- // KSCloudWebManager.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2024/1/19.
- //
- #import "KSCloudWebManager.h"
- #import <CloudAccompanyLibrary/CloudWebConfig.h>
- #import <CloudAccompanyLibrary/KSCloudWebViewController.h>
- #import <KSToolLibrary/RecordCheckManager.h>
- #import "KSPremissionAlert.h"
- #import "KSBaseWKWebViewController.h"
- #import "KSMediaMergeView.h"
- #import <KSToolLibrary/UIDevice+TFDevice.h>
- #import "AppDelegate+AppService.h"
- #import "AccompanyLoadingView.h"
- #import "KSWebLoadRefreshView.h"
- @interface KSCloudWebManager ()<KSCloudWebViewControllerDelegate>
- @property (nonatomic, strong) AccompanyLoadingView *customLoading;
- @property (nonatomic, strong) KSWebLoadRefreshView *errorView;
- @end
- @implementation KSCloudWebManager
- + (instancetype)shareInstance {
-
- static KSCloudWebManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[KSCloudWebManager alloc] init];
- });
- return manager;
- }
- - (void)configCloudWebSource {
- CLOUD_CONFIG.scrpit_name = SCRIPT_NAME;
- CLOUD_CONFIG.agent_name = AGENT_NAME;
- CLOUD_CONFIG.agent_domain = AGENT_DOMAIN;
- CLOUD_CONFIG.token_type = UserDefault(Token_type);
- CLOUD_CONFIG.current_token = UserDefault(TokenKey);
- CLOUD_CONFIG.user_ID = UserDefault(UIDKey);
- CLOUD_CONFIG.socket_host = SOCKET_URL;
- CLOUD_CONFIG.authFilePath = CERT_PATH;
- CLOUD_CONFIG.need_auth = SSL_AUTH;
- CLOUD_CONFIG.themeColor = CLIENT_THEMECOLOR;
- CLOUD_CONFIG.soundFilePath = [[NSBundle mainBundle] pathForResource:@"synthgms" ofType:@"sf2"];
- }
- - (void)showWebView:(NSDictionary *)parm fromController:(CustomNavViewController *)navCtrl {
- // 先检测相机 再检测麦克风
- [RecordCheckManager checkCameraPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
-
- [self afterCheckCameraCheckMic:type parm:parm fromController:navCtrl];
- }];
- }
- - (void)afterCheckCameraCheckMic:(PREMISSIONTYPE)cameraType parm:(NSDictionary *)sourceParm fromController:(CustomNavViewController *)navCtrl {
-
- [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) {
- if (type == PREMISSIONTYPE_YES && cameraType == PREMISSIONTYPE_YES) {
- NSDictionary *valueDic = [sourceParm ks_dictionaryValueForKey:@"content"];
-
- KSCloudWebViewController *ctrl = [[KSCloudWebViewController alloc] init];
- ctrl.webViewDelegate = self;
- ctrl.url = [valueDic ks_stringValueForKey:@"url"];
- ctrl.parmDic = valueDic;
- NSInteger orientation = [valueDic ks_integerValueForKey:@"orientation"];
- BOOL isLandScape = orientation == 0 ? YES : NO;
- ctrl.ks_landScape = isLandScape;
- [navCtrl pushViewController:ctrl animated:YES];
- }
- else { //
-
- NSString *content = @"";
- CHECKDEVICETYPE checkType = CHECKDEVICETYPE_BOTH;
- if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_NO) {
- content = @"请开启相机和麦克风访问权限";
- checkType = CHECKDEVICETYPE_BOTH;
- }
- else if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_YES) {
- content = @"请开启相机访问权限";
- checkType = CHECKDEVICETYPE_CAMREA;
- }
- else if (cameraType == PREMISSIONTYPE_YES && type == PREMISSIONTYPE_NO) {
- content = @"请开启麦克风访问权限";
- checkType = CHECKDEVICETYPE_MIC;
- }
- [self showAlertWithMessage:content type:checkType];
- }
- }];
- }
- - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType {
- [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:[NSObject getKeyWindow] cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
-
- }
- - (void)openSettingView {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- }
- // 回调权限
- - (void)NotiferShowPremissAlert:(NSString *)message {
- [KSPremissionAlert shareInstanceDisplayImage:CHECKDEVICETYPE_BOTH message:message showInView:[NSObject getKeyWindow] cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
- }
- // 下载
- - (void)notiferDownloadFileUrl:(NSString *)url success:(void (^)(NSURL * _Nonnull, NSString * _Nonnull))success faliure:(void (^)(void))faliure {
- [KSNetworkingManager downloadFileRequestWithFileUrl:url progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- if (success) {
- success(fileUrl,url);
- }
- } faliure:^(NSError * _Nonnull error) {
- if (faliure) {
- faliure();
- }
- }];
- }
- // 上传
- - (void)uploadFile:(NSString *)uploadFileUrl success:(void (^)(NSString * _Nonnull))success faliure:(void (^)(NSString * _Nonnull))faliure {
- [LOADING_MANAGER showCustomLoading:@"上传中..."];
- NSData *fileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:uploadFileUrl]];
- NSString *suffix = [NSString stringWithFormat:@".%@",[uploadFileUrl pathExtension]];
- [UPLOAD_MANAGER configWithfilePath:@"/user/"];
- [UPLOAD_MANAGER videoUpload:fileData fileName:@"video" fileSuffix:suffix progress:^(int64_t bytesWritten, int64_t totalBytes) {
- // 显示进度
- int progress = (int)(bytesWritten / totalBytes * 100);
- __block NSString *tipsMessage = [NSString stringWithFormat:@"上传中 %d%%",progress];
- dispatch_main_async_safe(^{
- [LOADING_MANAGER.loadingView setDisplayText:tipsMessage];
- });
- } successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
- [LOADING_MANAGER removeCustomLoading];
- NSString *fileUrl = [fileUrlArray lastObject];
- success(fileUrl);
-
- } faliure:^(NSError * _Nullable error, NSString * _Nullable descMessaeg) {
- [LOADING_MANAGER removeCustomLoading];
- faliure(descMessaeg);
- }];
- }
- #pragma mark ------- KSCloudWebViewControllerDelegate
- - (void)openWebViewWithParm:(NSDictionary *)parm inNavController:(UINavigationController *)navController {
- KSBaseWKWebViewController *detailCtrl = [[KSBaseWKWebViewController alloc] init];
- detailCtrl.url = [parm ks_stringValueForKey:@"url"];
- detailCtrl.parmDic = parm;
- NSInteger orientation = [parm ks_integerValueForKey:@"orientation"];
- BOOL isLandScape = orientation == 0 ? YES : NO;
- detailCtrl.ks_landScape = isLandScape;
- // if ([parm ks_boolValueForKey:@"showLoadingAnim"]) {
- // [detailCtrl showCoursewareLoading];
- // }
- [navController pushViewController:detailCtrl animated:YES];
- }
- - (void)showMergeViewWithParm:(NSDictionary *)parm displayInView:(UIView *)displayView callBackController:(KSCloudWebViewController *)controller {
- KSMediaMergeView *mergeView = [[KSMediaMergeView alloc] init];
- [displayView addSubview:mergeView];
- [mergeView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(displayView);
- }];
- mergeView.recordId = [parm ks_stringValueForKey:@"recordId"];
- mergeView.songName = [parm ks_stringValueForKey:@"title"];
- mergeView.coverImage = [parm ks_stringValueForKey:@"coverImg"];
- MJWeakSelf;
- NSInteger defaultDelay = [parm ks_integerValueForKey:@"defaultDelay"];
- NSURL *videoUrl = [parm valueForKey:@"videoUrl"];
- NSURL *bgAudioUrl = [parm valueForKey:@"bgAudioUrl"];
- NSURL *recordUrl = [parm valueForKey:@"recordUrl"];
- NSString *accompanyUrl = [parm ks_stringValueForKey:@"remoteBgUrl"];
-
- [mergeView configWithVideoUrl:videoUrl bgAudioUrl:bgAudioUrl remoteBgUrl:accompanyUrl recordUrl:recordUrl offsetTime:defaultDelay mergeCallback:^(BOOL isPublished) {
- if (isPublished) {
- [weakSelf callBackMergeSuccess:controller];
- }
- }];
- }
- - (void)callBackMergeSuccess:(KSCloudWebViewController *)controller {
-
- [controller musicPublishCallBack];
- }
- - (void)changeCurrentPageLandScape:(BOOL)landScape changeController:(KSCloudWebViewController *)controller {
- if (landScape) {
- // 切换到横屏
- if (IS_IPAD) {
- controller.zh_statusBarHidden = YES;
- }
- AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
- delegate.allowAutoRotate = YES;
- [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight inController:controller];
- }
- else {
- if (IS_IPAD) {
- controller.zh_statusBarHidden = NO;
- }
- AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
- delegate.allowAutoRotate = NO;
- [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait inController:controller];
- }
- }
- - (void)notiferShowAICustomLoadingDisplayInView:(UIView *)view callBackController:(KSCloudWebViewController *)controller {
- MJWeakSelf;
- [self.customLoading loadingCallback:^{
- [weakSelf notiferBack:controller];
- }];
- if ([view.subviews containsObject:self.customLoading]) {
- return;
- }
- [view addSubview:self.customLoading];
- [self.customLoading mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.bottom.mas_equalTo(view);
- }];
- [view bringSubviewToFront:self.customLoading];
- [self.customLoading showLoading];
- }
- - (void)notiferRemoveAICustomLoading {
- [self.customLoading stopLoading];
- _customLoading = nil;
- }
- #pragma mark ---- error View
- - (void)notiferShowErrorViewDisplayInView:(UIView *)view callBackController:(KSCloudWebViewController *)controller {
- MJWeakSelf;
- [_errorView failViewActionCallback:^(BOOL isBack) {
- [weakSelf hideErrorViewInView:view];
- if (isBack) {
- [weakSelf notiferBack:controller];
- }
- else {
- [weakSelf notiferRefresh:controller];
- }
- }];
- }
- - (void)notiferRemoveErrorInView:(UIView *)view {
- [self hideErrorViewInView:view];
- }
- - (void)hideErrorViewInView:(UIView *)view {
-
- if ([view.subviews containsObject:self.errorView]) {
- [self.errorView removeFromSuperview];
- }
- _errorView = nil;
- }
- - (void)notiferRefresh:(KSCloudWebViewController *)controller {
- [controller loadRequest];
- }
- #pragma mark ----- 小酷AI loading
- - (AccompanyLoadingView *)customLoading {
- if (!_customLoading) {
- _customLoading = [AccompanyLoadingView shareInstance];
- }
- return _customLoading;
- }
- - (void)notiferBack:(KSCloudWebViewController *)controller {
- if (_customLoading) {
- [self notiferRemoveAICustomLoading];
- }
- if (_errorView) {
- [_errorView removeFromSuperview];
- _errorView = nil;
- }
- [controller backAction];
- }
- #pragma mark --- web error
- - (KSWebLoadRefreshView *)errorView {
- if (!_errorView) {
- _errorView = [KSWebLoadRefreshView shareInstance];
- }
- return _errorView;
- }
- @end
|