Files
peko-ios/YuMi/Modules/YMWeb/MSRoomGameWebVC.m

439 lines
16 KiB
Mathematica
Raw Normal View History

2024-04-28 11:41:57 +08:00
//
// MSRoomGameWebVC.m
// YuMi
//
// Created by duoban on 2024/4/28.
//
#import "MSRoomGameWebVC.h"
2024-04-30 19:30:11 +08:00
#import "LittleGameInfoModel.h"
#import "RoomInfoModel.h"
#import "XPIAPRechargeViewController.h"
2024-04-28 11:41:57 +08:00
@interface MSWeakWebViewScriptMessageDelegate : NSObject<WKScriptMessageHandler>
//WKScriptMessageHandler JavaScriptOC
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
@implementation MSWeakWebViewScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
//WKScriptMessageHandler
//JSname
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([self.scriptDelegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) {
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
}
@end
NSString * const kMSGetConfig = @"getConfig";
NSString * const kMSDestroy = @"destroy";
NSString * const kMSGameRecharge = @"gameRecharge";
NSString * const kMSGameLoaded = @"gameLoaded";
@interface MSRoomGameWebVC () <WKNavigationDelegate, WKScriptMessageHandler,WKUIDelegate>
@property (strong, nonatomic) WKWebView *webView;
2024-04-28 11:41:57 +08:00
@property (strong, nonatomic) UIProgressView *progressView;
@property (nonatomic, strong) WKUserContentController *ms_userContentController;
2024-04-30 19:30:11 +08:00
@property (nonatomic,weak) id<RoomHostDelegate> hostDelegate;
@property(nonatomic,strong) ActivityInfoModel *gameModel;
2024-05-07 19:40:21 +08:00
@property(nonatomic,strong) UIButton *backBtn;
2024-10-21 18:29:30 +08:00
@property (nonatomic, assign) BOOL isBaiShun;
@property (nonatomic, assign) BOOL isCharing;
2024-04-28 11:41:57 +08:00
@end
@implementation MSRoomGameWebVC
2024-04-30 19:30:11 +08:00
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate gameModel:(ActivityInfoModel *)gameModel
{
self = [super init];
if (self) {
self.hostDelegate = delegate;
self.gameModel = gameModel;
2024-10-21 18:29:30 +08:00
self.isBaiShun = [gameModel.code.uppercaseString isEqualToString:@"BAISHUN"];
2024-04-30 19:30:11 +08:00
}
return self;
}
- (BOOL)isHiddenNavBar {
return YES;
}
2024-04-28 11:41:57 +08:00
- (void)viewDidLoad {
[super viewDidLoad];
[self installUI];
[self setupBackButton];
[self showLoading];
}
2024-10-21 18:29:30 +08:00
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!self.isBaiShun &&
self.isCharing) {
self.isCharing = NO;
[self updateCoin];
}
}
- (void)setupBackButton {
2024-05-07 19:40:21 +08:00
self.backBtn = [UIButton new];
[self.view addSubview:self.backBtn];
2024-10-21 18:29:30 +08:00
self.backBtn.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
2024-05-07 19:40:21 +08:00
[self.backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
2024-04-28 11:41:57 +08:00
}
2024-04-28 11:41:57 +08:00
-(void)installUI{
2024-05-07 19:40:21 +08:00
self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
2024-04-28 11:41:57 +08:00
MSWeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[MSWeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
_ms_userContentController = [[WKUserContentController alloc] init];
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kMSGetConfig];
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kMSDestroy];
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kMSGameRecharge];
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kMSGameLoaded];
2024-10-21 18:29:30 +08:00
/// LEADERCC
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:@"closeGame"];
[_ms_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:@"pay"];
2024-04-28 11:41:57 +08:00
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
config.allowsInlineMediaPlayback = YES;
[config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
//⼿,
2024-10-21 18:29:30 +08:00
config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
2024-04-28 11:41:57 +08:00
config.preferences = [[WKPreferences alloc]init];
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
config.preferences = preferences;
config.preferences.javaScriptEnabled = YES;
config.userContentController = _ms_userContentController;
2024-04-30 19:30:11 +08:00
2024-10-21 18:29:30 +08:00
CGRect frame = CGRectZero;
if (self.isBaiShun) {
frame = CGRectMake(0,0,
KScreenWidth, KScreenHeight);
} else {
if(self.gameModel.showType == ActivityShowType_Half){
frame = CGRectMake(0, KScreenHeight * 0.3,
KScreenWidth, KScreenHeight * 0.7);
} else {
frame = CGRectMake(0,0,
KScreenWidth, KScreenHeight);
}
}
self.webView = [[WKWebView alloc] initWithFrame:frame
configuration:config];
[self.webView.scrollView setBackgroundColor:[UIColor clearColor]];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setUIDelegate:self];
self.webView.navigationDelegate = self;
2024-04-28 11:41:57 +08:00
//
[self.webView setOpaque:NO];
2024-04-28 11:41:57 +08:00
//
if (@available(iOS 11.0, *)) {
2024-10-21 18:29:30 +08:00
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
2024-04-28 11:41:57 +08:00
}
[self.view addSubview:self.webView];
2024-04-30 19:30:11 +08:00
NSString *h5Url = self.gameModel.skipContent;
2024-10-21 18:29:30 +08:00
NSURL *url = [self handleGameURL:h5Url];
2024-04-30 19:30:11 +08:00
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
2024-10-21 18:29:30 +08:00
- (NSURL *)handleGameURL:(NSString *)url {
if (self.isBaiShun) {
return [NSURL URLWithString:url];
} else {
url = [url stringByAppendingFormat:@"&uid=%@", [AccountInfoStorage instance].getUid];
url = [url stringByAppendingFormat:@"&token=%@", self.gameModel.gameModel.code];
url = [url stringByAppendingFormat:@"&lang=%@", @"zh-CN"];
url = [url stringByAppendingFormat:@"&roomid=%ld", (long)self.hostDelegate.getRoomInfo.uid];
return [NSURL URLWithString:url];
}
return nil;
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self hideHUD];
2024-10-21 18:29:30 +08:00
if (self.isBaiShun) {
} else {
self.backBtn.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight*0.3);
}
#if DEBUG
NSString *fileName = @"vconsole.min.js"; //
NSString *directory = [[NSBundle mainBundle] resourcePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:directory];
NSString *filePath;
for (NSString *path in enumerator) {
if ([[path lastPathComponent] containsString:fileName]) {
filePath = [directory stringByAppendingPathComponent:path];
break;
}
}
if (filePath.length > 0) {
NSString *vConsoleScript = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:nil];
[self.webView evaluateJavaScript:vConsoleScript completionHandler:nil];
// vConsole
NSString *initVConsoleScript = @"var vConsole = new VConsole();";
[self.webView evaluateJavaScript:initVConsoleScript completionHandler:nil];
}
#endif
2024-04-28 11:41:57 +08:00
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
2024-04-28 11:41:57 +08:00
NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,card);
});
}
}
// JS
- (void)callJs:(NSString*)method withJavaScriptValue:(nullable id)arguments
{
@kWeakify(self);
2024-04-28 11:41:57 +08:00
if (arguments) {
NSData *data = [NSJSONSerialization dataWithJSONObject:arguments
options:(NSJSONWritingPrettyPrinted) error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSString *jsMethods = [NSString stringWithFormat:@"%@(%@)", method,
jsonStr];
[self.webView evaluateJavaScript:jsMethods completionHandler:^(id
2024-04-28 11:41:57 +08:00
_Nullable resp, NSError * _Nullable error) {
@kStrongify(self);
2024-04-28 11:41:57 +08:00
NSLog(@"error = %@ , response = %@",error, resp);
[self.backBtn removeFromSuperview];
2024-04-28 11:41:57 +08:00
}];
} else {
NSString *jsMethods = [NSString stringWithFormat:@"%@({})", method];
[self.webView evaluateJavaScript:jsMethods completionHandler:^(id
2024-04-28 11:41:57 +08:00
_Nullable resp, NSError * _Nullable error) {
@kStrongify(self);
2024-04-28 11:41:57 +08:00
NSLog(@"error = %@ , response = %@",error, resp);
[self.backBtn removeFromSuperview];
2024-04-28 11:41:57 +08:00
}];
}
}
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
if (jsonString == nil && jsonString.length == 0) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err)
{
NSLog(@"json解析失败%@",err);
return nil;
}
return dic;
}
//
2024-10-21 18:29:30 +08:00
- (void)userContentController:(WKUserContentController *)userContentController
2024-04-28 11:41:57 +08:00
didReceiveScriptMessage:(WKScriptMessage *)message
{
2024-10-21 18:29:30 +08:00
/// BaiShun
if (self.isBaiShun) {
NSDictionary *dicBody = [self dictionaryWithJsonString:message.body];
if ([message.name isEqualToString:kMSGetConfig]) {
[self getConfig:dicBody];
} else if ([message.name isEqualToString:kMSDestroy]) {
[self destroy:dicBody];
} else if ([message.name isEqualToString:kMSGameLoaded]) {
[self gameLoaded:dicBody];
} else if ([message.name isEqualToString:kMSGameRecharge]) {
[self gameRecharge:dicBody];
} else {
NSLog(@"未实现⽅法 : %@ --> %@", message.name, message.body);
}
} else {
2024-10-21 18:29:30 +08:00
/// LeaderCC
NSString* method = [NSString stringWithFormat:@"%@:", message.name];
SEL selector = NSSelectorFromString(method);
if([self respondsToSelector:selector]){
[self performSelector:selector withObject:message.body];
}else{
NSLog(@"未實現方法 : %@ --> %@", message.name, message.body);
}
2024-04-28 11:41:57 +08:00
}
}
2024-10-21 18:29:30 +08:00
#pragma mark - LeaderCC Delegate
//
- (void)closeGame:(NSDictionary*)args {
// NSLog(@"1234");
TTAlertConfig *config = [[TTAlertConfig alloc]init];
config.message = YMLocalizedString(@"MSRoomGameWebVC0");
config.actionStyle = TTAlertActionBothStyle;
[TTPopup alertWithConfig:config showBorder:NO confirmHandler:^{
[self willMoveToParentViewController:nil]; //1
[self.view removeFromSuperview]; //2
[self removeFromParentViewController]; //3
} cancelHandler:^{
}];
}
//
- (void)pay:(NSDictionary*)args {
// TODO
TTAlertConfig *config = [[TTAlertConfig alloc]init];
config.message = YMLocalizedString(@"XPNobleCenterViewController3");
config.actionStyle = TTAlertActionBothStyle;
@kWeakify(self);
[TTPopup alertWithConfig:config showBorder:NO confirmHandler:^{
@kStrongify(self);
XPIAPRechargeViewController * webVC =[[XPIAPRechargeViewController alloc] init];
webVC.type = @"4";
[self.navigationController pushViewController:webVC animated:YES];
self.isCharing = YES;
} cancelHandler:^{
}];
}
- (void)updateCoin {
//
[self.webView evaluateJavaScript:@"updateCoin()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (error) {
NSLog(@"Error calling JS function: %@", error.localizedDescription);
} else {
NSLog(@"JS function called successfully, result: %@", result);
}
}];
}
#pragma mark - BaiShun Delegate
2024-04-28 11:41:57 +08:00
//
- (void) getConfig:(NSDictionary*)args
{
NSLog(@"BSGAME %s","游戏调⽤getConfig");
NSString* method = [args objectForKey:@"jsCallback"];
2024-04-30 19:30:11 +08:00
RoomInfoModel *roomInfo = self.hostDelegate.getRoomInfo;
ActivityInfoItemModel *gameModel = self.gameModel.gameModel;
NSString *appChannel = gameModel.appChannel ?: @"";
int64_t appId = gameModel.appId;
NSString *userId = [AccountInfoStorage instance].getUid;
NSString *code = gameModel.code ?: @"";
NSString *roomId = [NSString stringWithFormat:@"%ld",roomInfo.uid];
NSString *gameMode = gameModel.gameMode ?: @"";
NSDictionary *gameConfig = gameModel.gameConfig ?: @{};
int gsp = gameModel.gsp;
2024-04-28 11:41:57 +08:00
// APP
NSObject* configData = @{
2024-04-30 19:30:11 +08:00
@"appChannel":appChannel,
@"appId":@(appId),
@"userId":userId,
@"code":code,
@"roomId":roomId,
@"gameMode":gameMode,
@"language":[self getlanguage],
@"gameConfig":gameConfig,
@"gsp":@(gsp),
2024-04-28 11:41:57 +08:00
};
[self callJs:method withJavaScriptValue:configData];
}
2024-04-30 19:30:11 +08:00
-(NSString *)getlanguage{
NSString *language = [NSBundle getLanguageText];
if ([language hasPrefix:@"zh"]) {
if ([language rangeOfString:@"Hans"].location != NSNotFound) {
language = @"0"; //
} else {
language = @"1"; //
}
}else if([language hasPrefix:@"ar"]){///
language = @"7";
}else{///
language = @"2";
}
return language;
}
2024-04-28 11:41:57 +08:00
//
- (void) destroy:(NSDictionary*)args
{
NSLog(@"BSGAME %s","游戏调⽤destroy");
// TODO
2024-04-30 19:30:11 +08:00
[self willMoveToParentViewController:nil]; //1
[self.view removeFromSuperview]; //2
[self removeFromParentViewController]; //3
2024-04-28 11:41:57 +08:00
}
2024-05-07 19:40:21 +08:00
-(void)backBtnAction{
TTAlertConfig *config = [[TTAlertConfig alloc]init];
config.message = YMLocalizedString(@"MSRoomGameWebVC0");
config.actionStyle = TTAlertActionBothStyle;
[TTPopup alertWithConfig:config showBorder:NO confirmHandler:^{
[self willMoveToParentViewController:nil]; //1
[self.view removeFromSuperview]; //2
[self removeFromParentViewController]; //3
} cancelHandler:^{
}];
}
2024-04-28 11:41:57 +08:00
//
- (void) gameRecharge:(NSDictionary*)args
{
NSLog(@"BSGAME %s","游戏调⽤gameRecharge");
// TODO
2024-04-30 19:30:11 +08:00
TTAlertConfig *config = [[TTAlertConfig alloc]init];
config.message = YMLocalizedString(@"XPNobleCenterViewController3");
config.actionStyle = TTAlertActionBothStyle;
2024-10-21 18:29:30 +08:00
@kWeakify(self);
2024-04-30 19:30:11 +08:00
[TTPopup alertWithConfig:config showBorder:NO confirmHandler:^{
2024-10-21 18:29:30 +08:00
@kStrongify(self);
2024-04-30 19:30:11 +08:00
XPIAPRechargeViewController * webVC =[[XPIAPRechargeViewController alloc] init];
webVC.type = @"4";
[self.navigationController pushViewController:webVC animated:YES];
2024-10-21 18:29:30 +08:00
self.isCharing = YES;
2024-04-30 19:30:11 +08:00
} cancelHandler:^{
}];
2024-04-28 11:41:57 +08:00
}
//
- (void) gameLoaded:(NSDictionary*)args
{
// TODO
2024-05-07 19:40:21 +08:00
[self.backBtn removeFromSuperview];
2024-04-28 11:41:57 +08:00
}
@end