302 lines
10 KiB
Lua
302 lines
10 KiB
Lua
LoginManager = {};
|
|
local this = LoginManager
|
|
this.sign = "d13b3e8ef74bf72d8aafce3a1c8672a0"
|
|
this.openId = nil
|
|
this.token = nil
|
|
this.IsLogin = false
|
|
this.pt_pId = ""
|
|
this.pt_gId = ""
|
|
|
|
local LoginRoot_Url = ServerConfigManager.GetVersionInfo("serverUrl")
|
|
function this.Initialize()
|
|
this.IsLogin = false
|
|
this.GameName='DouDou'
|
|
local defaultLoginWay=0
|
|
if UnityEngine.Application.isMobilePlatform then
|
|
defaultLoginWay=1
|
|
else
|
|
defaultLoginWay=0
|
|
end
|
|
this.CurLoginWay=PlayerPrefs.GetInt(this.GameName.."LoginWay",defaultLoginWay)
|
|
this.CurAccount=PlayerPrefs.GetString(this.GameName.."Account",'')
|
|
this.CurSession=PlayerPrefs.GetString(this.GameName.."Session",'')
|
|
|
|
this.isRegister = false
|
|
|
|
SDKMgr.onInitLaunchCallback = function(result)
|
|
if result == "1" then
|
|
Game.GlobalEvent:DispatchEvent(GameEvent.SDK.InitSuccess)
|
|
end
|
|
end
|
|
SDKMgr.onRegisterCallback = function()
|
|
this.isRegister = true
|
|
end
|
|
|
|
SDKMgr.onLoginCallback = function(loginResp)
|
|
-- 获取登录信息
|
|
local result = string.split(loginResp, "#")
|
|
result[1] = tonumber(result[1])
|
|
if result[1] == SDK_RESULT.SUCCESS then
|
|
AppConst.SdkId = result[2] or ""
|
|
AppConst.OpenId = result[2] or ""
|
|
this.SdkLoginTimeStamp = result[3] or ""
|
|
AppConst.TokenStr = result[4] or ""
|
|
AppConst.SdkChannel = result[5] or ""
|
|
this.extData = result[6] or ""
|
|
this.p_appId = result[7] or ""
|
|
this.SdkSubChannel = result[8] or ""
|
|
|
|
AppConst.SdkPackageName = AndroidDeviceInfo.Instance:GetPackageName()
|
|
-- 账号数据正确性检测
|
|
this.CheckUserId(this.extData, function()
|
|
-- 判断是否是注册并登录
|
|
if this.isRegister then
|
|
this.isRegister = false
|
|
-- 触发创建账户的事件
|
|
this.TrackCreateAccount()
|
|
-- 发送登录成功事件
|
|
Game.GlobalEvent:DispatchEvent(GameEvent.LoginSuccess.OnLoginSuccess, result[1])
|
|
else
|
|
this.CheckIsRegister(AppConst.OpenId, function(isRegister)
|
|
if isRegister then
|
|
-- 触发创建账户的事件
|
|
this.TrackCreateAccount()
|
|
end
|
|
-- 发送登录成功事件
|
|
Game.GlobalEvent:DispatchEvent(GameEvent.LoginSuccess.OnLoginSuccess, result[1])
|
|
end)
|
|
end
|
|
-- 防沉迷检测
|
|
this.CheckRealName()
|
|
end)
|
|
else
|
|
local errCode = result[2]
|
|
if not errCode then
|
|
MsgPanel.ShowOne("登录失败")
|
|
elseif errCode == "101" then
|
|
MsgPanel.ShowOne("根据国家防沉迷相关规定,我们将不再向未成年人提供服务,敬请谅解!")
|
|
end
|
|
end
|
|
end
|
|
|
|
SDKMgr.onSwitchAccountCallback = function(resp)
|
|
Log("onSwitchAccountCallback::"..resp)
|
|
local result = string.split(resp, "#")
|
|
if tonumber(result[1]) == SDK_RESULT.SUCCESS then
|
|
ThinkingAnalyticsManager.Track("change_account")
|
|
Game.Restart()
|
|
end
|
|
end
|
|
|
|
SDKMgr.onLogoutCallback = function(resp)
|
|
Log("onLogoutCallback::"..resp)
|
|
local result = string.split(resp, "#")
|
|
if LoginManager.IsLogin and tonumber(result[1]) == SDK_RESULT.SUCCESS then
|
|
ThinkingAnalyticsManager.Track("quit_account")
|
|
Game.Restart()
|
|
end
|
|
-- 发送登出事件
|
|
Game.GlobalEvent:DispatchEvent(GameEvent.LoginSuccess.OnLogout)
|
|
end
|
|
end
|
|
--
|
|
local _Time
|
|
function this.CheckRealName()
|
|
local appId = PackageManager.GetAppID()
|
|
if appId then
|
|
local url = "http://identity.tyu89.wang/cp/nppa/loop"
|
|
local jsonData = string.format("appId;%s;userId;%s", appId, AppConst.OpenId)
|
|
networkMgr:SendHttpPost_Json_Lua(url, jsonData, function(msg)
|
|
local json = require 'cjson'
|
|
local context = json.decode(msg)
|
|
if context and context.code == 200 then
|
|
if context.data.allowPlay then
|
|
if context.data.needLoop then
|
|
-- 每隔60秒请求一次
|
|
if _Time then
|
|
_Time:Stop()
|
|
end
|
|
_Time = Timer.New(this.CheckRealName, 60)
|
|
_Time:Start()
|
|
end
|
|
else
|
|
if context.data.allowRemind then
|
|
MsgPanel.ShowOne(context.data.remindMessage, function()
|
|
if _Time then
|
|
_Time:Stop()
|
|
end
|
|
Game.Restart()
|
|
end)
|
|
else
|
|
if _Time then
|
|
_Time:Stop()
|
|
end
|
|
Game.Restart()
|
|
end
|
|
end
|
|
end
|
|
end, nil)
|
|
end
|
|
end
|
|
|
|
function this.CheckUserId(extData, func)
|
|
-- 如果没有OpenId则向登录服请求校验
|
|
if not AppConst.OpenId or AppConst.OpenId == "" then
|
|
local json = require 'cjson'
|
|
if extData then
|
|
local context = json.decode(extData)
|
|
if context then
|
|
HttpManager:SendGetHttp(LoginRoot_Url .. "jl_loginserver/getKTGameUserInfo?gid=".. context.gid .. "&pid=".. context.pid .. "&access_token=" .. AppConst.TokenStr,
|
|
function(str)
|
|
if str then
|
|
Log(str)
|
|
local data = json.decode(str)
|
|
AppConst.OpenId = data.openid
|
|
end
|
|
-- 完成
|
|
if func then
|
|
func()
|
|
end
|
|
end)
|
|
return
|
|
end
|
|
end
|
|
end
|
|
-- 不符合条件
|
|
if func then
|
|
func()
|
|
end
|
|
end
|
|
|
|
-- 检测是否是新账户
|
|
function this.CheckIsRegister(openId, func)
|
|
if func then
|
|
func(false)
|
|
end
|
|
end
|
|
|
|
function this.TrackCreateAccount()
|
|
-- 登出之前的账号
|
|
ThinkingAnalyticsManager.Logout()
|
|
ThinkingAnalyticsManager.ClearSuperProperties()
|
|
-- 开始
|
|
ThinkingAnalyticsManager.SetSuperProperties({
|
|
account = AppConst.isSDK and tostring(AppConst.OpenId) or "",
|
|
Bundle_id = AppConst.isSDK and AppConst.SdkPackageName or "",
|
|
xx_id = AppConst.isSDK and AppConst.SdkChannel or "",
|
|
device_id = AppConst.isSDK and ThinkingAnalyticsManager.GetDeviceId() or "",
|
|
packId = PackageManager.GetPackageID() or "",
|
|
})
|
|
ThinkingAnalyticsManager.Track("create_account")
|
|
end
|
|
|
|
function this.SaveLoginInfo(user, pw)
|
|
--[[
|
|
PlayerPrefs.SetInt(this.GameName.."LoginWay", this.CurLoginWay);
|
|
PlayerPrefs.SetString(this.GameName.."Account", this.CurAccount);
|
|
PlayerPrefs.SetString(this.GameName.."Session", this.CurSession);
|
|
PlayerPrefs.Save();
|
|
]]--
|
|
--sdkMgr:SaveSession(this.CurLoginWay)
|
|
|
|
|
|
end
|
|
|
|
function this.RequestRegist(name, pw, call)
|
|
local sign = Util.MD5Encrypt(string.format("%s%s%s", name,
|
|
pw, this.sign))
|
|
|
|
RequestPanel.Show(Language[11132])
|
|
HttpManager:SendGetHttp(LoginRoot_Url .. "jl_loginserver/registerUser?userName=".. name .. "&password=".. pw .."&repeat=".. pw .. "&sign=" .. sign,
|
|
function(str)
|
|
RequestPanel.Hide()
|
|
if str == nil then
|
|
return
|
|
end
|
|
--Log(str)
|
|
local json = require 'cjson'
|
|
local data = json.decode(str)
|
|
|
|
if data.code==1 then
|
|
PopupTipPanel.ShowTip(Language[11133]..name.." "..data.msg)
|
|
else
|
|
--PopupTipPanel.ShowTip("账号:"..name.." "..data.msg)
|
|
end
|
|
if call then
|
|
call(data.code)
|
|
end
|
|
end,nil,nil,nil)
|
|
end
|
|
|
|
function this.RequestUser(name, pw, call)
|
|
local sign = Util.MD5Encrypt(string.format("%s%s%s", name,
|
|
pw, this.sign))
|
|
|
|
RequestPanel.Show(Language[11134])
|
|
HttpManager:SendGetHttp(LoginRoot_Url .. "jl_loginserver/userLogin?userName=".. name .. "&password=".. pw .. "&sign=" .. sign, function(str)
|
|
RequestPanel.Hide()
|
|
if str == nil then
|
|
return
|
|
end
|
|
--Log(str)
|
|
local json = require 'cjson'
|
|
local data = json.decode(str)
|
|
|
|
--Log("data.code:"..data.code)
|
|
--Log("data.msg:"..data.msg)
|
|
|
|
if data.code==0 then
|
|
--Log("data.parms.openId:"..data.parms.openId)
|
|
--Log("data.parms.token:"..data.parms.token)
|
|
|
|
--PopupTipPanel.ShowTip(data.msg)
|
|
this.openId = data.parms.openId
|
|
this.token = data.parms.token
|
|
--场景转换
|
|
else
|
|
PopupTipPanel.ShowTip(data.msg)
|
|
end
|
|
if call then
|
|
call(data.code)
|
|
end
|
|
end,nil,nil,nil)
|
|
end
|
|
|
|
-- 判断服务器状态是否可进入
|
|
function this.IsServerStateEnterable(state)
|
|
if not state then
|
|
return
|
|
end
|
|
if state ~= ServerStateDef.Fluency
|
|
and state ~= ServerStateDef.Congestion
|
|
and state ~= ServerStateDef.Full then
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
|
|
|
|
-- 初始化包信息
|
|
function this.InitPackInfo()
|
|
local LoginRoot_Url = ServerConfigManager.GetVersionInfo("serverUrl")
|
|
local LoginRoot_Channel = ServerConfigManager.GetVersionInfo("channel")
|
|
local LoginRoot_UserChannel = ServerConfigManager.GetSettingValue(ServerConfigManager.SettingConfig.USER_CHANNEL) or ""
|
|
local ChannelID = LoginRoot_UserChannel == "" and LoginRoot_Channel or LoginRoot_UserChannel
|
|
local packId = PackageManager.GetPackageID()
|
|
if packId then
|
|
ChannelID = ChannelID.. packId
|
|
end
|
|
HttpManager:SendGetHttp(LoginRoot_Url.."jl_loginserver/getChannelInfo?ccId="..ChannelID, function(str)
|
|
if str == "" or str == "null" or string.find(str, "html") then
|
|
LogWarn("获取到的数据是空值,请检查!")
|
|
else
|
|
local json = require 'cjson'
|
|
local context = json.decode(str)
|
|
this.IsShield = context.shielding == 1
|
|
end
|
|
end)
|
|
end
|
|
|
|
|
|
return this |