这里主要用到两个文件的处理,一个是登录界面的初始化,另外一个便是登录逻辑的处理,附上lua源码。
local LoginManager = require("app.Manager.LoginManager");
require "app.Common.CommonDefine"
--创建登录场景
local LoginScene = class("LoginScene", function()
return cc.Scene:create();
end)
LoginScene.__index = LoginScene;
--定义变量
LoginScene._isSelected = true;
LoginScene._rAisSelected = true; --记住密码
LoginScene._isClickLoginBtn = false;
--创建场景
function LoginScene:create()
--清除缓存,重新加载
cc.Director:getInstance():getTextureCache():removeAllTextures();
cc.SpriteFrameCache:getInstance():removeSpriteFrames();
local scene = LoginScene.new();
scene:setName("SCMJ_LoginScene");
--初始化变量
scene._isSelected = true;
scene._rAisSelected = true; --记住密码
scene._isClickLoginBtn = false;
--加载场景
scene._rootLayer = scene:InitScene();
scene:addChild(scene._rootLayer);
--注册场景节点事件
scene:registerScriptHandler(handler(scene, scene.onNodeEvent));
return scene;
end
------------------------------------------------
--场景节点事件处理
function LoginScene:onNodeEvent(event)
if event == "enter" then
require_ex("app.Net.OpCode");
require_ex("app.Net.MessageRegister");
require_ex("cocos.cocos2d.bitExtend");
MessageRegister_DealFunc();
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform ~= cc.PLATFORM_OS_WINDOWS then
local wxToken = GetUserDataString(USER_DATA_STRING_WX_TOKEN);
local AYopenId = GetUserDataString(USER_DATA_STRING_AY_USERID);
-- 没有获得过授权,不会执行自动登录操作
if string.len(wxToken) == 0 and string.len(AYopenId) == 0 then
else
self:ServerDetection();
end
end
end
end
------------------------------------------------
------------------------------------------------
--按钮的触摸
function LoginScene:BtnTouchCallBack(sender,eventType)
if eventType == ccui.TouchEventType.began then
--播放按钮音效
ButtonSE();
end
end
------------------------------------------------
--登录按钮的实现
function LoginScene:LoginBtnCallback (sender)
self:ServerDetection();
end
--协议关闭按钮的实现
function LoginScene:protocolCloseBtnFunc(sender)
local protocolNode = self._rootLayer:getChildByName("Panel_protocol")
if protocolNode ~= nil then
--protocolNode:setVisible(false)
ViewCloseAnimation(protocolNode, function() protocolNode:setVisible(false) end);
end
end
--同意协议的复选框的实现
function LoginScene:CheckBoxCallBack(sender, eventType)
if eventType == CHECKBOX_STATE_EVENT_SELECTED then
self._isSelected = true;
elseif eventType == CHECKBOX_STATE_EVENT_UNSELECTED then
self._isSelected = false;
end
end
--同意记住账号的复选框的实现
function LoginScene:rCheckBoxCallBack(sender, eventType)
if eventType == CHECKBOX_STATE_EVENT_SELECTED then
self._rAisSelected = true;
elseif eventType == CHECKBOX_STATE_EVENT_UNSELECTED then
self._rAisSelected = false;
end
end
--协议按钮的实现
function LoginScene:WordBtnCallFun(sender)
--显示协议
local protocolNode = self._rootLayer:getChildByName("Panel_protocol");
protocolNode:setVisible(true);
ViewPopAnimation(protocolNode);
end
function LoginScene:AccountLoginBtnCallFunc(sender)
AYorWXchoiced = 0;
local layer = sender:getParent();
local account = layer:getChildByName("Account_Input");
local password = layer:getChildByName("Account_Password");
local realInput = account:getChildByName("RealInput");
local instance=cc.UserDefault:getInstance()
local strPassWord = password:getString();
local strAccount = realInput:getText();
g_tableUserData.sAcctName = strAccount;
g_tableUserData.sPassWord = strPassWord;
if self._rAisSelected then
print(self._rAisSelected)
print(self._rAisSelected)
local account=instance:setStringForKey("Account",strAccount)
else
local account=instance:setStringForKey("Account",nil)
end
LoginManager:ConnectLoginServer();
end
------------------------------------------------------------------
--加载场景
function LoginScene:InitScene()
local loginLayer = cc.CSLoader:createNode("LoginScene.csb");
--登录按钮的实现
local function loginBtnFunc(sender)
--获取按钮名
local btnName = sender:getName();
if btnName == "Button_login" then
-- 清除爱约登录的信息
SetUserDataString(USER_DATA_STRING_AY_USERID, "");
AYorWXchoiced = 1;
elseif btnName == "Button_login_quick" then
-- 清除爱约登录的信息
SetUserDataString(USER_DATA_STRING_AY_USERID, "");
AYorWXchoiced = 0;
elseif btnName == "Button_login_AY" then
--未安装爱约 点击爱约跳转至下载页面
if AYAppInstalled == false and btnName == "Button_login_AY" then
local strUrl;
if g_DictTable[DictTable_AiYue_Download_Url] then
strUrl = g_DictTable[DictTable_AiYue_Download_Url]["Content"];
end
cc.Application:getInstance():openURL(strUrl);
return;
else
-- 清除微信登录的信息
SetUserDataString(USER_DATA_STRING_WX_REFRESH_TOKEN, "");
SetUserDataString(USER_DATA_STRING_WX_OPENID, "");
SetUserDataString(USER_DATA_STRING_WX_TOKEN, "");
AYorWXchoiced = 2;
end
end
self._isClickLoginBtn = true;
g_tablePlayerData = {} --清除玩家数据
LoginManager:ClearUserData() --清除玩家数据
self:LoginBtnCallback(sender);
end
--获取登录按钮
local loginBtn = loginLayer:getChildByName("Button_login");
if loginBtn ~= nil then
loginBtn:addClickEventListener(loginBtnFunc);
loginBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
end
--获取快速登录按钮
local loginQuickBtn = loginLayer:getChildByName("Button_login_quick");
if loginQuickBtn ~= nil then
loginQuickBtn:addClickEventListener(loginBtnFunc);
loginQuickBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
end
--获取爱约登录按钮
local aiYueBtn = loginLayer:getChildByName("Button_login_AY");
if aiYueBtn ~= nil then
aiYueBtn:addClickEventListener(loginBtnFunc);
aiYueBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
end
local function keFuCall()
cc.Application:getInstance():openURL(CustomServiceUrl);
end
--获取客服按钮
local keFuBtn = loginLayer:getChildByName("LoginScene_Panel_Button_Agent");
if keFuBtn ~= nil then
keFuBtn:addClickEventListener(keFuCall);
keFuBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
if wxAppInsTALL == false then
keFuBtn:setVisible(false);
end
end
end
if wxAppInsTALL == false and AYAppInstalled == false then
--如果微信与爱约吧都没有安装,则快速登录居中显示
aiYueBtn:setVisible(false);
loginQuickBtn:setPositionX(640);
loginBtn:setVisible(false);
elseif wxAppInsTALL == true and AYAppInstalled == false then
--如果只安装了微信,爱约和微信都显示
loginQuickBtn:setVisible(false);
elseif wxAppInsTALL == false and AYAppInstalled == true then
--如果只安装了爱约吧,则爱约吧居中显示
aiYueBtn:setPositionX(640);
loginQuickBtn:setVisible(false);
loginBtn:setVisible(false);
elseif wxAppInsTALL == true and AYAppInstalled == true then
--如果两个都安装了,则隐藏快速登录按钮
loginQuickBtn:setVisible(false);
end
local CheckBox2=loginLayer:getChildByName("CheckBox_2");
CheckBox2:setVisible(false);
local rAisSelectText=loginLayer:getChildByName("Text_Acount_remember");
rAisSelectText:setVisible(false);
--获取协议
local protocolPanel = loginLayer:getChildByName("Panel_protocol");
protocolPanel:setVisible(false);
local protocolNode = protocolPanel:getChildByName("protocol_kuang");
if protocolNode ~= nil then
--关闭按钮的实现
local function closeBtnFunc(sender)
self:protocolCloseBtnFunc(sender);
end
--关闭按钮
local closeBtn = protocolNode:getChildByName("Button_close");
if closeBtn ~= nil then
closeBtn:addClickEventListener(closeBtnFunc);
closeBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
end
--设置协议内容
local pScrollView = protocolNode:getChildByName("ScrollView_protocol");
if pScrollView ~= nil then
--设置滚动区域大小
local scrollSize = cc.size(1020,2500);
pScrollView:setInnerContainerSize(scrollSize);
--显示协议内容
local protocolText = pScrollView:getChildByName("Text_protocol");
if protocolText then
local wordString = cc.FileUtils:getInstance():getStringFromFile("Config/LoginSceneProtocol.txt");
protocolText:setString(wordString);
protocolText:setPosition(0,scrollSize.height);
end
end
end
--复选框的实现
local function checkBoxFunc(sender, eventType)
self:CheckBoxCallBack(sender, eventType);
end
local function rcheckBoxFunc(sender,eventType)
self:rCheckBoxCallBack(sender, eventType);
end
--获取复选框
local pCheckBox = loginLayer:getChildByName("CheckBox_1");
if pCheckBox then
pCheckBox:setSelected(self._isSelected);
pCheckBox:addEventListener(checkBoxFunc);
end
--获取记住账号复选框
local rAcountCheckBox=loginLayer:getChildByName("CheckBox_2");
if rAcountCheckBox then
rAcountCheckBox:setSelected(self._rAisSelected)
rAcountCheckBox:addEventListener(rcheckBoxFunc);
end
--同意协议按钮的实现
local function wordBtnFunc(sender)
self:WordBtnCallFun(sender);
end
local function accountLoginFunc(sender)
self:AccountLoginBtnCallFunc(sender);
end
--获取“同意协议”的按钮
local agreeWordBtn = loginLayer:getChildByName("Button_protocol_word");
if agreeWordBtn then
agreeWordBtn:addClickEventListener(wordBtnFunc);
agreeWordBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
end
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_WINDOWS then
local account = loginLayer:getChildByName("Account_Input");
local passWord = loginLayer:getChildByName("Account_Password");
local accountLoginBtn = loginLayer:getChildByName("Account_Login");
account:setVisible(true);
passWord:setVisible(true);
accountLoginBtn:setVisible(true);
accountLoginBtn:addClickEventListener(accountLoginFunc);
accountLoginBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
--从本地读取上次记住的帐号
local strAccount = cc.UserDefault:getInstance():getStringForKey("Account")
local boxContent = account:getContentSize();
local accountEditBox = ccui.EditBox:create(boxContent, "");
accountEditBox:setInputMode(cc.EDITBOX_INPUT_MODE_ANY);
accountEditBox:setInputFlag(cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_WORD);
accountEditBox:setAnchorPoint(ccp(0.5,0.5));
accountEditBox:setPosition(cc.p(150,20));
accountEditBox:setFontSize(48);
accountEditBox:setFontColor(cc.c3b(255,255,0));
accountEditBox:setPlaceholderFontColor(cc.c3b(214,246,255));
accountEditBox:setText(strAccount);
accountEditBox:registerScriptEditBoxHandler(handler(self, self.EditBoxEvent));
account:addChild(accountEditBox);
accountEditBox:setName("RealInput");
CheckBox2:setVisible(true)
rAisSelectText:setVisible(true)
end
--获取文网文隐藏
local textWangWen = loginLayer:getChildByName("Text_WangWen");
textWangWen:setVisible(false);
local returnBtnLayer = CommonFunc_return_key();
loginLayer:addChild(returnBtnLayer);
return loginLayer;
end
--输入框响应
function LoginScene:EditBoxEvent(strEventName,pSender)
if strEventName == "began" then
elseif strEventName == "ended" then
elseif strEventName == "return" then
elseif strEventName == "changed" then
end
end
function LoginScene:AutoLogin()
--同意协议才能登录
if self._isSelected then
local function timeoutFunc()
-- if GetConnectState() == NET_CONNECT_STATE_DOING then
-- AddLoadingLayer(8, timeoutFunc);
-- return;
-- end
end
if self._isClickLoginBtn == false then
local wxToken = GetUserDataString(USER_DATA_STRING_WX_TOKEN);
local AYopenId = GetUserDataString(USER_DATA_STRING_AY_USERID);
--判断登录类型
if wxAppInsTALL and string.len(wxToken) ~= 0 then
-- 微信登录
AYorWXchoiced = 1;
elseif AYAppInstalled and string.len(AYopenId) ~= 0 then
-- 爱约登录
AYorWXchoiced = 2;
else
-- 快速登录
AYorWXchoiced = 0;
end
end
LoginManager:ConnectLoginServer(timeoutFunc);
else
--不同意协议,弹出提示框
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE, ErrorCode_Agree_Protocol);
self._rootLayer:addChild(tipsLayer,1000);
end
end
local serverStateDir = "http://120.77.156.221:8008/serverState.txt";
local localServerStateDir = "cachePath/serverState.txt";
function LoginScene:ServerDetection()
AddLoadingLayer(10);
local webFile = CWebFile:Create(serverStateDir, "cachePath");
webFile:SetHandler(handler(self, self.GetServerStateInfo));
end
function LoginScene:GetServerStateInfo()
RemoveLoadingLayer();
local data = cc.FileUtils:getInstance():getStringFromFile(localServerStateDir);
if data == "1" then
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE, RrrorCode_ServerMaintenancing);
cc.Director:getInstance():getRunningScene():addChild(tipsLayer,1000);
else
self:AutoLogin();
end
end
return LoginScene;
---------------------------下面是逻辑的处理----------------------------------
require ("app.Net.OpCode")
g_tableUserData =
{
nUserId1 = 0,
nUserId2 = 0,
nSessionNumber,
sAcctName,
sPassWord,
sGameServerIp,
sGameServerPort,
nServerId = 0,
bGuest = false,
bFirstSelectGoldRoom = true,
}
local LoginManager = class("LoginManager")
--sLoginIp = "192.168.2.58";
--sLoginIp = "192.168.2.38";
--------------local sLoginIp = "192.168.2.149";
--local sLoginPort = "9121";
--local sLoginIp = "192.168.2.37";
--local sLoginPort = "9090";
--local sLoginIp = "120.76.228.214"; --外网
--local sLoginPort = "19191";
--local sLoginIp = "120.25.106.227"; --外网测试
--local sLoginPort = "19191";
--local sLoginIp = "192.168.1.125";
--local sLoginPort = "17272";
--外网正式服
--local sLoginSucc = DnsParse("wmfjmj01.18083.com");
--local sLoginIp = "39.108.224.20" --外网测试服
--if sLoginSucc ~= false then
-- sLoginIp = sLoginSucc
--end
local sLoginIp = "192.168.2.231"
--local sLoginPort = "9090";
--local sLoginIp = "39.108.224.20" --外网测试服
--local sLoginPort = "20921";
--local sLoginIp = "39.108.116.153" --外网测试服
local sLoginPort = "9090";
--登录服链接回调
function LoginManager_ConnectLoginResult(bRst)
if bRst then
--链接成功
printf("ConnectLoginServer_Succeed");
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_WINDOWS then
if g_tableUserData.sAcctName == nil then
LoginManager_SendQuickLoginEx();
else
local accountLen = string.len(g_tableUserData.sAcctName);
if accountLen > 0 then
LoginManager:SendAccountAuth(g_tableUserData.sAcctName,g_tableUserData.sPassWord);
else
LoginManager_SendQuickLoginEx();
end
end
else
if g_tableUserData.sAcctName == nil then
if AYorWXchoiced ~= 2 then
WxAuth();
else
AYAuth();
end
else
local accountLen = string.len(g_tableUserData.sAcctName);
if accountLen > 0 then
LoginManager:SendAccountAuth(g_tableUserData.sAcctName,g_tableUserData.sPassWord);
end
end
end
else
--链接失败
printf("ConnectLoginServerResult_Faild");
RemoveLoadingLayer();
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE,ErrorCode_Connect_LoginServer_Fail);
--获取当前运行的scene
local runningScene = cc.Director:getInstance():getRunningScene();
local sceneName = runningScene:getName();
--不在登录界面需要切换到登录界面
if sceneName == "SCMJ_LoginScene" then
cc.Director:getInstance():getRunningScene():addChild(tipsLayer,1000);
else
LoginManager:ReturnToLogin();
end
end
end
--游戏服链接回调
function LoginManager_ConnectGameServerResult(bRst)
if bRst then
--链接成功
printf("ConnectGameServerResult_Succeed");
LoginManager:SendAuthToGame();
else
--链接失败
printf("ConnectGameServerResult_Faild");
printf("ConnectLoginServerResult_Faild");
RemoveLoadingLayer();
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE,ErrorCode_Connect_GameServer_Fail);
cc.Director:getInstance():getRunningScene():addChild(tipsLayer,1000);
end
end
--通知进行微信登录操作
function LoginManager_NotifyWxAuth(sAccessToken,sOpenId)
if sOpenId == nil or sOpenId == "" then
SetUserData(USER_DATA_STRING_WX_TOKEN, "");
SetUserData(USER_DATA_STRING_WX_OPENID, "");
SetUserData(USER_DATA_STRING_WX_REFRESH_TOKEN, "");
WxAuth();
else
local sTempOpenId = sOpenId;
if AYorWXchoiced == 2 then
sTempOpenId = GetUserDataString(USER_DATA_STRING_AY_USERID);
end
local nChannel = 0;
if GetGameChannel then
nChannel = GetGameChannel()
end
local buffer = Buffer:new()
buffer:WriteString(sAccessToken);
buffer:WriteString(sTempOpenId);
buffer:WriteInt(nChannel);
SendTcpMessage(CL_PACKET_WX_AUTH,buffer:GetBuffer(), buffer:GetSize())
end
end
--设置用户信息
function LoginManager:SetUserInfo(nUserId1,nUserId2,nSessionNumber,sAcctName,sPassWord)
g_tableUserData.nUserId1 = nUserId1;
g_tableUserData.nUserId2 = nUserId2;
g_tableUserData.nSessionNumber = nSessionNumber;
g_tableUserData.sAcctName = sAcctName;
g_tableUserData.sPassWord = sPassWord;
end
--设置游戏服信息
function LoginManager:SetGameServerInfo(sGameServerIp,sGameServerPort,nServerId)
g_tableUserData.sGameServerIp = sGameServerIp;
g_tableUserData.sGameServerPort = sGameServerPort;
g_tableUserData.nServerId = nServerId;
end
--获取用户id
function LoginManager:GetUserId()
return g_tableUserData[UserId1],tableUserData[UserId2];
end
--获取账号
function LoginManager:GetAcctName()
return g_tableUserData[sAcctName];
end
--获取登录流水号
function LoginManager:GetSessionNumber()
return g_tableUserData[SessionNumber];
end
--一键登录
function LoginManager:SendQuickLogin()
local buffer = Buffer:new()
--UserName
buffer:WriteString("");
--PassWord
buffer:WriteString("");
--UserType
buffer:WriteInt(0);
SendTcpMessage(CL_PACKET_LOGIN,buffer:GetBuffer(), buffer:GetSize())
end
--一键登录,如果是手机根据UUID登录
function LoginManager_SendQuickLoginEx()
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_WINDOWS then
LoginManager:SendQuickLogin();
else
local strAccount = GetDeviceID();
g_tableUserData.bGuest = true;
if strAccount == nil or strAccount == "" then
print("GetDeviceUUID() error, strAccount is NULL");
return;
end
LoginManager_NotifyWxAuth("", strAccount);
end
end
function LoginManager_SendAccountAuth(strAccount,strPassword)
LoginManager:SendAccountAuth(strAccount,strPassword);
end
function LoginManager:SendAccountAuth(strAccount,strPassword)
local buffer = Buffer:new()
--UserName
buffer:WriteString(strAccount);
--PassWord
buffer:WriteString(strPassword);
--UserType
buffer:WriteInt(1);
SendTcpMessage(CL_PACKET_LOGIN,buffer:GetBuffer(), buffer:GetSize())
end
--链接登录服
function LoginManager:ConnectLoginServer(timeoutFunc)
local function Login()
--同意协议,发送登录信息
--CloseConnect();
RegisterConnectCallback("LoginManager_ConnectLoginResult");
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_IPAD or targetPlatform == cc.PLATFORM_OS_IPHONE then
if wxAppInsTALL == false then
sLoginIp = "39.108.224.20";
end
print("sLoginIp", sLoginIp)
end
ConnectServerWithIPAndPort(sLoginIp,sLoginPort);
--显示加载层,超时运行处理函数
AddLoadingLayer(8, timeoutFunc);
end
--本地不更新,lua就不检测文件lua版本
if g_bLoadingUdate == false then
Login();
return;
end
--lua文件版本更新检查
if LuaVersionUrl and LuaVersion then --检查路径,不存在就登出重新下载lua文件
local function callback(pData)
print(pData.nStatusCode, pData["ConpletePathName"]);
RemoveLoadingLayer();
--下载失败
if pData.nStatusCode ~= 200 then
--失败
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE, RrrorCode_RoomGetServerConnectStateFailed, "", LoginManager.ReturnToLogin);
cc.Director:getInstance():getRunningScene():addChild(tipsLayer, 1000);
return;
end
local filePath = pData["ConpletePathName"];
local nVersion = tonumber(cc.FileUtils:getInstance():getStringFromFile(filePath));
--读取失败
if nVersion == nil then
--失败
local pLayer = require("app.views.TipsLayer");
local tipsLayer = pLayer:createTipsLayer(TIPS_LAYER_ONLY_SURE, ErrorCode_Fail_CheckVersionFailed, "", LoginManager.ReturnToLogin);
cc.Director:getInstance():getRunningScene():addChild(tipsLayer, 1000);
return;
end
if nVersion == LuaVersion then
Login();
else
LoginManager:LoginOut();
end
end
AddLoadingLayer();
local webFile = CWebFile:Create(LuaVersionUrl, "cachePath");
webFile:SetHandler(callback);
else
LoginManager:LoginOut();
end
end
--链接游戏服
function LoginManager:ConnectGameServer()
printf("ConnectGameServer");
RegisterConnectCallback("LoginManager_ConnectGameServerResult")
printf("ServerIp:%s sPort:%s",g_tableUserData.sGameServerIp,g_tableUserData.sGameServerPort)
ConnectServerWithIPAndPort(g_tableUserData.sGameServerIp,g_tableUserData.sGameServerPort);
end
--发送游戏服登录请求
function LoginManager:SendAuthToGame()
local buffer = Buffer:new();
buffer:WriteString(g_tableUserData.sAcctName);
buffer:WriteInt(g_tableUserData.nSessionNumber);
buffer:WriteInt(g_tableUserData.nServerId);
SendTcpMessage(CGT_PACKET_AUTH,buffer:GetBuffer(), buffer:GetSize())
end
function LoginManager:ClearUserData()
g_tableUserData.nUserId1 = 0;
g_tableUserData.nUserId2 = 0;
g_tableUserData.nSessionNumber = 0;
g_tableUserData.sAcctName = nil;
g_tableUserData.sPassWord = nil;
g_tableUserData.sGameServerIp = "";
g_tableUserData.sGameServerPort = "";
g_tableUserData.nServerId = 0;
g_tableUserData.bGuest = false;
end
--断线后连接服务器的回调
function LoginManager_ReconnectGameServerResult(bRst)
if bRst then
--链接成功,发送重连消息
printf("ReConnectGameServerResult_Succeed");
local buffer = Buffer:new();
buffer:WriteInt(g_tableUserData.nUserId2);
buffer:WriteInt(g_tableUserData.nUserId1);
buffer:WriteInt(g_tableUserData.nSessionNumber);
buffer:WriteInt(g_tableUserData.nServerId);
SendTcpMessage(CTG_PACKET_RECONNECT,buffer:GetBuffer(), buffer:GetSize())
else
--链接失败
printf("ReConnectGameServerResult_Faild");
--执行动作
local action1 = cc.DelayTime:create(0.1);
local action2 = cc.CallFunc:create(function()
RemoveLoadingLayer();
LoginManager:ReturnToLogin();
end);
display.getRunningScene():runAction(cc.Sequence:create(action1,action2));
end
end
--断线后先连接服务器
function LoginManager:ReConnectGameServer()
if GetConnectState() == NET_CONNECT_STATE_DOING then return; end --已经在连接就不重新连接了
RegisterConnectCallback("LoginManager_ReconnectGameServerResult");
ConnectServerWithIPAndPort(g_tableUserData.sGameServerIp,g_tableUserData.sGameServerPort);
end
function LoginManager:SendUpdateUserInfo()
if AYorWXchoiced == 2 then
AYGetUserInfo();
else
WxGetUserInfo();
end
end
function LoginManager_SendUpdateUserInfo(sHeadUrl,sNickName,nSex)
local buffer = Buffer:new();
buffer:WriteString(sHeadUrl);
buffer:WriteString(sNickName);
buffer:WriteInt(nSex);
g_tablePlayerData.sHeadUrl = sHeadUrl;
g_tablePlayerData.sName = sNickName;
g_tablePlayerData.nSex = nSex;
--获取当前运行的scene
local runningScene = cc.Director:getInstance():getRunningScene();
local sceneName = runningScene:getName();
--更新用户头像昵称
if sceneName == SCENE_NAME_TEXAS_GAMESCENE or sceneName == "SCMJ_MainScene" then
cc.Director:getInstance():getRunningScene():UpdateUserInfo();
end
SendTcpMessage(CG_PACKET_UPDATE_USER_INFO,buffer:GetBuffer(), buffer:GetSize());
end
--返回登录界面
function LoginManager:LoginOut()
--退出登录
--CloseConnect();
LoginManager:ReturnToLogin();
end
function LoginManager:ReLogin()
--退出登录
local RoomDataManager = require "app.Manager.RoomDataManager";
RoomDataManager:ResetRoomData();
LoginManager:ClearUserData();
CloseConnect();
local function timeoutFunc()
if GetConnectState() == NET_CONNECT_STATE_DOING then
AddLoadingLayer(8, timeoutFunc);
return;
end
LoginManager:ReturnToLogin();
end
--连接服务器
LoginManager:ConnectLoginServer(timeoutFunc);
end
function LoginManager:ReturnToLogin()
local RoomDataManager = require "app.Manager.RoomDataManager";
RoomDataManager:ResetRoomData();
LoginManager:ClearUserData();
nReconnectCount = 0; --重置重连状态
compare_num = 0;
StopBGM();
require_ex("main.lua");
end
return LoginManager;
------------------------------服务器回复登录的信息-------------------------------
-- 回应登录请求
function MessageRegister_HandleLogin(buf, size)
printf("MessageRegister_HandleLogin");
local LoginManager = require("app.Manager.LoginManager")
local buffer = Buffer:new();
buffer:WriteBuffer(buf, size);
-- 64位UserId
local nUserId2 = buffer:ReadInt();
local nUserId1 = buffer:ReadInt();
-- 流水号
local nSessionNumber = buffer:ReadInt();
-- 账号
local sAccount = buffer:ReadString();
-- 密码
local sPassWord = buffer:ReadString();
-- 账号模式
local nAccountMode = buffer:ReadInt();
LoginManager:SetUserInfo(nUserId1, nUserId2, nSessionNumber, sAccount, sPassWord);
local targetPlatform = cc.Application:getInstance():getTargetPlatform();
if targetPlatform == cc.PLATFORM_OS_ANDROID or targetPlatform == cc.PLATFORM_OS_IPAD or targetPlatform == cc.PLATFORM_OS_IPHONE then
WXVoiceLogin(UserIdToString(nUserId1, nUserId2));
end
end
-- 设置秘钥
function MessageRegister_HandleAuthKey(buf, size)
printf("MessageRegister_HandleAuthKey");
local buffer = Buffer:new();
buffer:WriteBuffer(buf, size);
local bUseKey = buffer:ReadBool();
local nKey = buffer:ReadInt();
if bUseKey then
SetPassKey(nKey, g_tableUserData.nSessionNumber, g_tableUserData.sAcctName);
end
end
function MessageRegister_HandleReKey(buf, size)
printf("MessageRegister_HandleReKey");
local buffer = Buffer:new();
buffer:WriteBuffer(buf, size);
local sKey = buffer:ReadString();
ResetPassKey(sKey);
end
-- 服务器信息
function MessageRegister_HandleServerInfo(buf, size)
printf("MessageRegister_HandleServerInfo");
local LoginManager = require("app.Manager.LoginManager")
local buffer = Buffer:new();
buffer:WriteBuffer(buf, size);
local sHost = buffer:ReadString();
local nPort = buffer:ReadInt();
local sPort = IntToString(nPort);
local nServerId = buffer:ReadInt();
local sLoginSucc = DnsParse(sHost);
local sIp = ""
if sLoginSucc ~= false then
sIp = sLoginSucc;
else
sIp = sLoginIp
end
printf("ServerIp:%s sPort:%s nServerId:%d", sIp, sPort, nServerId);
LoginManager:SetGameServerInfo(sIp, sPort, nServerId);
-- CloseConnect();
LoginManager:ConnectGameServer();
end
-- 用户初始化信息(登录成功后)
function MessageRegister_HandleEnterGame(buf, size)
printf("MessageRegister_HandleEnterGame");
local buffer = Buffer:new();
buffer:WriteBuffer(buf, size);
-- 昵称
local sName = buffer:ReadString();
-- 返回为空,需要下面去获取
-- 头像链接
local sHeadUrl = buffer:ReadString();
-- 剩余房卡
local nRoomCard = buffer:ReadInt();
-- 是否有房间
local bHaveRoom = buffer:ReadBool();
-- 分享总次数
local nShareTimes = buffer:ReadInt();
-- ip
local sIp = buffer:ReadString();
-- 性别
local nSex = buffer:ReadInt();
-- 手机号
local sPhoneNum = buffer:ReadString();
-- 真实姓名
local sRealName = buffer:ReadString();
-- 金币
local nGoldCount = buffer:ReadUInt64();
-- 奖券
local nTicketCount = buffer:ReadUInt64();
local PlayerDataManager = require("app.Manager.PlayerDataManager");
PlayerDataManager:SetPlayerInfo(sName, sHeadUrl, nRoomCard, nShareTimes, sIp, nSex, sPhoneNum, sRealName, nGoldCount, nTicketCount);
if bHaveRoom == false then
-- 切换到主场景
local scene = require("app.views.MainScene")
local sceneNode = scene:create();
cc.Director:getInstance():replaceScene(sceneNode)
end
-- 亲加登入
local uUserId1 = tostring(g_tableUserData.nUserId1)
local uUserId2 = tostring(g_tableUserData.nUserId2)
local uUserId = uUserId1 .. uUserId2
-- QJLogin(uUserId)
end