Files
wnmj/Classes/Scenes/PayScene.cpp
2026-02-13 14:34:15 +08:00

357 lines
8.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "PayScene.h"
#include "PopScene.h"
#include "Utility.h"
#include "Packet.h"
#include "GlobalUserInfo.h"
#include "WebReq.h"
#include "Platform.h"
#include "GlobalJosn.h"
PayScene::PayScene()
{
m_cbCurStep = 0;
m_txtTitle = nullptr;
m_txtNumber = nullptr;
m_txtGameID = nullptr;
m_txtNickName = nullptr;
m_txtCurScore = nullptr;
}
PayScene::~PayScene()
{
}
bool PayScene::init()
{
if (!Node::init())
{
return false;
};
auto rootPanel = CSLoader::createNode("Platform/PayScene.csb");
CC_ASSERT(rootPanel != nullptr);
this->addChild(rootPanel);
m_rootPanel = rootPanel->getChildByName("Image_bg");
CC_ASSERT(m_rootPanel != nullptr);
//获取关闭按钮;
auto btnClose = (Button*)m_rootPanel->getChildByName("btnClose");
CC_ASSERT(btnClose != nullptr);
//注册关闭按钮事件;
btnClose->addClickEventListener([this](Ref*){
YSAudioEngine::Instance().playBtnClickEffect();
//this->removeFromParent();
this->popScene();
});
//上一步;
m_btnPrev = (Button*)m_rootPanel->getChildByName("btn_Prev");
CC_ASSERT(m_btnPrev != nullptr);
//注册按钮事件;
m_btnPrev->addClickEventListener(CC_CALLBACK_1(PayScene::onPrevButtonClick, this));
m_btnPrev->setVisible(false);
//下一步;
m_btnNext = (Button*)m_rootPanel->getChildByName("btn_Next");
CC_ASSERT(m_btnNext != nullptr);
m_btnNext->setPositionX(m_rootPanel->getContentSize().width/2);
//注册按钮事件;
m_btnNext->addClickEventListener(CC_CALLBACK_1(PayScene::onNextButtonClick, this));
//标题;
m_txtTitle = (Text*)m_rootPanel->getChildByName("txtTitle");
CC_ASSERT(m_txtTitle != nullptr);
//输入数字;
m_txtNumber = (Text*)m_rootPanel->getChildByName("txtNum");
CC_ASSERT(m_txtNumber != nullptr);
m_txtNumber->setString("");
//游戏ID;
m_txtGameID = (Text*)m_rootPanel->getChildByName("txtGameID");
CC_ASSERT(m_txtGameID != nullptr);
m_txtGameID->setVisible(false);
//昵称;
m_txtNickName = (Text*)m_rootPanel->getChildByName("txtNickName");
CC_ASSERT(m_txtNickName != nullptr);
m_txtNickName->setVisible(false);
//房卡;
m_txtCurScore = (Text*)m_rootPanel->getChildByName("txtCurScore");
CC_ASSERT(m_txtCurScore != nullptr);
m_txtCurScore->setVisible(false);
for (int j = 0; j < 10; j++)
{
std::string strKey = __String::createWithFormat("btn_num_%d", j)->getCString();
auto btnNum = (Button*)m_rootPanel->getChildByName(strKey);
CC_ASSERT(btnNum != nullptr);
btnNum->addClickEventListener([=](Ref* obj){
YSAudioEngine::Instance().playBtnClickEffect();
std::string strText = m_txtNumber->getString();
strText += StringUtils::format("%d", j);
m_txtNumber->setString(strText);
});
}
// 删除
auto btnDel = (Button*)m_rootPanel->getChildByName("btn_del");
CC_ASSERT(btnDel != nullptr);
btnDel->addClickEventListener([this](Ref* obj){
YSAudioEngine::Instance().playBtnClickEffect();
std::string strText = m_txtNumber->getString();
if (strText.length()>0)
{
strText.erase(strText.length() - 1, 1);
m_txtNumber->setString(strText);
}
});
// 清空
auto btnC = (Button*)m_rootPanel->getChildByName("btn_C");
CC_ASSERT(btnC != nullptr);
btnC->addClickEventListener([this](Ref* pSender){
YSAudioEngine::Instance().playBtnClickEffect();
m_txtNumber->setString("");
});
return true;
}
void PayScene::onEnter()
{
Node::onEnter();
}
void PayScene::onEnterTransitionDidFinish()
{
Node::onEnterTransitionDidFinish();
}
void PayScene::onExit()
{
Node::onExit();
}
void PayScene::onPrevButtonClick(Ref*)
{
if (1 == m_cbCurStep)
{
m_cbCurStep--;
// 更改标题;
m_txtTitle->setString(utility::a_u8("请输入玩家ID"));
// 隐藏下一步按钮;
m_btnPrev->setVisible(false);
m_btnNext->setPositionX(m_rootPanel->getContentSize().width / 2);
m_strScore = m_txtNumber->getString();
m_txtNumber->setString(m_strGameID);
// 隐藏玩家游戏ID;
m_txtGameID->setVisible(false);
// 隐藏玩家昵称;
m_txtNickName->setVisible(false);
// 隐藏剩余房卡;
m_txtCurScore->setVisible(false);
}
}
void PayScene::onNextButtonClick(Ref*)
{
std::string strText = m_txtNumber->getString();
if (0==m_cbCurStep)
{
if (strText.length()<6)
{
PopScene::Instance().show(utility::a_u8("请填入正确的游戏ID"));
return ;
}
LoadingScene::Instance().show();
m_strGameID = strText;
std::string strUrl = StringUtils::format("%s?gid=%s", GET_NICKNAME_HTTP_ADDRESS, strText.c_str());
CWebReq::getInstance().sendRequestDocumentUrl(strUrl, CC_CALLBACK_1(PayScene::responsePlayerNickName, this), nullptr);
}
else if (1 == m_cbCurStep)
{
strText = m_txtNumber->getString();
if (strText.length() < 1)
{
PopScene::Instance().show(utility::a_u8("请输入房卡数量"));
return;
}
if (atoi(strText.c_str())<=0)
{
PopScene::Instance().show(utility::a_u8("房卡数量输入错误,请重新输入!"));
return;
}
m_strScore = strText;
LoadingScene::Instance().show();
CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance();
tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData();
// PAY_TO_PLAYER_HTTP_ADDRESS
std::string strData = StringUtils::format("%d&%s&%d&%d", pGlobalUserData->dwUserID, pGlobalUserData->szAccounts, atoi(m_strGameID.c_str()), atoi(m_strScore.c_str()));
std::string strResult;
encryptData(strData, strResult);
std::string strUrl = StringUtils::format("%s?key=%s", GlobalJosn::getInstance()->m_strPayUrl.c_str(), strResult.c_str());
CWebReq::getInstance().sendRequestDocumentUrl(strUrl, CC_CALLBACK_1(PayScene::responsePayResult, this), nullptr);
}
}
//请求玩家昵称
void PayScene::responsePlayerNickName(rapidjson::Document* pDoc)
{
LoadingScene::Instance().hide();
if (pDoc == nullptr)
{
PopScene::Instance().show(utility::a_u8("网络错误"));
return;
}
int bSuccess = CWebReq::getDataValueInt(pDoc, "ret");
if (bSuccess==1)
{
m_cbCurStep++;
// 更改标题;
m_txtTitle->setString(utility::a_u8("请输入充卡数量"));
// 显示下一步按钮;
m_btnPrev->setVisible(true);
int nStartPosX = m_rootPanel->getContentSize().width / 2;
m_btnPrev->setPositionX(nStartPosX - m_btnPrev->getContentSize().width / 2 - 50);
m_btnNext->setPositionX(nStartPosX + m_btnNext->getContentSize().width / 2 + 50);
m_txtNumber->setString(m_strScore);
// 显示玩家游戏ID;
m_txtGameID->setVisible(true);
// 显示玩家昵称;
m_txtNickName->setVisible(true);
// 显示剩余房卡;
m_txtCurScore->setVisible(true);
int nGameID = CWebReq::getDataValueInt(pDoc, "GameID");
std::string strNickName = CWebReq::getDataValueStr(pDoc, "NickName");
int nCurScore = CWebReq::getDataValueInt(pDoc, "CardNum");
m_txtGameID->setString(utility::a_u8("游戏ID: ") + StringUtils::format("%d", nGameID));
m_txtNickName->setString(utility::a_u8("玩家昵称: ") + utility::getShortName(strNickName, 16) + StringUtils::format(" %d", nCurScore) + utility::a_u8(""));
//m_txtCurScore->setString(StringUtils::format("%d", nCurScore) + utility::a_u8("张"));
m_txtCurScore->setString("");
}
else
{
PopScene::Instance().show(utility::a_u8("玩家游戏ID不存在请重新输入"));
}
}
//请求信息结果
void PayScene::responsePayResult(rapidjson::Document* pDoc)
{
LoadingScene::Instance().hide();
if (pDoc == nullptr)
{
PopScene::Instance().show(utility::a_u8("网络错误"));
return;
}
int bSuccess = CWebReq::getDataValueInt(pDoc, "ret");
if (bSuccess == 1)
{
PopScene::Instance().show(utility::a_u8("充值成功!"));
m_txtNumber->setString("");
m_strGameID = "";
onPrevButtonClick(nullptr);
int nCurNum = CWebReq::getDataValueInt(pDoc, "CurNum");
CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance();
tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData();
pGlobalUserData->lUserInsure = nCurNum;
auto dispatcher = Director::getInstance()->getEventDispatcher();
EventCustom event(UPDATE_USER_INSURE);
//派发事件至MainScene
dispatcher->dispatchEvent(&event);
}
else
{
std::string strReason = CWebReq::getDataValueStr(pDoc, "Reason");
PopScene::Instance().show(utility::a_u8("充值失败:") + strReason);
}
}
//生成密文
bool PayScene::mapEncrypt(const char * pszSourceData, char * pszEncrypData, uint16 wMaxCount)
{
//效验参数
CC_ASSERT((pszEncrypData != NULL) && (pszSourceData != NULL));
CC_ASSERT(wMaxCount > strlen(pszSourceData));
//变量定义
size_t nLength = strlen(pszSourceData);
uint8 * pcbEncrypData = (uint8 *)pszEncrypData;
uint8 * pcbSourceData = (uint8 *)pszSourceData;
//解密数据
for (uint32 i = 0, n = nLength*sizeof(uint8); i < n; i++)
{
pcbEncrypData[i] = g_SendByteMap[pcbSourceData[i]];
}
//设置结果
pszEncrypData[nLength] = 0;
return true;
}
void PayScene::encryptData(std::string stData, std::string& strOut)
{
size_t nLength = stData.size();
char* pBuffer = new char[nLength + 1];
mapEncrypt(stData.c_str(), pBuffer, nLength + 1);
for (int i = 0, n = nLength; i < n; i++)
{
strOut += StringUtils::format("%02X", (uint8)pBuffer[i]);
}
}