67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
[System.Serializable]
|
|
public class LoginResponse
|
|
{
|
|
public string msg;
|
|
public string state;
|
|
public int userId;
|
|
public string token;
|
|
}
|
|
public class LoginManager : MonoBehaviour
|
|
{
|
|
public static LoginManager Instance;
|
|
|
|
public LoginScene loginScene;
|
|
//public string loginIP = "http://172.16.1.254";
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
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);
|
|
|
|
|
|
LoginResponse resp = JsonUtility.FromJson<LoginResponse>(json);
|
|
if (resp.state == "true")
|
|
{
|
|
Debug.Log("登录成功,准备回调");
|
|
Debug.Log("账号密码正确登录成功");
|
|
OnLoginSuccess?.Invoke(); // ← 关键
|
|
}
|
|
else
|
|
{
|
|
loginScene.ShowTip("登录失败,用户名或密码错误!");
|
|
Debug.LogWarning("登录失败:" + resp.msg);
|
|
}
|
|
}
|
|
}
|
|
}
|