MOWebViewController.m 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. //
  2. // MOWebViewController.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/12/25.
  6. //
  7. // 用于标记webView序号
  8. static int webViewTag = 0;
  9. #import "MOWebViewController.h"
  10. #import "POWeakWKWebViewScriptMessageHandler.h"
  11. #import "MOTopUpVC.h"//充值界面
  12. #import "MOUserHomePageVC.h" // 个人主页
  13. #import "MOUploadDeviceTool.h"
  14. @interface MOWebViewController ()<WKNavigationDelegate,WKUIDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>
  15. /** 标记WebView序号 */
  16. @property (nonatomic, assign) int webViewTag;
  17. /**记录是否添加了观察者,避免dealloc时释放观察者崩溃*/
  18. @property (nonatomic, assign) BOOL hasAddedObserver;
  19. /** 标记是否需要更新钻石 */
  20. @property (nonatomic, assign) BOOL needUpdateDiamond;
  21. /** 跳转tag, 防止网页频繁点击跳转 */
  22. @property (nonatomic, assign) BOOL pushTag;
  23. @property (strong, nonatomic) UIProgressView *progressView;
  24. @property (nonatomic, strong) BigBtn *closeBtn;
  25. @end
  26. @implementation MOWebViewController
  27. - (void)viewWillAppear:(BOOL)animated{
  28. [super viewWillAppear:animated];
  29. if(self.titleStr.length != 0 || self.isShowTitle){
  30. [self.navigationController setNavigationBarHidden:NO animated:animated];
  31. // [self mo_setNavLeftItemWithImage:[UIImage imageNamed:@"icon_new_left_gray"] andBackgroundImg:[UIImage imageNamed:@"icon_mine_base_bg"]];
  32. [self mo_v2_setNavLeftItemWithImage:[UIImage imageNamed:@"v_2_icon_new_back_black"] andBackgroundImg:nil AndBgColor:nil];
  33. }
  34. else{
  35. [self.navigationController setNavigationBarHidden:YES animated:animated];
  36. }
  37. if(self.isWame || self.isMiniWame){
  38. [[MOChatNotificationManager sharedManager] enterScene:MOChatNotificationSceneWame];//进入聊天列表场景
  39. }
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view.
  44. [self.poWebView loadRequest:self.request];
  45. if(self.isHalfShow){
  46. self.view.backgroundColor = [UIColor clearColor];
  47. [self.poWebView setBackgroundColor:[UIColor clearColor]];
  48. CGFloat topSpacing = SCREENHEIGHT / 2.0 - 100.0;
  49. [self.poWebView mas_remakeConstraints:^(MASConstraintMaker *make) {
  50. make.left.right.bottom.equalTo(self.view);
  51. make.top.equalTo(self.view).offset(topSpacing);
  52. }];
  53. }
  54. if(self.isWame){
  55. self.isNeedUpdateMoney = YES;
  56. self.view.backgroundColor = [UIColor blackColor];
  57. [self.poWebView setBackgroundColor:[UIColor blackColor]];
  58. }
  59. //半屏适配
  60. if(self.isMiniWame){
  61. self.isNeedUpdateMoney = YES;
  62. CGFloat width = SCREENWIDTH;
  63. CGFloat height = width;
  64. NSString *urlStr = self.webUrl.absoluteString;
  65. if(self.minHeight > 0){
  66. float ratio = (float)750/self.minHeight;
  67. height = SCREENWIDTH / ratio;
  68. }
  69. self.view.backgroundColor = [UIColor clearColor];
  70. [self.poWebView setBackgroundColor:[UIColor whiteColor]];
  71. [self.poWebView mas_remakeConstraints:^(MASConstraintMaker *make) {
  72. make.left.right.equalTo(self.view);
  73. make.bottom.equalTo(self.view).offset(-12.0);
  74. make.height.equalTo(@(height));
  75. }];
  76. }
  77. if(self.isRedRain){
  78. self.view.backgroundColor = [UIColor clearColor];
  79. [self.poWebView setBackgroundColor:[UIColor clearColor]];
  80. CGFloat topSpacing = STATUS_BAR_HEIGHT + 70.0;
  81. [self.view addSubview:self.closeBtn];
  82. [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.equalTo(self.view).offset(topSpacing);
  84. make.height.width.equalTo(@30.0);
  85. make.right.equalTo(self.view).offset(-15.0);
  86. }];
  87. self.closeBtn.layer.cornerRadius = 30.0 / 2.0;
  88. self.closeBtn.layer.masksToBounds = YES;
  89. }
  90. if(self.titleStr.length != 0 ){
  91. self.navigationItem.title = self.titleStr;
  92. [self setNavLeftItemWithImage:[UIImage imageNamed:@"icon_nav_back"]];
  93. }
  94. if(self.isShowTitle){
  95. [self setNavLeftItemWithImage:[UIImage imageNamed:@"icon_nav_back"]];
  96. }
  97. // 添加 KVO 监听器,监听 title 属性的变化
  98. // [self.poWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
  99. // // 创建并添加 UIProgressView
  100. // self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  101. // self.progressView.frame = CGRectMake(0, 300.0, self.view.frame.size.width, 2);
  102. // self.progressView.backgroundColor = [UIColor redColor];
  103. // [self.view addSubview:self.progressView];
  104. // // 添加观察者,监听加载进度
  105. // [self.poWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
  106. self.needUpdateDiamond = NO;
  107. self.poWebView.allowsBackForwardNavigationGestures = YES;
  108. // Set the navigation controller delegate
  109. self.navigationController.delegate = self;
  110. if (self.blindBoxPaddingTop) {
  111. self.view.backgroundColor = [UIColor clearColor];
  112. self.poWebView.opaque = NO;
  113. self.poWebView.backgroundColor = [UIColor clearColor];
  114. self.poWebView.scrollView.backgroundColor = [UIColor clearColor];
  115. }
  116. }
  117. #pragma mark - UINavigationControllerDelegate
  118. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  119. if (viewController == self && !self.poWebView.canGoBack) {
  120. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  121. } else {
  122. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  123. }
  124. }
  125. - (void)LeftBarItemClick
  126. {
  127. if(self.isNeedUpdateMoney){
  128. self.needUpdateDiamond = YES;
  129. }
  130. if (self.isWame) {
  131. SendNotification(@"kNotification_CloseWame")
  132. }
  133. // 左按钮点击触发的事件,实际事件请在在子类重写,可能不是回到上一级视图
  134. NSArray *viewControllers = self.navigationController.viewControllers;
  135. if (viewControllers.count > 1)
  136. {
  137. [self.navigationController popViewControllerAnimated:YES];
  138. }
  139. else if (viewControllers.count==1)
  140. {
  141. [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
  142. }
  143. self.closeViewBlock ? self.closeViewBlock() : nil;
  144. }
  145. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  146. if(self.isNeedUpdateMoney){
  147. self.needUpdateDiamond = YES;
  148. }
  149. if(self.isHalfShow || self.isMiniWame){
  150. if(self.isMiniWame){
  151. WEAKSELF
  152. MOTitleNormalAlertView *alertView = [[MOTitleNormalAlertView alloc] init];
  153. alertView.titleLabel.text = NSLocalString(@"mimo_alert_normal_tip");
  154. alertView.subTitleLabel.text = NSLocalString(@"mimo_fan_club_quit_alert_title");
  155. [alertView.cancelBtn setTitle:NSLocalString(@"mimo_Cancel") forState:UIControlStateNormal];
  156. [alertView.confirmBtn setTitle:NSLocalString(@"mimo_TipConfirm") forState:UIControlStateNormal];
  157. alertView.confirmBlock = ^{
  158. //跳转充值界面 (全屏)
  159. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  160. };
  161. [alertView show];
  162. }
  163. else{
  164. [self dismissViewControllerAnimated:YES completion:nil];
  165. }
  166. }
  167. else{
  168. [self.navigationController popViewControllerAnimated:YES];
  169. }
  170. self.closeViewBlock ? self.closeViewBlock() : nil;
  171. }
  172. - (void)loadView{
  173. [super loadView];
  174. [self.view addSubview:self.poWebView];
  175. if(self.isHaveSafeTop){
  176. [self.poWebView mas_makeConstraints:^(MASConstraintMaker *make){
  177. make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
  178. make.left.right.bottom.equalTo(self.view);
  179. }];
  180. }
  181. else{
  182. [self.poWebView mas_makeConstraints:^(MASConstraintMaker *make){
  183. make.edges.mas_equalTo(0);
  184. }];
  185. }
  186. if (@available(iOS 11.0,*)) {
  187. [self.poWebView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
  188. }
  189. else{
  190. self.edgesForExtendedLayout = UIRectEdgeNone;
  191. }
  192. // [self.poWebView loadRequest:self.request];
  193. }
  194. - (WKWebView *)poWebView
  195. {
  196. if (!_poWebView)
  197. {
  198. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  199. config.selectionGranularity = WKSelectionGranularityDynamic;
  200. config.allowsInlineMediaPlayback = YES;
  201. config.mediaTypesRequiringUserActionForPlayback = NO;
  202. WKPreferences *preferences = [WKPreferences new];
  203. //是否支持JavaScript
  204. preferences.javaScriptEnabled = YES;
  205. //不通过用户交互,是否可以打开窗口
  206. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  207. config.preferences = preferences;
  208. [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
  209. [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
  210. //新建一个弱引用代理,防止循环引用
  211. POWeakWKWebViewScriptMessageHandler *weakHandler = [[POWeakWKWebViewScriptMessageHandler alloc] initWithSriptMessageHandler:self];
  212. config.userContentController = [self userControllerWithMessageHandler:weakHandler];
  213. _poWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) configuration:config];
  214. [_poWebView setBackgroundColor:[UIColor colorWithRed:242/255. green:242/255. blue:246/255. alpha:1.0]];
  215. [_poWebView setOpaque:NO];
  216. WEAKSELF;
  217. [_poWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error)
  218. {
  219. NSString *oldUA = result;
  220. NSString *newUA = [NSString stringWithFormat:@"%@/xiaohui",oldUA];
  221. weakSelf.poWebView.customUserAgent = newUA;
  222. }];
  223. _poWebView.navigationDelegate = self;
  224. _poWebView.UIDelegate = self;
  225. [_poWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
  226. self.webViewTag = webViewTag;
  227. webViewTag++;
  228. self.hasAddedObserver = YES;
  229. _poWebView.scrollView.bounces = NO;//禁用弹性效果
  230. //FIXME: 默认缓存策略是NSURLRequestUseProtocolCachePolicy
  231. #if kAPP_Environment == 1
  232. NSURLRequestCachePolicy policy = NSURLRequestUseProtocolCachePolicy;
  233. #elif kAPP_Environment == 2
  234. NSURLRequestCachePolicy policy = NSURLRequestUseProtocolCachePolicy;
  235. // NSURLRequestCachePolicy policy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
  236. if (@available(iOS 16.4,*)) {
  237. [self.poWebView setInspectable:YES];
  238. }
  239. #endif
  240. self.request = [NSURLRequest requestWithURL:self.webUrl cachePolicy:policy timeoutInterval:40];
  241. MOLogV(@"[WebView] init poWebView(%d) url:%@",self.webViewTag,self.webUrl);
  242. }
  243. return _poWebView;
  244. }
  245. - (WKUserContentController *)userControllerWithMessageHandler:(POWeakWKWebViewScriptMessageHandler *)weakHandler{
  246. WKUserContentController *userController = [[WKUserContentController alloc] init];
  247. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_Close];
  248. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_GetUserInfo];
  249. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_GetStatusHeight];
  250. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_GetLanguage];
  251. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_GetDiamond];
  252. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_ToGetRain];
  253. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_ToSubmitShareLink];
  254. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_TokenExpired];
  255. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_ExportUrl];
  256. //
  257. [userController addScriptMessageHandler:weakHandler name:@"recharge"];
  258. [userController addScriptMessageHandler:weakHandler name:@"clickRecharge"];
  259. [userController addScriptMessageHandler:weakHandler name:@"newTppClose"];
  260. // 2
  261. [userController addScriptMessageHandler:weakHandler name:@"getConfig"];
  262. [userController addScriptMessageHandler:weakHandler name:@"destroy"];
  263. [userController addScriptMessageHandler:weakHandler name:[MOTextAESTools toGetHameRecharge]];
  264. [userController addScriptMessageHandler:weakHandler name:[MOTextAESTools toGetHameLoaded]];
  265. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_ShareToPlatAgency];
  266. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_BindAgencySuccess];
  267. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_ChagneMysteriousStatus];
  268. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_GetBlindBoxScreenParams];
  269. [userController addScriptMessageHandler:weakHandler name:kWebCallBack_SetBlindBoxResult];
  270. return userController;
  271. }
  272. - (void)setWameKeyboardHeight:(CGFloat)wameKeyboardHeight{
  273. _wameKeyboardHeight = wameKeyboardHeight;
  274. if(wameKeyboardHeight > 100.0){
  275. [self.poWebView mas_updateConstraints:^(MASConstraintMaker *make) {
  276. make.bottom.equalTo(self.view).offset(-(wameKeyboardHeight - 100.0));
  277. }];
  278. }
  279. else{
  280. [self.poWebView mas_updateConstraints:^(MASConstraintMaker *make) {
  281. make.bottom.equalTo(self.view).offset(-12.0);
  282. }];
  283. }
  284. }
  285. #pragma mark ========= JS-->OC ===============
  286. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  287. //#define kWebCallBack_GetUserInfo @"getUserInfo" //传用户信息
  288. if ([message.name isEqualToString:kWebCallBack_GetUserInfo])
  289. {
  290. MOMeDataInfo *userInfoData = (MOMeDataInfo *)[[MODataCache sharedYYCache] objectForKey:kMineUserInfo];
  291. if(userInfoData){
  292. // ADJAttribution *adjInfo = [Adjust attribution];
  293. userInfoData.token = GetToken;
  294. userInfoData.adid = [MOUploadDeviceTool shareTool].adid;
  295. NSString *versionString = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  296. userInfoData.appVersion = versionString;
  297. NSDictionary *userDict = [userInfoData dictionaryRepresentation];
  298. NSString *jsonString = [userDict modelToJSONString];
  299. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",@"getUserInfoResponse",jsonString];
  300. [self.poWebView evaluateJavaScript:jsString completionHandler:nil];
  301. }
  302. else{
  303. NSString *jsonString = @"{\"follows\":3,\"vip\":{\"giveGiftBag\":false,\"type\":0,\"expireTime\":0,\"giveGaily\":false},\"profile\":{\"id\":\"65d4ba9f097a721b1ef0a8c5\",\"intro\":\"\",\"medals\":[],\"userNo\":\"43999352\",\"bks\":[],\"agoraId\":43999352,\"adornment\":{\"headdressAnno\":false,\"bubbleCode\":0,\"enterBar\":0},\"avatar\":\"https:\\/\\/mimo-live.s3.ap-southeast-3.amazonaws.com\\/av\\/2024\\/52\\/b78\\/fa5f8c804caae17d1635afa724149118.jpg\",\"birthday\":\"1990-01-01\",\"nickname\":\"SuperY\",\"country\":\"ID\",\"gender\":1},\"mobile\":{},\"family\":{\"memberMax\":0,\"totalPower\":0,\"level\":0,\"memberNum\":0,\"weekPower\":0,\"dailyPower\":0},\"wallet\":{\"diamond\":22,\"consumeDiamond\":0,\"goldenBean\":0},\"fansClub\":{\"show\":false,\"level\":0,\"rank\":0,\"members\":0},\"currExp\":49,\"guild\":{\"anchor\":0},\"level\":3,\"friends\":1,\"fans\":1,\"nextExp\":113,\"liveBadge\":{\"type\":0},\"status\":{\"anchor\":true,\"guild\":true,\"mobile\":false,\"fansClub\":false,\"liveDiscount\":true,\"anchorType\":1,\"joinFansClub\":true}}";
  304. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",@"getUserInfoResponse",jsonString];
  305. [self.poWebView evaluateJavaScript:jsString completionHandler:nil];
  306. }
  307. }
  308. //#define kWebCallBack_ToGetRain @"getRedRainInfo" //获取红包雨
  309. else if ([message.name isEqualToString:kWebCallBack_ToGetRain]){
  310. NSDictionary *redRainDict = self.redRainData;
  311. NSString *jsonString = [redRainDict modelToJSONString];
  312. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",@"getRedRainInfoResponse",jsonString];
  313. [self.poWebView evaluateJavaScript:jsString completionHandler:nil];
  314. }
  315. //#define kWebCallBack_ToSubmitShareLink @"submitShareLink" //分享
  316. else if ([message.name isEqualToString:kWebCallBack_ToSubmitShareLink]){
  317. // 打印出传递的消息内容
  318. MOLogV(@"Received message: %@", message.body);
  319. [self shareTheLinkWith:message.body];
  320. }
  321. //#define kWebCallBack_TokenExpired @"tokenExpired" //token过期
  322. else if ([message.name isEqualToString:kWebCallBack_TokenExpired]){
  323. [self tokenExpiredAndGetTheUserInfo];
  324. return;
  325. }
  326. //#define kWebCallBack_ExportUrl @"exportUrl" //导出链接
  327. else if ([message.name isEqualToString:kWebCallBack_ExportUrl]){
  328. [self toExportUrlWith:message.body];
  329. return;
  330. }
  331. //#define kWebCallBack_Close @"closeActivity" //关闭界面
  332. else if ([message.name isEqualToString:kWebCallBack_Close]){
  333. [self LeftBarItemClick];
  334. if(self.redRainData){
  335. self.needUpdateDiamond = YES;
  336. }
  337. }
  338. //#define kWebCallBack_GetDiamond @"needUpdateDiamond" //获取钻石
  339. else if ([message.name isEqualToString:kWebCallBack_GetDiamond]){
  340. //标记需要更新钻石
  341. self.needUpdateDiamond = YES;
  342. }
  343. //#define kWebCallBack_GetStatusHeight @"getStatusHeight" //状态栏高度
  344. else if ([message.name isEqualToString:kWebCallBack_GetStatusHeight]){
  345. //顶部高度
  346. CGFloat statusHeght = STATUS_BAR_HEIGHT + 44;
  347. //底部高度
  348. CGFloat navigationBarHeight = HOME_KEY_HEIGHT;
  349. NSDictionary *userDict = @{@"statusHeight":@(statusHeght),@"navigationBarHeight":@(navigationBarHeight)};
  350. NSString *jsonString = [userDict modelToJSONString];
  351. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",@"getStatusHeightResponse",jsonString];
  352. [self.poWebView evaluateJavaScript:jsString completionHandler:nil];
  353. }
  354. //#define kWebCallBack_GetLanguage @"getLanguage" //语言
  355. else if ([message.name isEqualToString:kWebCallBack_GetLanguage]){
  356. NSString *currentLanguage = [NSBundle currentLanguage];
  357. if(currentLanguage.length == 0){
  358. currentLanguage = @"en-us";
  359. }
  360. NSDictionary *userDict = @{@"language":currentLanguage};
  361. NSString *jsonString = [userDict modelToJSONString];
  362. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",@"getLanguageResponse",jsonString];
  363. [self.poWebView evaluateJavaScript:jsString completionHandler:nil];
  364. }
  365. //MARK:
  366. else if ([message.name isEqualToString:@"recharge"]){
  367. if(self.enterBaseData.type == 3){
  368. //so88
  369. [self toPushTopUpVC];
  370. }
  371. else{
  372. //百顺
  373. [self noMoneyTipShow];
  374. }
  375. }
  376. else if ([message.name isEqualToString:@"clickRecharge"]){
  377. [self toPushTopUpVC];
  378. }
  379. else if ([message.name isEqualToString:@"newTppClose"]){
  380. [self LeftBarItemClick];
  381. self.needUpdateDiamond = YES;
  382. }
  383. //MARK:
  384. else if ([message.name isEqualToString:@"getConfig"]){
  385. if(self.enterBaseData.type == 3){
  386. [self so88GetConfig:message.body];
  387. }
  388. else{
  389. NSDictionary *dicBody = [self dictionaryWithJsonString:message.body];
  390. [self getConfig:dicBody];
  391. }
  392. }
  393. else if ([message.name isEqualToString:@"destroy"]){
  394. [self LeftBarItemClick];
  395. self.needUpdateDiamond = YES;
  396. }
  397. else if ([message.name isEqualToString:[MOTextAESTools toGetHameRecharge]]){
  398. [self toPushTopUpVC];
  399. }
  400. else if ([message.name isEqualToString:[MOTextAESTools toGetHameLoaded]]){
  401. //加载完毕
  402. MOLogV(@"Loaded finish");
  403. }
  404. //全民代理分享
  405. else if ([message.name isEqualToString:kWebCallBack_ShareToPlatAgency]){
  406. // 打印出传递的消息内容
  407. MOLogV(@"Received message: %@", message.body);
  408. [self shareTheLinkWith:message.body];
  409. } else if ([message.name isEqualToString:kWebCallBack_BindAgencySuccess]) {
  410. SendNotification(@"kNotification_H5BindAgencySuccess")
  411. }
  412. else if ([message.name isEqualToString:kWebCallBack_ChagneMysteriousStatus]) {
  413. [self updateMysteriousSwitchStatus:message.body];
  414. }
  415. else if ([message.name isEqualToString:kWebCallBack_GetBlindBoxScreenParams]) {
  416. [self setBlindBoxScreenParams];
  417. }
  418. else if ([message.name isEqualToString:kWebCallBack_SetBlindBoxResult]) {
  419. [self handleBlindBoxGiftResult:message.body];
  420. }
  421. }
  422. - (void)handleBlindBoxGiftResult:(NSString *)jsonString {
  423. NSDictionary *dict = [self dictionaryWithJsonString:jsonString];
  424. MORtmBlindboxDrawResult *result = [MORtmBlindboxDrawResult modelWithJSON:dict];
  425. SendObjNotification(@"kNotification_DrawBlindBoxResut", result)
  426. }
  427. - (void)setBlindBoxScreenParams {
  428. if (!self.blindBoxPaddingTop) {
  429. return;
  430. }
  431. // 屏幕尺寸(pt -> px,乘以 scale)
  432. CGFloat scale = [UIScreen mainScreen].scale;
  433. if (kBottomSafeAreaInset == 0) {
  434. scale = 2;
  435. }
  436. int width = (int)([UIScreen mainScreen].bounds.size.width * scale);
  437. int height = (int)([UIScreen mainScreen].bounds.size.height * scale);
  438. int paddingTop = (int)(self.blindBoxPaddingTop.intValue * scale);
  439. // 获取当前 roomId
  440. NSString *roomId = self.roomId ?: @"";
  441. NSString *js = [NSString stringWithFormat:@"setBlindBoxScreenParams(%d,%d,%d,'%@')", width, height, paddingTop, roomId];
  442. [self.poWebView evaluateJavaScript:js completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
  443. if (error) {
  444. MOLogV(@"调用 H5 setBlindBoxScreenParams 失败: %@", error);
  445. } else {
  446. MOLogV(@"已通知 H5 调整页面: %@", resp);
  447. }
  448. }];
  449. }
  450. // 刷新神秘人开关本地缓存
  451. - (void)updateMysteriousSwitchStatus:(NSString *)result {
  452. NSDictionary *dict = [self dictionaryWithJsonString:result];
  453. NSString *dataResult = [MODataManager objectOrNilForKey:@"data" fromDictionary:dict];
  454. BOOL enable = [dataResult boolValue];
  455. MOMeDataInfo *userInfoData = (MOMeDataInfo *)[[MODataCache sharedYYCache] objectForKey:kMineUserInfo];
  456. userInfoData.userProfile.mysterious = enable;
  457. [[MODataCache sharedYYCache] setObject:userInfoData forKey:kMineUserInfo];
  458. [[NSUserDefaults standardUserDefaults] setBool:enable forKey:kIsMysterious];
  459. [[NSUserDefaults standardUserDefaults] synchronize];
  460. }
  461. - (void)shareTheLinkWith:(NSString *)jsonString{
  462. // 将 JSON 字符串转化为 NSData
  463. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  464. // 使用 NSJSONSerialization 将 NSData 转为 NSDictionary
  465. NSError *error = nil;
  466. NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
  467. if (error) {
  468. MOLogV(@"JSON 解析失败: %@", error.localizedDescription);
  469. }
  470. NSString *methodStr = jsonDict[@"method"];
  471. if([methodStr isEqualToString:kWebCallBack_ToSubmitShareLink]){
  472. NSDictionary *dataDict = jsonDict[@"data"];
  473. NSString *key = dataDict[@"typeStr"];
  474. [self shareURLWith:key];
  475. } else if ([methodStr isEqualToString:kWebCallBack_ShareToPlatAgency]) {
  476. NSDictionary *dataDict = jsonDict[@"data"];
  477. [self shareAgency:dataDict];
  478. }
  479. }
  480. //platName
  481. //1. Facebook
  482. //2. Instagram
  483. //3. Twitter or X
  484. //4. Line
  485. //5. WhatsApp
  486. //6. copy link
  487. - (void)shareAgency:(NSDictionary *)dataDict {
  488. WEAKSELF
  489. int platName = [dataDict[@"platName"] intValue];
  490. NSString *titleStr = dataDict[@"title"];
  491. NSString *contentStr = dataDict[@"content"];
  492. NSString *urlString = dataDict[@"url"];
  493. NSString *imageUrl = dataDict[@"imageUrl"];
  494. #if !TARGET_IPHONE_SIMULATOR
  495. SSDKPlatformType playType;
  496. SSDKContentType contenType;
  497. if (platName == 1) {
  498. playType = SSDKPlatformTypeFacebook;
  499. contenType = SSDKContentTypeWebPage;
  500. imageUrl = nil;
  501. } else if (platName == 2) {
  502. playType = SSDKPlatformTypeInstagram;
  503. contenType = SSDKContentTypeImage;
  504. titleStr = nil;
  505. } else if (platName == 3) {
  506. playType = SSDKPlatformTypeTwitter;
  507. contenType = SSDKContentTypeText;
  508. contentStr = [NSString stringWithFormat:@"%@ \n %@",contentStr,urlString];
  509. } else if (platName == 4) {
  510. playType = SSDKPlatformTypeLine;
  511. contenType = SSDKContentTypeText;
  512. contentStr = [NSString stringWithFormat:@"%@ \n %@",contentStr,urlString];
  513. } else if (platName == 5) {
  514. playType = SSDKPlatformTypeWhatsApp;
  515. contenType = SSDKContentTypeText;
  516. contentStr = [NSString stringWithFormat:@"%@ \n %@",contentStr,urlString];
  517. } else {
  518. //copy
  519. UIPasteboard *pab = [UIPasteboard generalPasteboard];
  520. [pab setString:urlString];
  521. if (pab == nil) {
  522. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_money_Failed")];
  523. } else {
  524. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_common_copy_tip")];
  525. }
  526. return;
  527. }
  528. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  529. [params SSDKSetupShareParamsByText:contentStr
  530. images:imageUrl
  531. url:[NSURL URLWithString:urlString]
  532. title:titleStr
  533. type:contenType];
  534. [ShareSDK share:playType
  535. parameters:params
  536. onStateChanged:^(SSDKResponseState state,
  537. NSDictionary *userData, SSDKContentEntity *contentEntity,
  538. NSError *error) {
  539. switch (state) {
  540. case SSDKResponseStateSuccess:
  541. MOLogV(@"成功");//成功
  542. [weakSelf toReportAgencyResultResponse:@"1"];
  543. break;
  544. case SSDKResponseStateFail:
  545. {
  546. MOLogV(@"--%@",error.description);
  547. if(error.code == 200104){
  548. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_room_tool_share_no_install_tip")];
  549. }
  550. [weakSelf toReportAgencyResultResponse:@"2"];
  551. //失败
  552. break;
  553. }
  554. case SSDKResponseStateCancel:
  555. //取消
  556. [weakSelf toReportAgencyResultResponse:@"3"];
  557. break;
  558. default:
  559. break;
  560. }
  561. }];
  562. #endif
  563. }
  564. - (void)shareURLWith:(NSString *)keyStr{
  565. WEAKSELF
  566. NSString *lowerKeyStr = [keyStr lowercaseString];
  567. NSString *titleStr = @"Mimo Live 2025 Hadiah Kesejahteraan Ramadan, login harian untuk menerima hadiah, jutaan diamond diberikan setiap hari!";
  568. NSString *shareStr = [NSString stringWithFormat:@"%@/#/ramadanShareDownload",kNetPath_Web_Base];
  569. NSString *contentStr = [NSString stringWithFormat:@"%@ \n %@",titleStr,shareStr];
  570. //copy
  571. UIPasteboard *pab = [UIPasteboard generalPasteboard];
  572. [pab setString:contentStr];
  573. if (pab == nil)
  574. {
  575. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_money_Failed")];
  576. }
  577. else
  578. {
  579. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_common_copy_tip")];
  580. }
  581. #if !TARGET_IPHONE_SIMULATOR
  582. __block SSDKPlatformType playType;
  583. if([lowerKeyStr isEqualToString:@"facebook"]){
  584. playType = SSDKPlatformTypeFacebook;
  585. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  586. [params SSDKSetupShareParamsByText:titleStr
  587. images:nil
  588. url:[NSURL URLWithString:shareStr]
  589. title:@"Mimo Live"
  590. type:SSDKContentTypeWebPage];
  591. [ShareSDK share:playType
  592. parameters:params
  593. onStateChanged:^(SSDKResponseState state,
  594. NSDictionary *userData, SSDKContentEntity *contentEntity,
  595. NSError *error) {
  596. switch (state) {
  597. case SSDKResponseStateSuccess:
  598. MOLogV(@"成功");//成功
  599. [weakSelf toReportTheShareSuccess];
  600. break;
  601. case SSDKResponseStateFail:
  602. {
  603. MOLogV(@"--%@",error.description);
  604. [weakSelf toReportTheShareSuccess];
  605. //失败
  606. break;
  607. }
  608. case SSDKResponseStateCancel:
  609. //取消
  610. [weakSelf toReportTheShareSuccess];
  611. break;
  612. default:
  613. break;
  614. }
  615. }];
  616. }
  617. else if ([lowerKeyStr isEqualToString:@"instagram"]){
  618. playType = SSDKPlatformTypeInstagram;
  619. NSString *mimoImgUrlStr = @"https://s3.mimolive.vip//images/myG9NN-kl69i11nhE795A0zN.png";
  620. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  621. [params SSDKSetupShareParamsByText:contentStr
  622. images:@[mimoImgUrlStr]
  623. url:[NSURL URLWithString:shareStr]
  624. title:nil
  625. type:SSDKContentTypeImage];
  626. [ShareSDK share:playType
  627. parameters:params
  628. onStateChanged:^(SSDKResponseState state,
  629. NSDictionary *userData, SSDKContentEntity *contentEntity,
  630. NSError *error) {
  631. switch (state) {
  632. case SSDKResponseStateSuccess:
  633. MOLogV(@"成功");//成功
  634. [weakSelf toReportTheShareSuccess];
  635. break;
  636. case SSDKResponseStateFail:
  637. {
  638. MOLogV(@"--%@",error.description);
  639. //失败
  640. [weakSelf toReportTheShareSuccess];
  641. break;
  642. }
  643. case SSDKResponseStateCancel:
  644. //取消
  645. [weakSelf toReportTheShareSuccess];
  646. break;
  647. default:
  648. break;
  649. }
  650. }];
  651. }
  652. else if ([lowerKeyStr isEqualToString:@"line"]){
  653. playType = SSDKPlatformTypeLine;
  654. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  655. [params SSDKSetupShareParamsByText:contentStr
  656. images:nil
  657. url:nil
  658. title:nil
  659. type:SSDKContentTypeText];
  660. [ShareSDK share:playType
  661. parameters:params
  662. onStateChanged:^(SSDKResponseState state,
  663. NSDictionary *userData, SSDKContentEntity *contentEntity,
  664. NSError *error) {
  665. switch (state) {
  666. case SSDKResponseStateSuccess:
  667. MOLogV(@"成功");//成功
  668. [weakSelf toReportTheShareSuccess];
  669. break;
  670. case SSDKResponseStateFail:
  671. {
  672. MOLogV(@"--%@",error.description);
  673. //失败
  674. [weakSelf toReportTheShareSuccess];
  675. break;
  676. }
  677. case SSDKResponseStateCancel:
  678. //取消
  679. [weakSelf toReportTheShareSuccess];
  680. break;
  681. default:
  682. break;
  683. }
  684. }];
  685. }
  686. else if ([lowerKeyStr isEqualToString:@"twitter"]){
  687. playType = SSDKPlatformTypeTwitter;
  688. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  689. [params SSDKSetupShareParamsByText:contentStr
  690. images:nil
  691. url:nil
  692. title:nil
  693. type:SSDKContentTypeText];
  694. [ShareSDK share:playType
  695. parameters:params
  696. onStateChanged:^(SSDKResponseState state,
  697. NSDictionary *userData, SSDKContentEntity *contentEntity,
  698. NSError *error) {
  699. switch (state) {
  700. case SSDKResponseStateSuccess:
  701. MOLogV(@"成功");//成功
  702. [weakSelf toReportTheShareSuccess];
  703. break;
  704. case SSDKResponseStateFail:
  705. {
  706. MOLogV(@"--%@",error.description);
  707. //失败
  708. [weakSelf toReportTheShareSuccess];
  709. break;
  710. }
  711. case SSDKResponseStateCancel:
  712. //取消
  713. [weakSelf toReportTheShareSuccess];
  714. break;
  715. default:
  716. break;
  717. }
  718. }];
  719. }
  720. else if ([lowerKeyStr isEqualToString:@"whatsapp"]){
  721. playType = SSDKPlatformTypeWhatsApp;
  722. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  723. [params SSDKSetupShareParamsByText:titleStr
  724. images:nil
  725. url:[NSURL URLWithString:shareStr]
  726. title:@"Mimo Live"
  727. type:SSDKContentTypeText];
  728. [ShareSDK share:playType
  729. parameters:params
  730. onStateChanged:^(SSDKResponseState state,
  731. NSDictionary *userData, SSDKContentEntity *contentEntity,
  732. NSError *error) {
  733. switch (state) {
  734. case SSDKResponseStateSuccess:
  735. MOLogV(@"成功");//成功
  736. [weakSelf toReportTheShareSuccess];
  737. break;
  738. case SSDKResponseStateFail:
  739. {
  740. MOLogV(@"--%@",error.description);
  741. //失败
  742. [weakSelf toReportTheShareSuccess];
  743. break;
  744. }
  745. case SSDKResponseStateCancel:
  746. //取消
  747. [weakSelf toReportTheShareSuccess];
  748. break;
  749. default:
  750. break;
  751. }
  752. }];
  753. }
  754. #endif
  755. }
  756. - (void)toReportTheShareSuccess{
  757. //submitShareLinkResponse()
  758. NSString *jsString = [NSString stringWithFormat:@"%@()",kWebCallBack_ToSubmitShareLinkResponse];
  759. [self.poWebView evaluateJavaScript:jsString completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
  760. MOLogV(@"error = %@ , response = %@",error, resp);
  761. }];
  762. }
  763. /// 全民代理分享结果通知H5
  764. /// - Parameter result: 1:成功 2.失败 3.取消
  765. - (void)toReportAgencyResultResponse:(NSString *)result {
  766. NSString *jsString = [NSString stringWithFormat:@"%@(%@)",kWebCallBack_GetShareLinkAgencyResultResponse, result];
  767. [self.poWebView evaluateJavaScript:jsString completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
  768. MOLogV(@"error = %@ , response = %@",error, resp);
  769. }];
  770. }
  771. - (void)tokenExpiredAndGetTheUserInfo{
  772. WEAKSELF
  773. [kHttpManager getMeUserInfoWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  774. }];
  775. }
  776. - (void)toExportUrlWith:(NSString *)jsonStr{
  777. NSDictionary *dict = [self dictionaryWithJsonString:jsonStr];
  778. NSString *theUrl = [MODataManager objectOrNilForKey:@"url" fromDictionary:dict[@"data"]];
  779. NSURL *url = [NSURL URLWithString:theUrl];
  780. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  781. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  782. }
  783. }
  784. - (void)getConfig:(NSDictionary *)args{
  785. MOLogV(@"BSWAME %s","调⽤getConfig");
  786. NSString* method = [args objectForKey:@"jsCallback"];
  787. NSString *languagaStr = [MOWebViewController getWameCurrentLanguages];
  788. //gsp 101新加坡 201迪拜 301硅谷 401法兰克福
  789. NSDictionary *configDict = [self.wame2Config dictionaryRepresentation];
  790. NSMutableDictionary *needDict = [NSMutableDictionary dictionaryWithDictionary:configDict];
  791. [needDict setObject:languagaStr forKey:@"language"];
  792. //wameMode 2 半屏 3 全屏
  793. if(self.isMiniWame){
  794. [needDict setObject:@"2" forKey:[MOTextAESTools toGetHameMode]];
  795. }
  796. else{
  797. [needDict setObject:@"3" forKey:[MOTextAESTools toGetHameMode]];
  798. }
  799. //房间ID
  800. NSString *roomId = self.roomId;
  801. if(roomId.length == 0){
  802. roomId = @"";
  803. }
  804. [needDict setObject:roomId forKey:@"roomId"];
  805. [self callJs:method withJavaScriptValue:needDict];
  806. }
  807. - (void)so88GetConfig:(NSString *)methodName{
  808. NSString* method = methodName;
  809. NSDictionary *configDict = [self.enterBaseData.so88GM dictionaryRepresentation];
  810. NSMutableDictionary *needDict = [NSMutableDictionary dictionaryWithDictionary:configDict];
  811. NSString *languageStr = [self getTheNewCurrentLanguages];
  812. [needDict setObject:languageStr forKey:@"language"];
  813. #if kAPP_Environment == 1
  814. //生产
  815. [needDict setObject:@"4" forKey:@"server"];
  816. #elif kAPP_Environment == 2
  817. //测试
  818. [needDict setObject:@"2" forKey:@"server"];
  819. #endif
  820. [self callJs:method withJavaScriptValue:needDict];
  821. }
  822. - (void)callJs:(NSString *)method withJavaScriptValue:(nullable id)arguments{
  823. if(arguments){
  824. NSData *data = [NSJSONSerialization dataWithJSONObject:arguments options:(NSJSONWritingPrettyPrinted) error:nil];
  825. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  826. NSString *jsMethods = [NSString stringWithFormat:@"%@(%@)",method,jsonStr];
  827. [self.poWebView evaluateJavaScript:jsMethods completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
  828. MOLogV(@"error = %@ , response = %@",error, resp);
  829. }];
  830. }
  831. else{
  832. NSString *jsMethods = [NSString stringWithFormat:@"%@({})", method];
  833. [self.poWebView evaluateJavaScript:jsMethods completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
  834. MOLogV(@"error = %@ , response = %@",error, resp);
  835. }];
  836. }
  837. }
  838. - (void)noMoneyTipShow{
  839. WEAKSELF
  840. MOTitleNormalAlertView *alertView = [[MOTitleNormalAlertView alloc] init];
  841. alertView.titleLabel.text = NSLocalString(@"mimo_alert_normal_tip");
  842. alertView.subTitleLabel.text = NSLocalString(@"mimo_no_money_tip");
  843. [alertView.cancelBtn setTitle:NSLocalString(@"mimo_Cancel") forState:UIControlStateNormal];
  844. [alertView.confirmBtn setTitle:NSLocalString(@"mimo_no_money_jump") forState:UIControlStateNormal];
  845. alertView.confirmBlock = ^{
  846. //跳转充值界面 (全屏)
  847. [weakSelf toPushTopUpVC];
  848. };
  849. [alertView show];
  850. }
  851. - (void)toPushTopUpVC{
  852. if(self.pushTag){
  853. return;
  854. }
  855. self.pushTag = YES;
  856. [self performSelector:@selector(resetPushTag) withObject:nil afterDelay:2.0];
  857. WEAKSELF
  858. MOTopUpVC *vc = [[MOTopUpVC alloc] init];
  859. vc.needRefreshBlock = ^{
  860. if(weakSelf.enterBaseData.type == 3){
  861. if(weakSelf.enterBaseData.so88GM.userId.length > 0){
  862. NSObject *map = @{@"userId":weakSelf.enterBaseData.so88GM.userId};
  863. [weakSelf callJs:@"walletUpdate" withJavaScriptValue:map];
  864. }
  865. }
  866. else{
  867. NSString *userId = GetUserId;
  868. if(userId.length > 0){
  869. NSObject *map = @{@"userId":userId};
  870. [weakSelf callJs:@"walletUpdate" withJavaScriptValue:map];
  871. }
  872. }
  873. };
  874. [self.navigationController pushViewController:vc animated:YES];
  875. }
  876. - (void)resetPushTag{
  877. self.pushTag = NO;
  878. }
  879. - (void)toUserHomePageWithUrl:(NSString *)urlString{
  880. NSString *userId;
  881. if ([urlString rangeOfString:@"userId="].location != NSNotFound)
  882. {
  883. userId = [urlString componentsSeparatedByString:@"userId="].lastObject;
  884. }
  885. if(userId.length == 0){
  886. return ;
  887. }
  888. MOUserHomePageVC *vc = [[MOUserHomePageVC alloc] init];
  889. vc.userId = userId;
  890. [self.navigationController pushViewController:vc animated:YES];
  891. }
  892. - (void)openRechargeView{
  893. NSURL *url = [NSURL URLWithString:@"mimo://jump_native:80/recharge"];
  894. if ([[UIApplication sharedApplication]respondsToSelector:@selector(openURL:options:completionHandler:)])
  895. {
  896. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}];
  897. }
  898. else
  899. {
  900. [[UIApplication sharedApplication]openURL:url];
  901. }
  902. }
  903. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler
  904. {
  905. }
  906. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  907. // 获取跳转到的新地址
  908. NSString *urlString = webView.URL.absoluteString;
  909. MOLogV(@"Navigated to URL: %@", urlString);
  910. }
  911. /* 开始返回内容 */
  912. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
  913. {
  914. NSString *urlString = webView.URL.absoluteString;
  915. MOLogV(@"Navigated to URL: %@", urlString);
  916. WEAKSELF
  917. [webView evaluateJavaScript:@"document.title" completionHandler:^(NSString *title, NSError * _Nullable error) {
  918. if(self.isShowTitle){
  919. if(title.length > 0 ){
  920. if([title isEqualToString:@"MINO LIVE"]){
  921. self.navigationItem.title = @"Mimo Live";
  922. }
  923. else{
  924. self.navigationItem.title = title;
  925. }
  926. }
  927. }
  928. }];
  929. }
  930. /* 页面开始加载 */
  931. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  932. NSString *urlString = webView.URL.absoluteString;
  933. MOLogV(@"Navigated to URL: %@", urlString);
  934. }
  935. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
  936. NSString *urlString = webView.URL.absoluteString;
  937. MOLogV(@"Navigated to URL: %@", urlString);
  938. }
  939. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  940. // 在收到响应开始加载后,决定是否跳转
  941. NSString *urlString = webView.URL.absoluteString;
  942. MOLogV(@"Navigated to URL: %@", urlString);
  943. decisionHandler(WKNavigationResponsePolicyAllow);
  944. }
  945. /// 页面加载失败,指初始化第一个页面
  946. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
  947. {
  948. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(reloadWebViewData) object:nil];
  949. [self performSelector:@selector(reloadWebViewData) withObject:nil afterDelay:2.0];
  950. }
  951. - (void)reloadWebViewData{
  952. #if kAPP_Environment == 1
  953. NSURLRequestCachePolicy policy = NSURLRequestUseProtocolCachePolicy;
  954. #elif kAPP_Environment == 2
  955. NSURLRequestCachePolicy policy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
  956. #endif
  957. self.request = [NSURLRequest requestWithURL:self.webUrl cachePolicy:policy timeoutInterval:40];
  958. MOLogV(@"[WebView] init poWebView(%d) url:%@",self.webViewTag,self.webUrl);
  959. [self.poWebView loadRequest:self.request];
  960. }
  961. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  962. {
  963. if ([keyPath isEqualToString:@"title"])
  964. {
  965. if (object == self.poWebView)
  966. {
  967. // NSString *newTitle = change[NSKeyValueChangeNewKey];
  968. // MOLogV(@"网页标题变化: %@", newTitle);
  969. // self.navigationItem.title = newTitle;
  970. }
  971. else
  972. {
  973. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  974. }
  975. }
  976. // else if ([keyPath isEqualToString:@"estimatedProgress"]) {
  977. // self.progressView.progress = self.poWebView.estimatedProgress;
  978. //
  979. // // 隐藏进度条,当加载完成时
  980. // if (self.poWebView.estimatedProgress >= 1.0) {
  981. // self.progressView.hidden = YES;
  982. // } else {
  983. // self.progressView.hidden = NO;
  984. // }
  985. // }
  986. else
  987. {
  988. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  989. }
  990. }
  991. /* 在发送请求之前,决定是否跳转 */
  992. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  993. NSString *urlString = navigationAction.request.URL.absoluteString;
  994. MOLogV(@"mimo-urlString-%@",urlString);
  995. if([urlString containsString:@"mimo://"]){
  996. NSURL *url = [NSURL URLWithString:urlString];
  997. MOLogV(@"mimo-urlString-%@",urlString);
  998. //个人主页跳转
  999. if([urlString containsString:@"mimo://jump_native:80/homePage"]){
  1000. [self toUserHomePageWithUrl:urlString];
  1001. decisionHandler(WKNavigationActionPolicyCancel);
  1002. return;
  1003. }
  1004. if ([[UIApplication sharedApplication]respondsToSelector:@selector(openURL:options:completionHandler:)])
  1005. {
  1006. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {}];
  1007. }
  1008. else
  1009. {
  1010. [[UIApplication sharedApplication]openURL:webView.URL];
  1011. }
  1012. decisionHandler(WKNavigationActionPolicyCancel);
  1013. return;
  1014. }
  1015. MOLogV(@"12313");
  1016. decisionHandler(WKNavigationActionPolicyAllow);
  1017. }
  1018. - (void)dealloc{
  1019. // [self.poWebView removeObserver:self forKeyPath:@"title"];
  1020. [[MOChatNotificationManager sharedManager] leaveScene:MOChatNotificationSceneWame];//进入聊天列表场景
  1021. [[MOChatNotificationManager sharedManager] leaveScene:MOChatNotificationSceneWidraw];//进入聊天列表场景
  1022. if(self.needUpdateDiamond){
  1023. [[MORefreshViewManage shareManager] toUpdataTheUserInfo];
  1024. }
  1025. [self removeHandler];
  1026. }
  1027. - (void)removeHandler{
  1028. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_Close];
  1029. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_GetUserInfo];
  1030. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_GetStatusHeight];
  1031. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_GetLanguage];
  1032. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_GetDiamond];
  1033. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_ToGetRain];
  1034. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_ToSubmitShareLink];
  1035. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_TokenExpired];
  1036. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_ExportUrl];
  1037. //
  1038. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:@"recharge"];
  1039. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:@"clickRecharge"];
  1040. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:@"newTppClose"];
  1041. // 2
  1042. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:@"getConfig"];
  1043. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:@"destroy"];
  1044. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:[MOTextAESTools toGetHameRecharge]];
  1045. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:[MOTextAESTools toGetHameLoaded]];
  1046. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_ShareToPlatAgency];
  1047. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_BindAgencySuccess];
  1048. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_ChagneMysteriousStatus];
  1049. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_GetBlindBoxScreenParams];
  1050. [_poWebView.configuration.userContentController removeScriptMessageHandlerForName:kWebCallBack_SetBlindBoxResult];
  1051. }
  1052. - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
  1053. if(jsonString == nil && jsonString.length == 0){
  1054. return nil;
  1055. }
  1056. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  1057. NSError *err;
  1058. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
  1059. if(err){
  1060. MOLogV(@"json解析失败:%@",err);
  1061. return nil;
  1062. }
  1063. return dic;
  1064. }
  1065. - (BigBtn *)closeBtn{
  1066. if (!_closeBtn)
  1067. {
  1068. _closeBtn = [BigBtn buttonWithType:UIButtonTypeCustom];
  1069. _closeBtn.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.3];
  1070. [_closeBtn setImage:[UIImage imageNamed:@"icon_live_close"] forState:UIControlStateNormal];
  1071. [_closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
  1072. }
  1073. return _closeBtn;
  1074. }
  1075. - (void)closeBtnClick{
  1076. [self LeftBarItemClick];
  1077. }
  1078. + (NSString *)getWameCurrentLanguages{
  1079. //2 英语
  1080. NSString *defaultLanguageStr = @"2";
  1081. NSArray *languages = [NSLocale preferredLanguages];
  1082. if(languages.count > 0){
  1083. NSString *languageStr = [languages firstObject];
  1084. //印尼语
  1085. if([languageStr hasPrefix:@"id"]){
  1086. defaultLanguageStr = @"3";
  1087. }
  1088. //中文
  1089. if([languageStr hasPrefix:@"zh"] || [languageStr hasPrefix:@"tw"]){
  1090. defaultLanguageStr = @"0";
  1091. }
  1092. }
  1093. return defaultLanguageStr;
  1094. }
  1095. - (NSString *)getTheNewCurrentLanguages{
  1096. NSString *defaultLanguageStr = @"en";
  1097. NSArray *languages = [NSLocale preferredLanguages];
  1098. if(languages.count > 0){
  1099. NSString *languageStr = [languages firstObject];
  1100. //中文
  1101. if([languageStr hasPrefix:@"zh"] || [languageStr hasPrefix:@"tw"]){
  1102. defaultLanguageStr = @"zh";
  1103. }
  1104. }
  1105. return defaultLanguageStr;
  1106. }
  1107. @end