rp_11009/assets/Game/Scripts/WebView.ts
2026-04-01 11:10:33 +08:00

124 lines
4.3 KiB
TypeScript

import { _decorator, Component, director, Label, log, Node, tween, UITransform, v3, Vec3, view, WebView } from 'cc';
import { getHistoryUrl, getOddsUrl, getSupportUrl } from '../../Main/Scripts/main/comm';
import { I18nManager } from '../../Main/Scripts/managers/I18nManager';
import { AudioManager } from '../../Main/Scripts/managers/AudioManager';
import { NodePoolManager } from '../../Main/Scripts/managers/NodePoolManager';
const { ccclass, property } = _decorator;
@ccclass('webView')
export class webView extends Component {
@property(WebView)
webV: WebView = null;
@property(Node)
mainNode: Node = null;
@property(Label)
title: Label = null;
@property(Node)
loadingView: Node = null;
startPos = v3(0, 0, 0);
endPos = v3(0, 0, 0);
onLoad(): void {
this.startPos.y = -view.getVisibleSize().height;
this.endPos.y = 0;
this.mainNode.active = false;
}
start(): void {
this.webV.node.on(WebView.EventType.LOADED, this.webViewLoaded, this);
this.webV.node.on(WebView.EventType.ERROR, this.webViewError, this);
this.webV.node.on(WebView.EventType.LOADING, this.webViewLoading, this);
const waitForIframe = () => {
const iframe = document.querySelector('iframe');
if (iframe) {
iframe.setAttribute('scrolling', 'no');
iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts allow-forms allow-top-navigation-by-user-activation allow-popups');
} else {
setTimeout(waitForIframe, 100);
}
};
waitForIframe();
}
webViewLoading() {
this.loadingView.active = true;
}
webViewLoaded() {
this.scheduleOnce(() => {
this.loadingView.active = false;
this.webV.node.active = true;
}, 0.8);
}
webViewError() {
console.error('WebView load failed');
this.loadingView.active = false;
// 可以显示错误提示
this.onBtnClose();
}
// 'http://192.168.0.30:5173/#/rp_fortuneRabbit/support?l=en';
async open(param: string) {
try {
this.mainNode.active = true;
this.mainNode.setPosition(this.startPos);
tween(this.mainNode)
.set({ position: this.startPos })
.call(() => {
this.mainNode.active = true;
})
.to(0.2, { position: this.endPos }, { easing: 'quadOut' })
.call(() => {
this.loadingView.active = true;
// 计算初始位置 - 屏幕中心
this.webV.node.active = false;
let url = '';
let title = '';
if (param == 'rule') {
url = getSupportUrl();
title = I18nManager.instance.t('AID_MAIN_OPERATE_RULES');
} else if (param == 'paytable') {
url = getOddsUrl();
title = I18nManager.instance.t('AID_MAIN_OPERATE_PAYTABLE');
} else if (param == 'history') {
url = getHistoryUrl();
title = I18nManager.instance.t('AID_MAIN_OPERATE_HISTORY');
}
this.title.string = title;
this.webV.url = url;
})
.start();
} catch (error) {
console.error('WebView open failed:', error);
this.onBtnClose();
}
}
isClosing: boolean = false; // 添加状态标记
onBtnClose() {
// 如果正在关闭中,直接返回
if (this.isClosing) return;
tween(this.mainNode)
.to(0.2, { position: this.startPos }, { easing: 'quadOut' })
.call(() => {
this.isClosing = true; // 设置关闭状态
AudioManager.instance.playSFX('Click_Menu');
this.webV.node.off(WebView.EventType.LOADED, this.webViewLoaded, this);
this.webV.node.off(WebView.EventType.ERROR, this.webViewError, this);
this.webV.node.off(WebView.EventType.LOADING, this.webViewLoading, this);
this.node.destroy();
})
.start();
}
}