using System; using System.Collections; using System.Collections.Generic; using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; using UnityEngine.Serialization; /// /// 登录 /// [System.Serializable] public class LoginResponse { public string state; public string message; public Data data=new Data(); } public class Data { /// /// /// public string user_id { get; set; } /// /// /// public string login_name { get; set; } /// /// 1#学生 /// public string real_name { get; set; } /// /// /// public string role_code { get; set; } /// /// 学生 /// public string role_name { get; set; } } public class LoginManager : SingletonMono { //[FormerlySerializedAs("loginScene")] //public UI_LoginPanel uiLoginPanel; //public string loginIP = "http://172.16.1.254"; public LoginResponse loginRespons; public System.Action OnLoginSuccess; public void Login(string account, string password) { StartCoroutine(LoginRequest(account, password)); } // 发送网络请求 IEnumerator LoginRequest(string account, string password) { WWWForm form = new WWWForm(); form.AddField("action", "login"); form.AddField("login_name", account); form.AddField("password", password); using (UnityWebRequest webRequest = UnityWebRequest.Post("http://172.16.1.254:13030/Handler/User.ashx?action=login", form)) { yield return webRequest.SendWebRequest(); if (webRequest.result != UnityWebRequest.Result.Success) { Debug.LogError("登录失败:" + webRequest.error); yield break; } string json = webRequest.downloadHandler.text; Debug.Log("服务器返回:" + json); loginRespons = JsonConvert.DeserializeObject(json); if (loginRespons.state == "true") { Debug.Log("登录成功,准备回调"); Debug.Log("账号密码正确登录成功"); OnLoginSuccess?.Invoke(); // ← 关键 } else { UI_LoginPanel.instance.ShowTip("登录失败,用户名或密码错误!"); Debug.LogWarning("登录失败:" + loginRespons.state); } } } }