miduo_client/Assets/ManagedResources/~Lua/Modules/Login/LoginManager.lua

267 lines
9.0 KiB
Lua
Raw Normal View History

2021-07-27 10:04:22 +08:00
LoginManager = {};
2020-05-09 13:31:21 +08:00
local this = LoginManager
this.sign = "d13b3e8ef74bf72d8aafce3a1c8672a0"
this.openId = nil
this.token = nil
this.IsLogin = false
this.pt_pId = ""
this.pt_gId = ""
2021-03-02 15:18:06 +08:00
local LoginRoot_Url = ServerConfigManager.GetVersionInfo("serverUrl")
2020-05-09 13:31:21 +08:00
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
2020-07-23 20:46:51 +08:00
SDKMgr.onRegisterCallback = function()
this.isRegister = true
end
2020-07-15 13:44:37 +08:00
SDKMgr.onLoginCallback = function(loginResp)
-- 获取登录信息
local result = string.split(loginResp, "#")
result[1] = tonumber(result[1])
if result[1] == SDK_RESULT.SUCCESS then
2021-06-09 17:13:37 +08:00
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 ""
2020-09-10 14:50:25 +08:00
AppConst.SdkPackageName = AndroidDeviceInfo.Instance:GetPackageName()
-- 判断是否是注册并登录
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()
else
local errCode = result[2]
if not errCode then
MsgPanel.ShowOne("登录失败")
elseif errCode == "101" then
MsgPanel.ShowOne("根据国家防沉迷相关规定,我们将不再向未成年人提供服务,敬请谅解!")
end
end
2020-05-09 13:31:21 +08:00
end
2020-07-15 13:44:37 +08:00
SDKMgr.onSwitchAccountCallback = function(resp)
Log("onSwitchAccountCallback::"..resp)
2020-05-09 13:31:21 +08:00
local result = string.split(resp, "#")
if tonumber(result[1]) == SDK_RESULT.SUCCESS then
ThinkingAnalyticsManager.Track("change_account")
Game.Restart()
2020-05-09 13:31:21 +08:00
end
end
2020-07-15 13:44:37 +08:00
SDKMgr.onLogoutCallback = function(resp)
Log("onLogoutCallback::"..resp)
2020-05-09 13:31:21 +08:00
local result = string.split(resp, "#")
if LoginManager.IsLogin and tonumber(result[1]) == SDK_RESULT.SUCCESS then
ThinkingAnalyticsManager.Track("quit_account")
Game.Restart()
2020-05-09 13:31:21 +08:00
end
-- 发送登出事件
Game.GlobalEvent:DispatchEvent(GameEvent.LoginSuccess.OnLogout)
2020-05-09 13:31:21 +08:00
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.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 "",
2021-07-27 10:04:22 +08:00
device_id = AppConst.isSDK and ThinkingAnalyticsManager.GetDeviceId() or "",
packId = PackageManager.GetPackageID() or "",
})
ThinkingAnalyticsManager.Track("create_account")
end
2020-05-09 13:31:21 +08:00
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))
2021-03-02 16:53:12 +08:00
RequestPanel.Show(Language[11132])
2021-07-13 18:24:33 +08:00
this:SendGetHttp(LoginRoot_Url .. "jl_loginserver/registerUser?userName=".. name .. "&password=".. pw .."&repeat=".. pw .. "&sign=" .. sign,
2020-05-09 13:31:21 +08:00
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
2021-03-02 16:53:12 +08:00
PopupTipPanel.ShowTip(Language[11133]..name.." "..data.msg)
2020-05-09 13:31:21 +08:00
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))
2021-03-02 16:53:12 +08:00
RequestPanel.Show(Language[11134])
2021-07-13 18:24:33 +08:00
this:SendGetHttp(LoginRoot_Url .. "jl_loginserver/userLogin?userName=".. name .. "&password=".. pw .. "&sign=" .. sign, function(str)
2020-05-09 13:31:21 +08:00
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
2021-07-13 18:24:33 +08:00
require("Base.HttpCrypt")
local CRYPT_KEY = "d53b3e8ef74bf72d8aafce3a1c8671a0"
function this:SendGetHttp(url, callback, _, _, failCB)
2021-07-13 18:24:33 +08:00
local request = url
LogGreen("Http请求"..request)
if AppConst.Platform == "IOS" then
local urlList = string.split(url, "?")
urlList[2] = HTTP_ENCRYPT(urlList[2], CRYPT_KEY)
request = urlList[1] .. "?crypt=" .. urlList[2]
LogGreen("Http加密请求"..request)
end
networkMgr:SendGetHttp(request, function(msg)
LogGreen("Http请求结果"..msg)
if AppConst.Platform == "IOS" then
msg = HTTP_DECRYPT(msg, CRYPT_KEY)
LogGreen("Http请求结果解密"..msg)
end
if callback then
callback(msg)
end
end, nil, nil, failCB)
2021-07-13 18:24:33 +08:00
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
2020-06-23 18:36:24 +08:00
return this