c1_lua/lua/app/module/account/account_manager.lua
2023-06-02 19:00:26 +08:00

149 lines
4.6 KiB
Lua

local AccountManager = class("AccountManager", BaseModule)
function AccountManager:showBindUI()
if DataManager.PlayerData:isBinded() then
return
end
UIManager:showUI("app/ui/game_setting/setting_binding_ui")
end
function AccountManager:showDeleteUI()
return UIManager:showUI("app/ui/game_setting/account_delete_ui")
end
function AccountManager:getIsBinded()
local accountInfo = LocalData:getAccountInfo()
if accountInfo.google_id and accountInfo.google_id ~= "" then
return true
end
if accountInfo.apple_id and accountInfo.apple_id ~= "" then
return true
end
return false
end
function AccountManager:deleteAccount()
self:sendMessage(ProtoMsgType.FromMsgEnum.DeleteReq, {}, {}, self.onDeleteAccount, BIReport.ITEM_GET_TYPE.NONE)
end
function AccountManager:onDeleteAccount(result)
if result.err_code == GConst.ERROR_STR.SUCCESS then -- 删除账号成功
local info = LocalData:getLastLoginInfo()
BIReport:postAccountDelete(info.type)
ModuleManager.LoginManager:goToLoginScene()
else
GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DELETE_ACCOUNT_FAILED))
end
end
function AccountManager:bindAccount()
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
end
BIReport:postAccountBindClick(loginType)
SDKManager:login(function(params)
if not params.token then
return
end
local args = {
type = SDKManager.LOGIN_TYPE[loginType],
id = params.id,
token = params.token
}
self:sendMessage(ProtoMsgType.FromMsgEnum.BindReq, args, {}, self.onBindAccount, BIReport.ITEM_GET_TYPE.NONE)
end, loginType)
end
function AccountManager:onBindAccount(result)
if result.status == 0 then
local accountInfo = LocalData:getAccountInfo()
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
accountInfo.apple_id = result.reqData.id
else
accountInfo.google_id = result.reqData.id
end
LocalData:setLastLoginInfo(loginType, result.reqData.id, result.reqData.token)
LocalData:setAccountInfo(accountInfo)
LocalData:save()
BIReport:postAccountBindFinish(loginType, true)
GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_SUCCESS))
EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.BIND_ACCOUNT_SUCCESS)
else
local params = {
boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
}
if result.status == 13 then -- 重复绑定
params.content = I18N:getGlobalText(I18N.GlobalConst.ALREADY_BINDED_DESC)
else
params.content = I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_FAILED)
end
GFunc.showMessageBox(params)
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
end
BIReport:postAccountBindFinish(loginType, false)
end
end
function AccountManager:changeAccount()
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
end
BIReport:postAccountChangeClick(loginType)
SDKManager:login(function(params)
if not params.token then
return
end
local args = {
type = SDKManager.LOGIN_TYPE[loginType],
id = params.id,
token = params.token
}
self:sendMessage(ProtoMsgType.FromMsgEnum.ExistReq, args, {}, self.onChangeAccount, BIReport.ITEM_GET_TYPE.NONE)
end, loginType)
end
function AccountManager:onChangeAccount(result)
if result.status == 0 then
local accountInfo = LocalData:getAccountInfo()
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
accountInfo.apple_id = result.reqData.id
else
accountInfo.google_id = result.reqData.id
end
LocalData:setLastLoginInfo(loginType, result.reqData.id, result.reqData.token)
LocalData:setAccountInfo(accountInfo)
LocalData:save()
BIReport:postAccountChangeFinish(loginType, true)
ModuleManager.LoginManager:goToLoginScene()
else
local params = {
boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
}
if result.status == 14 then
params.content = I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_EXCHANGE_DESC)
else
params.content = I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_FAILED)
end
GFunc.showMessageBox(params)
local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
if Platform:isIosPlatform() then
loginType = SDKManager.BF_LOGIN_TYPE.APPLE
end
BIReport:postAccountChangeFinish(loginType, false)
end
end
return AccountManager