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

208 lines
6.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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]
AppConst.SdkPackageName = AndroidDeviceInfo.Instance:GetPackageName()
end
-- 判断是否是注册并登录
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
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
-- 检测是否是新账户
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])
this: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])
this: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
require("Base.HttpCrypt")
local CRYPT_KEY = "d53b3e8ef74bf72d8aafce3a1c8671a0"
function this:SendGetHttp(url, callback, _, _, failCB)
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)
end
return this