373 lines
12 KiB
Lua
373 lines
12 KiB
Lua
local PhoneLoginView = import(".PhoneLoginView2")
|
||
local IdPasswordLoginView = import(".IdPasswordLoginView")
|
||
LoginView = {}
|
||
|
||
|
||
local M = {}
|
||
|
||
--- Create a new LoginView
|
||
function LoginView.new()
|
||
setmetatable(M, { __index = BaseView })
|
||
local self = setmetatable({}, { __index = M })
|
||
self.class = "LoginView"
|
||
self._full = true
|
||
self._full_offset = false
|
||
self:init()
|
||
return self
|
||
end
|
||
|
||
---
|
||
--@function [parent=#LoginView] init
|
||
--@param self
|
||
function M:init()
|
||
UIPackage.AddPackage("base/login/ui/Login")
|
||
|
||
--UIPackage.AddPackage("UI/Card")
|
||
ViewUtil.PlaySoundBg()
|
||
self:InitView("ui://Login/Main")
|
||
|
||
local view = self._view
|
||
-- view:GetChild("tex_version").text = "Version:" .. GetGameInfoPlatform("version")
|
||
-- -- print(GameApplication.Instance.accountTest and 1 or 0)
|
||
view:GetController("test").selectedIndex = GameApplication.Instance.accountTest and 1 or 0
|
||
self.agree = view:GetController("agree");
|
||
self.agree.selectedIndex = 1
|
||
-- Utils.LoadBg("loginbg", view)
|
||
if GameApplication.Instance.accountTest then
|
||
local json_data = Utils.LoadLocalFile("userId")
|
||
if json_data then
|
||
local _data = json.decode(json_data)
|
||
view:GetChild("tex_unionid").text = _data.userId
|
||
end
|
||
end
|
||
|
||
local _btn_login = view:GetChild("btn_wx")
|
||
|
||
_btn_login.onClick:Add(function()
|
||
if self.agree.selectedIndex == 0 then
|
||
ViewUtil.ErrorTip(self._root_view, "请勾选同意《用户协议》")
|
||
return
|
||
end
|
||
ViewUtil.ShowModalWait2(self._root_view, "正在登录游戏...")
|
||
coroutine.start(function()
|
||
coroutine.wait(8)
|
||
if self.isWXCallBackMark then
|
||
return
|
||
end
|
||
ViewUtil.CloseModalWait2()
|
||
ViewUtil.ErrorTip(10000, "微信登录失败!")
|
||
end)
|
||
if (not GameApplication.Instance.accountTest) then
|
||
GameApplication.Instance:WXLogin(handler(self, self.LoginCallBack))
|
||
else
|
||
--local ctr_user = view:GetController("user")
|
||
|
||
local _tex_unionid = view:GetChild("tex_unionid")
|
||
|
||
local utez = _tex_unionid.text --.. (ctr_user.selectedIndex + 1)
|
||
|
||
local _data = {}
|
||
_data["userId"] = utez
|
||
local key = "userId"
|
||
local s, e = pcall(function()
|
||
Utils.SaveLocalFile(key, json.encode(_data))
|
||
end)
|
||
if not s then
|
||
-- print("Error:" .. e)
|
||
end
|
||
|
||
DataManager.SelfUser.acc = utez
|
||
DataManager.SelfUser.nick_name = utez
|
||
DataManager.SelfUser.sex = 1
|
||
DataManager.SelfUser.head_url = ""
|
||
self:LoginCallBack(0)
|
||
end
|
||
end)
|
||
|
||
--按钮取消,功能暂时不用
|
||
-- local btn_passwrod = view:GetChild("btn_passwrod")
|
||
-- btn_passwrod.onClick:Set(function()
|
||
-- self:PhoneLogin()
|
||
-- end)
|
||
|
||
local btn_phone_code = view:GetChild("btn_phone_code")
|
||
if btn_phone_code then
|
||
btn_phone_code.onClick:Set(function()
|
||
if self.agree.selectedIndex == 0 then
|
||
ViewUtil.ErrorTip(self._root_view, "请勾选同意《用户协议》")
|
||
return
|
||
end
|
||
self:PhoneCodeLogin()
|
||
end)
|
||
end
|
||
|
||
local btn_id = view:GetChild("btn_id")
|
||
if btn_id then
|
||
btn_id.onClick:Set(function()
|
||
if self.agree.selectedIndex == 0 then
|
||
ViewUtil.ErrorTip(self._root_view, "请勾选同意《用户协议》")
|
||
return
|
||
end
|
||
self:IDLogin()
|
||
end)
|
||
end
|
||
end
|
||
|
||
function M:Destroy()
|
||
if self._agreement then
|
||
self._agreement:Destroy()
|
||
end
|
||
BaseView.Destroy(self)
|
||
end
|
||
|
||
function M:Show()
|
||
BaseView.Show(self)
|
||
self:QuickLogin()
|
||
end
|
||
|
||
local function __goto_lobby(response)
|
||
if response.Data then
|
||
local notices = response.Data.notice_list
|
||
if notices and #notices > 0 then
|
||
local tem = {}
|
||
tem.data = notices
|
||
tem.auto_show = true
|
||
DataManager.SelfUser.notices = tem
|
||
end
|
||
end
|
||
ControllerManager.ChangeController(LoddyController)
|
||
ViewManager.ChangeView(ViewManager.View_Lobby)
|
||
end
|
||
|
||
local function __join_room(roomid, res)
|
||
local loddyctr = ControllerManager.GetController(LoddyController)
|
||
loddyctr:JoinRoom(roomid, function(res1)
|
||
ViewUtil.CloseModalWait2()
|
||
if res1.ReturnCode == -2 then
|
||
__join_room(roomid, res)
|
||
elseif res1.ReturnCode == 0 then
|
||
ViewManager.ChangeView(ViewManager.View_Main, DataManager.CurrenRoom.game_id)
|
||
elseif res1.ReturnCode == 101 or res1.ReturnCode == 6 then
|
||
__goto_lobby(res)
|
||
else
|
||
ViewUtil.ErrorTip(res1.ReturnCode, "登录失败!")
|
||
end
|
||
end)
|
||
end
|
||
|
||
local function __login_response(self, response)
|
||
ViewUtil.CloseModalWait2()
|
||
local skey = "session_id"
|
||
if (response.ReturnCode == 0) then
|
||
local user = DataManager.SelfUser
|
||
-- if (user.guild) then
|
||
ExtendManager.Destroy()
|
||
|
||
local function f_enterLobby(...)
|
||
-- body
|
||
local _client = ControllerManager.WebClient
|
||
|
||
PlayerPrefs.SetString(skey, _client:getSession())
|
||
PlayerPrefs.Save()
|
||
|
||
ExtendManager.Init(user.games)
|
||
|
||
local roomid = user.room_id
|
||
if (string.len(roomid) > 1) then
|
||
ViewUtil.ShowModalWait2(self._root_view, "正在加入房间...")
|
||
if user.group_id == 0 then
|
||
__join_room(roomid, response)
|
||
return
|
||
else
|
||
self:ReconnectRoom(user.group_id)
|
||
return
|
||
end
|
||
end
|
||
__goto_lobby(response)
|
||
end
|
||
|
||
--把拿取跑马灯信息放在这里
|
||
local lobbyCtr1 = ControllerManager.GetController(LoddyController)
|
||
lobbyCtr1:UpdateNotice(DataManager.SelfUser.account_id, function(result, data)
|
||
if result then
|
||
if not data or not data.notice_list then
|
||
self._mesList = ""
|
||
else
|
||
local mesl = {}
|
||
for i = 1, #data.notice_list do
|
||
mesl[i] = data.notice_list[i] --.informContent
|
||
end
|
||
self._mesList = mesl
|
||
DataManager.GameNotice = mesl
|
||
DataManager.DownLink = data.share_link
|
||
end
|
||
end
|
||
end)
|
||
|
||
if user.update ~= 0 then
|
||
ExtendHotupdate.UpdateGameList(user.games, f_enterLobby)
|
||
else
|
||
f_enterLobby()
|
||
end
|
||
else
|
||
if (response.ReturnCode == Table_Error_code.ERR_SERVER or response.ReturnCode == Table_Error_code.ERR_LOGOUT) then
|
||
PlayerPrefs.DeleteKey(skey)
|
||
PlayerPrefs.Save()
|
||
end
|
||
ViewUtil.ErrorTip(response.ReturnCode, "登录失败!")
|
||
end
|
||
end
|
||
|
||
function M:PhoneLogin()
|
||
local _phoneView = nil
|
||
_phoneView = PhoneLoginView.new(0, function(res)
|
||
if res.ReturnCode == 0 then
|
||
_phoneView:Destroy()
|
||
end
|
||
__login_response(self, res)
|
||
end)
|
||
_phoneView:Show()
|
||
end
|
||
|
||
function M:PhoneCodeLogin()
|
||
local _phoneCodeView = nil
|
||
_phoneCodeView = PhoneLoginView.new(1, self.phone, function(res)
|
||
if res.ReturnCode == 0 then
|
||
_phoneCodeView:Destroy()
|
||
end
|
||
__login_response(self, res)
|
||
end)
|
||
_phoneCodeView.agree = self.agree;
|
||
_phoneCodeView:Show()
|
||
end
|
||
|
||
function M:IDLogin()
|
||
local _idView = nil
|
||
_idView = IdPasswordLoginView.new(0, function(res)
|
||
if res.ReturnCode == 0 then
|
||
_idView:Destroy()
|
||
end
|
||
__login_response(self, res)
|
||
end)
|
||
_idView:Show()
|
||
end
|
||
|
||
function M:QuickLogin()
|
||
if (not GameApplication.Instance.accountTest) then
|
||
local session_id = PlayerPrefs.GetString("session_id")
|
||
self.phone = PlayerPrefs.GetString("session_phone")
|
||
-- print("session_id:" .. session_id)
|
||
if session_id and string.len(session_id) > 3 then
|
||
PlayerPrefs.DeleteKey('session_phone')
|
||
PlayerPrefs.Save()
|
||
if self.phone and #self.phone > 0 then
|
||
return
|
||
end
|
||
ViewUtil.ShowModalWait2(self._root_view, "正在登录游戏...")
|
||
local loginCtr = ControllerManager.GetController(LoginController)
|
||
loginCtr:QuickLogin(session_id, function(response)
|
||
__login_response(self, response)
|
||
end)
|
||
end
|
||
end
|
||
end
|
||
|
||
function M:LoginCallBack(result, data)
|
||
self.isWXCallBackMark = true
|
||
---- print("微信登录返回================================================================")
|
||
---- print("result===>"..result)
|
||
--pt(data)
|
||
if (not result) or result ~= 0 then
|
||
if result == 10 then
|
||
ViewUtil.ShowModalWait2(self._root_view)
|
||
return
|
||
end
|
||
ViewUtil.CloseModalWait2()
|
||
return
|
||
end
|
||
|
||
if data then
|
||
local jd = json.decode(data)
|
||
pt(jd)
|
||
local headurl = jd["headimgurl"]
|
||
local unionid = jd["unionid"]
|
||
local sex = jd["sex"]
|
||
if (sex == 0) then sex = 1 end
|
||
local nickname = jd["nickname"]
|
||
|
||
DataManager.SelfUser.acc = unionid
|
||
DataManager.SelfUser.nick_name = nickname
|
||
DataManager.SelfUser.sex = sex
|
||
DataManager.SelfUser.head_url = headurl
|
||
if not DataManager.SelfUser.acc or string.len(DataManager.SelfUser.acc) < 1 then
|
||
ViewUtil.CloseModalWait2()
|
||
return
|
||
end
|
||
end
|
||
|
||
|
||
|
||
local loginCtr = ControllerManager.GetController(LoginController)
|
||
loginCtr:Login(function(response)
|
||
__login_response(self, response)
|
||
end)
|
||
end
|
||
|
||
--在进入前重连房间,如果有
|
||
function M:ReconnectRoom(groupId, times)
|
||
local roomId = DataManager.SelfUser.room_id
|
||
local times = times or 0
|
||
print("===============================ReconnectRoom", roomId, 1)
|
||
if roomId and #roomId > 0 then
|
||
local roomCtr = ControllerManager.GetController(RoomController)
|
||
roomCtr:PublicJoinRoom(
|
||
Protocol.WEB_FG_JOIN_ROOM,
|
||
roomId,
|
||
false,
|
||
function(response)
|
||
if (response.ReturnCode == -1) then
|
||
ViewUtil.CloseModalWait2()
|
||
ViewUtil.ErrorMsg(self._root_view, response.ReturnCode, 'response.ReturnCode == -1')
|
||
RestartGame()
|
||
return
|
||
end
|
||
|
||
if response.ReturnCode ~= 0 then
|
||
-- if times >= 6 then
|
||
-- ViewUtil.ErrorMsg(self._root_view, response.ReturnCode, '进入房间失败')
|
||
-- FamilyView.lastId = groupId
|
||
-- ViewManager.ChangeView(ViewManager.View_Family)
|
||
-- return
|
||
-- else
|
||
self:ReconnectRoom(groupId, times + 1)
|
||
return
|
||
-- end
|
||
else
|
||
ViewUtil.CloseModalWait2()
|
||
UIPackage.AddPackage("base/lobby/ui/Lobby")
|
||
FamilyView.lastId = groupId
|
||
DataManager.SelfUser.room_id = ""
|
||
local fgCtr = ControllerManager.GetController(NewGroupController)
|
||
fgCtr:FG_GroupList(function(res)
|
||
print("获取所有圈信息")
|
||
pt(res)
|
||
|
||
if res.ReturnCode ~= 0 then
|
||
ViewUtil.ErrorTip(res.ReturnCode, "重连获取亲友圈信息失败")
|
||
end
|
||
end)
|
||
ViewManager.ChangeView(ViewManager.View_Main, DataManager.CurrenRoom.game_id)
|
||
end
|
||
end,
|
||
groupId
|
||
)
|
||
end
|
||
end
|
||
|
||
function M:Destroy()
|
||
BaseView.Destroy(self)
|
||
-- UIPackage.RemovePackage("base/embed/ui/Hotupdate")
|
||
UIPackage.RemovePackage("base/login/ui/Login")
|
||
-- ResourcesManager.UnLoad("base/ui/Login.bytes")
|
||
end
|