91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.Serialization;
|
|
|
|
/// <summary>
|
|
/// 登录
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class LoginResponse
|
|
{
|
|
public string state;
|
|
public string message;
|
|
public Data data=new Data();
|
|
}
|
|
|
|
public class Data
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string user_id { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string login_name { get; set; }
|
|
/// <summary>
|
|
/// 1#学生
|
|
/// </summary>
|
|
public string real_name { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string role_code { get; set; }
|
|
/// <summary>
|
|
/// 学生
|
|
/// </summary>
|
|
public string role_name { get; set; }
|
|
}
|
|
public class LoginManager : SingletonMono<LoginManager>
|
|
{
|
|
//[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<LoginResponse>(json);
|
|
if (loginRespons.state == "true")
|
|
{
|
|
Debug.Log("登录成功,准备回调");
|
|
Debug.Log("账号密码正确登录成功");
|
|
OnLoginSuccess?.Invoke(); // ← 关键
|
|
}
|
|
else
|
|
{
|
|
UI_LoginPanel.instance.ShowTip("登录失败,用户名或密码错误!");
|
|
Debug.LogWarning("登录失败:" + loginRespons.state);
|
|
}
|
|
}
|
|
}
|
|
}
|