增加用户名登录

This commit is contained in:
2026-02-24 21:53:24 +08:00
parent f36d6cb9e9
commit 9ae4e8e614
23 changed files with 388 additions and 110 deletions

View File

@@ -7,8 +7,14 @@
#include "GlobalJosn.h"
#include "YvVoiceManager.hpp"
#include "13S_GameLogic.h"
#include "MD5.h"
LogonScene::LogonScene()
: m_pPanelAccount(nullptr)
, m_pTxtUsername(nullptr)
, m_pTxtPassword(nullptr)
, m_pBtnLogin(nullptr)
, m_pBtnClose(nullptr)
{
m_kLoginMission.setUrl(GlobalJosn::getInstance()->m_strLogonIp.c_str(), GlobalJosn::getInstance()->m_iPort);
@@ -42,6 +48,30 @@ bool LogonScene::init()
CC_ASSERT(btnWeiXinLogon != nullptr);
btnWeiXinLogon->addClickEventListener(CC_CALLBACK_1(LogonScene::onWeiXinLogon, this));
// Account login entry button
auto btnAccountLogin = (Button*)rootPanel->getChildByName("btnAccountLogin");
if (btnAccountLogin)
{
btnAccountLogin->addClickEventListener(CC_CALLBACK_1(LogonScene::onShowAccountPanel, this));
}
// Account login panel
m_pPanelAccount = rootPanel->getChildByName("panelAccount");
if (m_pPanelAccount)
{
m_pPanelAccount->setVisible(false);
auto imgBg = m_pPanelAccount->getChildByName("imgBg");
if (imgBg)
{
m_pTxtUsername = (TextField*)imgBg->getChildByName("txtUsername");
m_pTxtPassword = (TextField*)imgBg->getChildByName("txtPassword");
m_pBtnLogin = (Button*)imgBg->getChildByName("btnLogin");
m_pBtnClose = (Button*)imgBg->getChildByName("btnClose");
if (m_pBtnLogin) m_pBtnLogin->addClickEventListener(CC_CALLBACK_1(LogonScene::onUsernameLogon, this));
if (m_pBtnClose) m_pBtnClose->addClickEventListener(CC_CALLBACK_1(LogonScene::onHideAccountPanel, this));
}
}
//对手机返回键的监听
auto keyListener = EventListenerKeyboard::create();
@@ -139,13 +169,22 @@ void LogonScene::onGPLoginSuccess()
UserDefault::getInstance()->setStringForKey("Password", m_kPssword);
// 更新微信最新信息;
pGlobalUserData->cbGender = m_kWeiXinUserInfo.sex;
strncpy(pGlobalUserData->szNickName, m_kWeiXinUserInfo.nickname.c_str(), LEN_NICKNAME - 1);
strncpy(pGlobalUserData->szHeadHttp, m_kWeiXinUserInfo.headimgurl.c_str(), LEN_HEAD_HTTP - 1);
// Only update WeChat info for WeChat login (not username login)
if (!m_kWeiXinUserInfo.openid.empty())
{
pGlobalUserData->cbGender = m_kWeiXinUserInfo.sex;
strncpy(pGlobalUserData->szNickName, m_kWeiXinUserInfo.nickname.c_str(), LEN_NICKNAME - 1);
strncpy(pGlobalUserData->szHeadHttp, m_kWeiXinUserInfo.headimgurl.c_str(), LEN_HEAD_HTTP - 1);
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32
UserInfo::Instance().modifyWeiXinInfo(m_kWeiXinUserInfo.sex, m_kWeiXinUserInfo.nickname, m_kWeiXinUserInfo.headimgurl);
UserInfo::Instance().modifyWeiXinInfo(m_kWeiXinUserInfo.sex, m_kWeiXinUserInfo.nickname, m_kWeiXinUserInfo.headimgurl);
#endif
}
else
{
// Username login: clear head URL to use default avatar
pGlobalUserData->szHeadHttp[0] = '\0';
}
}
}
@@ -159,7 +198,8 @@ void LogonScene::onGPLoginComplete()
void LogonScene::onGPLoginFailure(unsigned int iErrorCode,const char* szDescription)
{
if (iErrorCode == 3 || iErrorCode == 1)
// Only auto-register for WeChat login, show error for username login
if ((iErrorCode == 3 || iErrorCode == 1) && !m_kWeiXinUserInfo.openid.empty())
{
RegisterAccount();
}
@@ -339,4 +379,81 @@ void LogonScene::ResponseResult(rapidjson::Document* pDoc)
}
#endif
}
//==================================================
// Username/Password Login
//==================================================
void LogonScene::onShowAccountPanel(Ref*)
{
YSAudioEngine::Instance().playBtnClickEffect();
if (m_pPanelAccount)
{
m_pPanelAccount->setVisible(true);
if (m_pTxtUsername) m_pTxtUsername->setString("");
if (m_pTxtPassword) m_pTxtPassword->setString("");
}
}
void LogonScene::onHideAccountPanel(Ref*)
{
YSAudioEngine::Instance().playBtnClickEffect();
if (m_pPanelAccount)
{
m_pPanelAccount->setVisible(false);
}
}
void LogonScene::onUsernameLogon(Ref*)
{
YSAudioEngine::Instance().playBtnClickEffect();
std::string strUsername = "";
std::string strPassword = "";
if (m_pTxtUsername) strUsername = m_pTxtUsername->getString();
if (m_pTxtPassword) strPassword = m_pTxtPassword->getString();
// Input validation
if (strUsername.empty())
{
PopScene::Instance().show(utility::a_u8("请输入用户名"));
return;
}
if (strPassword.empty())
{
PopScene::Instance().show(utility::a_u8("请输入密码"));
return;
}
if (strUsername.length() < 4)
{
PopScene::Instance().show(utility::a_u8("用户名至少4个字符"));
return;
}
if (strPassword.length() < 6)
{
PopScene::Instance().show(utility::a_u8("密码至少6个字符"));
return;
}
if (m_pPanelAccount) m_pPanelAccount->setVisible(false);
LoadingScene::Instance().show(this);
// MD5 encrypt password
m_kPssword = md5(strPassword);
// Clear WeChat info (mark as username login)
m_kWeiXinUserInfo = WxUserInfo();
// Build login packet
CMD_GP_LogonAccounts loginAccount;
zeromemory(&loginAccount, sizeof(loginAccount));
loginAccount.dwPlazaVersion = Helps::Instance()->GetPlazaVersion();
loginAccount.cbValidateFlags = MB_VALIDATE_FLAGS | LOW_VER_VALIDATE_FLAGS;
strcpy(loginAccount.szAccounts, strUsername.c_str());
strcpy(loginAccount.szPassword, m_kPssword.c_str());
loginAccount.szOpenId[0] = '\0';
loginAccount.szUnionid[0] = '\0';
cocos2d::log("Username Login: account=%s", loginAccount.szAccounts);
m_kLoginMission.loginAccount(loginAccount);
}

View File

@@ -1,6 +1,8 @@
#pragma once
#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "MD5.h"
#include "MissionWeiXin.h"
#include "LoginMission.h"
@@ -51,10 +53,21 @@ public:
//请求版本信息结果
void ResponseResult(rapidjson::Document* pDoc);
// Username login
void onShowAccountPanel(Ref*);
void onHideAccountPanel(Ref*);
void onUsernameLogon(Ref*);
private:
LoginMission m_kLoginMission;
std::string m_kPssword;
WxUserInfo m_kWeiXinUserInfo;
// Account login UI
Node* m_pPanelAccount;
cocos2d::ui::TextField* m_pTxtUsername;
cocos2d::ui::TextField* m_pTxtPassword;
cocos2d::ui::Button* m_pBtnLogin;
cocos2d::ui::Button* m_pBtnClose;
};