148 lines
4.1 KiB
TypeScript
148 lines
4.1 KiB
TypeScript
import { _decorator, Node, Button, Label } from 'cc';
|
||
import { BaseView } from 'db://assets/scripts/core/BaseView';
|
||
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
|
||
import { WxSDK, checkPrivacySetting, requirePrivacyAuthorize } from 'db://assets/scripts/utils/WxSDK';
|
||
import { StaminaManager } from 'db://assets/scripts/utils/StaminaManager';
|
||
import { ToastManager } from 'db://assets/scripts/utils/ToastManager';
|
||
import { StaminaInfo } from 'db://assets/scripts/types/ApiTypes';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/**
|
||
* 首页组件
|
||
* 继承 BaseView,实现页面生命周期
|
||
*/
|
||
@ccclass('PageHome')
|
||
export class PageHome extends BaseView {
|
||
/** 默认体力上限 */
|
||
private static readonly DEFAULT_STAMINA_MAX = 50;
|
||
|
||
@property({ type: Node, tooltip: '开始游戏按钮' })
|
||
startGameBtn: Node | null = null;
|
||
|
||
@property({ type: Node, tooltip: 'PK按钮' })
|
||
pkBtn: Node | null = null;
|
||
|
||
/** 体力值显示标签 */
|
||
@property(Label)
|
||
liveLabel: Label | null = null;
|
||
|
||
/**
|
||
* 页面首次加载时调用
|
||
*/
|
||
onViewLoad(): void {
|
||
console.log('[PageHome] onViewLoad');
|
||
this._initButtons();
|
||
this._initWxShare();
|
||
// 检查隐私授权
|
||
this._checkPrivacyAuthorization();
|
||
}
|
||
|
||
/**
|
||
* 初始化微信分享功能
|
||
*/
|
||
private _initWxShare(): void {
|
||
WxSDK.initShare({
|
||
title: '写英语',
|
||
imageUrl: '',
|
||
query: ''
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 检查隐私授权状态
|
||
*/
|
||
private async _checkPrivacyAuthorization(): Promise<void> {
|
||
if (!WxSDK.isWechat()) {
|
||
console.log('[PageHome] 非微信环境,跳过隐私授权检查');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const { needAuthorization } = await checkPrivacySetting();
|
||
if (needAuthorization) {
|
||
console.log('[PageHome] 用户未授权隐私,引导授权');
|
||
await requirePrivacyAuthorize();
|
||
} else {
|
||
console.log('[PageHome] 用户已授权隐私');
|
||
}
|
||
} catch (err) {
|
||
console.warn('[PageHome] 隐私授权检查异常:', err);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 初始化按钮事件
|
||
*/
|
||
private _initButtons(): void {
|
||
if (this.startGameBtn) {
|
||
this.startGameBtn.on(Button.EventType.CLICK, this._onStartGameClick, this);
|
||
}
|
||
if (this.pkBtn) {
|
||
this.pkBtn.on(Button.EventType.CLICK, this._onPkClick, this);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 开始游戏按钮点击回调
|
||
*/
|
||
private _onStartGameClick(): void {
|
||
console.log('[PageHome] 开始游戏按钮点击');
|
||
ViewManager.instance.open('PageLevel');
|
||
}
|
||
|
||
/**
|
||
* PK按钮点击回调
|
||
*/
|
||
private _onPkClick(): void {
|
||
console.log('[PageHome] PK按钮点击');
|
||
ToastManager.show('功能正在开发中,敬请期待吧!');
|
||
}
|
||
|
||
/**
|
||
* 页面每次显示时调用
|
||
*/
|
||
onViewShow(): void {
|
||
console.log('[PageHome] onViewShow');
|
||
this.updateStaminaLabel();
|
||
}
|
||
|
||
/**
|
||
* 获取体力上限
|
||
*/
|
||
private _getStaminaMax(stamina: StaminaInfo): number {
|
||
return typeof stamina.max === 'number' ? stamina.max : PageHome.DEFAULT_STAMINA_MAX;
|
||
}
|
||
|
||
/**
|
||
* 更新体力值显示
|
||
*/
|
||
private updateStaminaLabel(): void {
|
||
if (this.liveLabel) {
|
||
const stamina = StaminaManager.instance.getStamina();
|
||
const maxStamina = this._getStaminaMax(stamina);
|
||
this.liveLabel.string = `${stamina.current}/${maxStamina}`;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 页面隐藏时调用
|
||
*/
|
||
onViewHide(): void {
|
||
console.log('[PageHome] onViewHide');
|
||
}
|
||
|
||
/**
|
||
* 页面销毁时调用
|
||
*/
|
||
onViewDestroy(): void {
|
||
console.log('[PageHome] onViewDestroy');
|
||
// 移除按钮事件监听
|
||
if (this.startGameBtn) {
|
||
this.startGameBtn.off(Button.EventType.CLICK, this._onStartGameClick, this);
|
||
}
|
||
if (this.pkBtn) {
|
||
this.pkBtn.off(Button.EventType.CLICK, this._onPkClick, this);
|
||
}
|
||
}
|
||
}
|