增加用户名密码登录

This commit is contained in:
2026-03-12 10:52:03 +08:00
parent b11d1c94c1
commit bedc2009fc
22 changed files with 275 additions and 29 deletions

View File

@@ -249,7 +249,7 @@ bool LoginMission::onSocketSubLogonSuccess(void* data, int size)
pGlobalUserData->dwExperience =pData->dwExperience;
pGlobalUserData->cbInsureEnabled = pData->cbInsureEnabled;
pGlobalUserData->cbWXShareTimes = pData->cbWXShareTimes;
//strncpy(pGlobalUserData->szNickName, (char*)pData->szNickName, countarray(pGlobalUserData->szNickName)-1);
strncpy(pGlobalUserData->szNickName, (char*)pData->szNickName, countarray(pGlobalUserData->szNickName)-1);
strncpy(pGlobalUserData->szAccounts, ((char*)pData->szAccounts), countarray(pGlobalUserData->szAccounts)-1);
// 抽奖信息;

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);
@@ -43,6 +49,31 @@ bool LogonScene::init()
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();
//和回调函数绑定
@@ -130,22 +161,37 @@ void LogonScene::onGPLoginSuccess()
CGlobalUserInfo * pGlobalUserInfo=CGlobalUserInfo::GetInstance();
tagGlobalUserData * pGlobalUserData=pGlobalUserInfo->GetGlobalUserData();
//UserDefault::getInstance()->setStringForKey("Accounts",pGlobalUserData->szAccounts);
//UserDefault::getInstance()->setStringForKey("Password",m_kPssword);
m_kPssword = "WeiXinPassword";
if (pGlobalUserData != nullptr)
{
UserDefault::getInstance()->setStringForKey("Accounts", pGlobalUserData->szAccounts);
UserDefault::getInstance()->setStringForKey("Password", m_kPssword);
//UserDefault::getInstance()->setStringForKey("Accounts", pGlobalUserData->szAccounts);
//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
{
strcpy(pGlobalUserData->szPassword, m_kPssword.c_str());
// Username login: clear head URL to use default avatar
pGlobalUserData->szHeadHttp[0] = '\0';
}
}
}
@@ -339,4 +385,86 @@ 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(UserDefault::getInstance()->getStringForKey("Accounts", ""));
if (m_pTxtPassword) m_pTxtPassword->setString(UserDefault::getInstance()->getStringForKey("Password", ""));
}
}
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);
// 保存输入的 用户名和密码
UserDefault::getInstance()->setStringForKey("Accounts", strUsername);
UserDefault::getInstance()->setStringForKey("Password", strPassword);
// 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,7 +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 +52,22 @@ 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;
};