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

120 lines
3.3 KiB
TypeScript
Raw Normal View History

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