yunque9/lua_probject/base_project/Game/View/LoginView.lua

281 lines
8.0 KiB
Lua
Raw Normal View History

2025-05-24 14:29:14 +08:00
local PhoneLoginView = import(".PhoneLoginView")
LoginView = {}
local M = {}
--- Create a new LoginView
function LoginView.new()
2025-05-27 02:05:27 +08:00
setmetatable(M, { __index = BaseView })
local self = setmetatable({}, { __index = M })
2025-05-24 14:29:14 +08:00
self.class = "LoginView"
self._full = true
self:init()
return self
end
---
--@function [parent=#LoginView] init
--@param self
function M:init()
2025-05-27 02:05:27 +08:00
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
-- 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
2025-05-24 14:29:14 +08:00
end
2025-05-27 02:05:27 +08:00
local _btn_login = view:GetChild("btn_wx")
_btn_login.onClick:Add(function()
ViewUtil.ShowModalWait(self._root_view, "正在登录游戏...")
coroutine.start(function()
coroutine.wait(8)
if self.isWXCallBackMark then
return
end
ViewUtil.CloseModalWait()
ViewUtil.ErrorTip(10000, "微信登录失败!")
end)
2025-05-24 14:29:14 +08:00
if (not GameApplication.Instance.accountTest) then
2025-05-27 02:05:27 +08:00
GameApplication.Instance:WXLogin(handler(self, self.LoginCallBack))
2025-05-24 14:29:14 +08:00
else
--local ctr_user = view:GetController("user")
2025-05-27 02:05:27 +08:00
2025-05-24 14:29:14 +08:00
local _tex_unionid = view:GetChild("tex_unionid")
local utez = _tex_unionid.text --.. (ctr_user.selectedIndex + 1)
2025-05-27 02:05:27 +08:00
2025-05-24 14:29:14 +08:00
local _data = {}
_data["userId"] = utez
local key = "userId"
2025-05-27 02:05:27 +08:00
local s, e = pcall(function()
Utils.SaveLocalFile(key, json.encode(_data))
end)
2025-05-24 14:29:14 +08:00
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_phone = view:GetChild("btn_phone")
btn_phone.onClick:Set(function()
self:PhoneLogin()
end)
2025-05-27 02:05:27 +08:00
2025-06-29 22:21:50 +08:00
self:InitClip()
2025-05-24 14:29:14 +08:00
end
function M:Destroy()
if self._agreement then
self._agreement:Destroy()
end
BaseView.Destroy(self)
2025-05-27 02:05:27 +08:00
coroutine.stop(self.donhua)
2025-05-24 14:29:14 +08:00
end
function M:Show()
BaseView.Show(self)
self:QuickLogin()
2025-06-29 22:21:50 +08:00
self:InitClip()
2025-05-24 14:29:14 +08:00
end
local function __goto_lobby(response)
if response.Data then
local notices = response.Data.notice_list
if notices and #notices > 0 then
2025-05-27 02:05:27 +08:00
local tem = {}
tem.data = notices
tem.auto_show = true
DataManager.SelfUser.notices = tem
2025-05-24 14:29:14 +08:00
end
end
ControllerManager.ChangeController(LoddyController)
ViewManager.ChangeView(ViewManager.View_Lobby)
end
local function __join_room(roomid, res)
local loddyctr = ControllerManager.GetController(LoddyController)
2025-05-27 02:05:27 +08:00
loddyctr:JoinRoom(roomid, function(res1)
2025-05-24 14:29:14 +08:00
ViewUtil.CloseModalWait()
if res1.ReturnCode == -2 then
__join_room(roomid, res)
elseif res1.ReturnCode == 0 then
2025-05-27 02:05:27 +08:00
ViewManager.ChangeView(ViewManager.View_Main, DataManager.CurrenRoom.game_id)
2025-05-24 14:29:14 +08:00
elseif res1.ReturnCode == 101 or res1.ReturnCode == 6 then
__goto_lobby(res)
else
2025-05-27 02:05:27 +08:00
ViewUtil.ErrorTip(res1.ReturnCode, "登录失败!")
2025-05-24 14:29:14 +08:00
end
end)
end
2025-05-27 02:05:27 +08:00
local function __login_response(self, response)
2025-05-24 14:29:14 +08:00
ViewUtil.CloseModalWait()
local skey = "session_id"
if (response.ReturnCode == 0) then
local user = DataManager.SelfUser
-- if (user.guild) then
ExtendManager.Destroy()
2025-05-27 02:05:27 +08:00
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
if user.group_id == 0 then
ViewUtil.ShowModalWait(self._root_view, "正在加入房间...")
__join_room(roomid, response)
return
end
end
__goto_lobby(response)
2025-05-24 14:29:14 +08:00
end
2025-05-27 02:05:27 +08:00
2025-05-24 14:29:14 +08:00
if user.update ~= 0 then
2025-05-27 02:05:27 +08:00
ExtendHotupdate.UpdateGameList(user.games, f_enterLobby)
2025-05-24 14:29:14 +08:00
else
2025-05-27 02:05:27 +08:00
f_enterLobby()
2025-05-24 14:29:14 +08:00
end
else
2025-05-27 02:05:27 +08:00
if (response.ReturnCode == Table_Error_code.ERR_SERVER or response.ReturnCode == Table_Error_code.ERR_LOGOUT) then
2025-05-24 14:29:14 +08:00
PlayerPrefs.DeleteKey(skey)
PlayerPrefs.Save()
end
2025-05-27 02:05:27 +08:00
ViewUtil.ErrorTip(response.ReturnCode, "登录失败!")
2025-05-24 14:29:14 +08:00
end
end
function M:PhoneLogin()
2025-05-27 02:05:27 +08:00
local _phoneView = nil
2025-05-24 14:29:14 +08:00
_phoneView = PhoneLoginView.new(function(res)
if res.ReturnCode == 0 then
_phoneView:Destroy()
end
2025-05-27 02:05:27 +08:00
__login_response(self, res)
end)
2025-05-24 14:29:14 +08:00
_phoneView:Show()
end
function M:IDLogin()
2025-05-27 02:05:27 +08:00
local _idView = nil
2025-05-24 14:29:14 +08:00
_idView = IDLoginView.new(function(res)
if res.ReturnCode == 0 then
_idView:Destroy()
end
2025-05-27 02:05:27 +08:00
__login_response(self, res)
end)
2025-05-24 14:29:14 +08:00
_idView:Show()
end
function M:QuickLogin()
if (not GameApplication.Instance.accountTest) then
local session_id = PlayerPrefs.GetString("session_id")
2025-05-27 02:05:27 +08:00
print("session_id:" .. session_id)
if session_id and string.len(session_id) > 3 then
ViewUtil.ShowModalWait(self._root_view, "正在登录游戏...")
local loginCtr = ControllerManager.GetController(LoginController)
loginCtr:QuickLogin(session_id, function(response)
__login_response(self, response)
end)
2025-05-24 14:29:14 +08:00
end
end
end
2025-05-27 02:05:27 +08:00
function M:LoginCallBack(result, data)
self.isWXCallBackMark = true
--print("微信登录返回================================================================")
--print("result===>"..result)
--pt(data)
if (not result) or result ~= 0 then
2025-05-24 14:29:14 +08:00
if result == 10 then
2025-05-27 02:05:27 +08:00
ViewUtil.ShowModalWait(self._root_view)
return
2025-05-24 14:29:14 +08:00
end
ViewUtil.CloseModalWait()
return
end
if data then
local jd = json.decode(data)
2025-05-27 02:05:27 +08:00
pt(jd)
2025-05-24 14:29:14 +08:00
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
2025-05-27 02:05:27 +08:00
if not DataManager.SelfUser.acc or string.len(DataManager.SelfUser.acc) < 1 then
2025-05-24 14:29:14 +08:00
ViewUtil.CloseModalWait()
return
end
end
2025-05-27 02:05:27 +08:00
2025-05-24 14:29:14 +08:00
local loginCtr = ControllerManager.GetController(LoginController)
loginCtr:Login(function(response)
2025-05-27 02:05:27 +08:00
__login_response(self, response)
2025-05-24 14:29:14 +08:00
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")
2025-05-27 02:05:27 +08:00
end
2025-06-29 22:21:50 +08:00
function M:InitClip()
local xuehuaEnd = 120
local xuehuaName = "ui://Login/xuehua_00"
local xuehua = self._view:GetChild('clip_xuehua')
local xuehuaIndex = 0
self.donhua = coroutine.start(
function()
while true do
xuehuaIndex = self:ReplaceClip1(xuehuaEnd, xuehua, xuehuaIndex, xuehuaName)
coroutine.wait(0.12)
end
end
)
end
function M:ReplaceClip1(e, t, i, n)
if i > e then
i = 0
end
t.url = string.format("%s%03d", n, i)
return i + 6
end