_xiaofang/xiaofang/Assets/Script/loginScripts/LoginPanel.cs
2025-01-03 09:08:55 +08:00

177 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoginPanel : Base
{
public test test;
public Button loginBtn;
public Button getYzmBtn;
public InputField id;
public InputField pwd;
public InputField sjh;
public InputField yzm;
public Image image;
// Start is called before the first frame update
void Start()
{
string lastAccount = AccountManager.GetLastLoggedAccount();
id.text = lastAccount;
Debug.Log("存储数据"+lastAccount);
if (lastAccount != null)
{
Debug.Log($"上次登录的账号是: {lastAccount}");
}
else
{
Debug.Log("没有找到上次登录的账号.");
}
// 初始化:监听输入框和按钮事件
id.onValueChanged.AddListener(OnAccountInputChanged);
pwd.onValueChanged.AddListener(OnPasswordInputChanged);
loginBtn.onClick.AddListener(OnClickLoginBtn);
getYzmBtn.onClick.AddListener(OnClickGetYzmBtn);
image.gameObject.SetActive(false);
//初始状态:禁用登录按钮
loginBtn.interactable = false;
}
// 检查账号输入是否合法
private void OnAccountInputChanged(string input)
{
if (!IsValidAccount(input))
{
id.text = "";
Debug.Log("账号输入不合法!");
}
UpdateLoginButtonState();
}
// 更新登录按钮状态
private void UpdateLoginButtonState()
{
// 按钮启用条件:账号不为空且密码长度 >= 6
loginBtn.interactable = !string.IsNullOrEmpty(id.text) && pwd.text.Length >= 6;
}
// 检查密码输入是否合法
private void OnPasswordInputChanged(string input)
{
if (input.Length > 16)
{
pwd.text = input.Substring(0, 16); // 超过16位截取前16位
Debug.Log("密码长度超过限制!");
}
// 设置为显示 '*' 的形式
string hiddenPassword = new string('*', pwd.text.Length);
pwd.textComponent.text = hiddenPassword;
UpdateLoginButtonState();
}
public void OnClickGetYzmBtn()
{
}
public void OnClickLoginBtn()
{
Login();
}
//登录逻辑
public async void Login()
{
// 创建登录请求的 body
auth_login loginBody = new auth_login
{
grantType = "password",
clientId = "e5cd7e4891bf95d1d19206ce24a7b32e",
userType = "company_user",
username = id.text,//"ZF16c788632",13699802230,13051628292
password = pwd.text//"YYL5371!"
};
image.gameObject.SetActive (true);
//保存登录的账号
AccountManager.SaveLastLoggedAccount(id.text);
// 发送请求
Debug.Log("登陆入参"+ JsonUtility.ToJson(loginBody));
string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(loginBody));
Debug.Log("登录" + response);
// 解析服务器返回的数据
loginResponse serverData = JsonConvert.DeserializeObject<loginResponse>(response);
// 将登录响应数据保存到静态变量中
GlobalData.ServerData = serverData;
ReadRoom.instance.ID = id.text;
test.lodingWebSocket(id.text);
// 打印 access_token
Debug.Log(serverData.data.access_token);
//如果是管理员账号就跳预定演练
//if (loginBody.userType == "company_user")
if(GlobalData.ServerData.data.isCreater=="Y")
{
Debug.LogError(11);
ReadRoom.instance.isadministrator=true;
//ReadRoom.instance.adHead();
SceneManager.LoadScene("yhj");
}
else //if(id.text == "13699802230" && pwd.text == "YYL5371!")
{
//SceneManager.LoadScene("yhj");
ReadRoom.instance.head();
}
//else
//{
// SceneManager.LoadScene("Tmap 1");
//}
////如果是玩家账号就跳转到加入房间界面
//if (loginBody.userType == "Player")
// SceneManager.LoadScene(5);
// 跳转场景
//ReadRoom.instance.head();
}
// 账号合法性验证
private bool IsValidAccount(string input)
{
if (string.IsNullOrEmpty(input)) return false;
// 检查长度限制
//if (input.Length < 3 || input.Length > 20) return false;
// 检查首字符是否为字母
//if (!char.IsLetter(input[0])) return false;
// 检查是否只包含字母、数字和下划线
foreach (char c in input)
{
if (!char.IsLetterOrDigit(c) && c != '_') return false;
}
return true;
}
}