Files
rougelike-demo/client/assets/scripts/App/Login/UILogin.ts

117 lines
3.1 KiB
TypeScript
Raw Normal View History

import { _decorator, Button, EditBox } from 'cc';
2025-12-14 23:34:50 +08:00
import { NetManager } from '../../Framework/Net/NetManager';
import { UIBase } from '../../Framework/UI/UIBase';
2025-12-14 23:34:50 +08:00
import { AppStatusManager } from '../AppStatus/AppStatusManager';
import { LoginMessagePair } from '../Msg/Pair/LoginMessagePair';
2025-12-14 23:34:50 +08:00
const { ccclass } = _decorator;
/**
*
* :
* - UI
* -
* -
* -
* -
*/
@ccclass('UILogin')
export class UILogin extends UIBase {
/** 账号输入框 */
private _inputAccount: EditBox | null = null;
2025-12-14 23:34:50 +08:00
/** 登录按钮 */
private _btnLogin: Button | null = null;
2025-12-14 23:34:50 +08:00
/** 是否正在登录中 */
private _isLogging: boolean = false;
2025-12-14 23:34:50 +08:00
/**
* UI资源路径
*/
onGetUrl(): string {
return 'res://UI/Login/UILogin';
}
2025-12-14 23:34:50 +08:00
/**
* UI初始化
*/
async onStart(params?: any): Promise<void> {
console.log('[UILogin] 登录界面初始化', params);
2025-12-14 23:34:50 +08:00
// 使用GetChild查找子节点
this._inputAccount = this.GetChild('mid/input_account', EditBox);
this._btnLogin = this.GetChild('mid/btn_login', Button);
2025-12-14 23:34:50 +08:00
// 使用SetClick绑定点击事件
this.SetClick(this._btnLogin, this.onLoginClick, this);
}
2025-12-14 23:34:50 +08:00
/**
*
*/
private async onLoginClick(): Promise<void> {
if (this._isLogging) {
console.log('[UILogin] 正在登录中,请稍候...');
return;
}
2025-12-14 23:34:50 +08:00
// 获取账号
const account = this._inputAccount?.string?.trim() || '';
2025-12-14 23:34:50 +08:00
if (!account) {
console.warn('[UILogin] 请输入账号');
// TODO: 显示提示UI
return;
}
2025-12-14 23:34:50 +08:00
console.log(`[UILogin] 开始登录,账号: ${account}`);
2025-12-14 23:34:50 +08:00
// 开始登录
this._isLogging = true;
2025-12-14 23:34:50 +08:00
try {
await this.login(account);
} catch (error) {
console.error('[UILogin] 登录失败:', error);
// TODO: 显示错误提示UI
} finally {
this._isLogging = false;
}
}
2025-12-14 23:34:50 +08:00
/**
*
* @param account
*/
private async login(account: string): Promise<void> {
const netManager = NetManager.getInstance();
2025-12-14 23:34:50 +08:00
// 使用类型化的登录协议
const result = await netManager.callMsg(new LoginMessagePair(), {
playerId: account
},);
2025-12-14 23:34:50 +08:00
if (result && result.success) {
console.log('[UILogin] 登录成功:', result);
2025-12-14 23:34:50 +08:00
// 登录成功,切换到游戏状态
const appStatusManager = AppStatusManager.getInstance();
2025-12-18 16:04:56 +08:00
appStatusManager.changeState('Game', result);
2025-12-14 23:34:50 +08:00
// 隐藏登录界面
this.hide();
} else {
console.error('[UILogin] 登录失败:', result?.message || '返回结果为空');
// TODO: 显示错误提示UI
}
}
2025-12-14 23:34:50 +08:00
/**
* UI清理
*/
onEnd(): void {
console.log('[UILogin] 登录界面清理');
}
}