2024-11-11 19:48:32 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class LoginAndGetToken : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public delegate void TokenReceivedDelegate(string token);
|
|
|
|
|
public static event TokenReceivedDelegate OnTokenReceived;
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2024-11-13 09:13:53 +08:00
|
|
|
|
Login();
|
2024-11-11 19:48:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 11:44:51 +08:00
|
|
|
|
public async void Login()
|
2024-11-11 19:48:32 +08:00
|
|
|
|
{
|
|
|
|
|
// ... <20><>¼<EFBFBD><EFBFBD> ...
|
|
|
|
|
loginbody body = new loginbody();
|
|
|
|
|
body.userName = "15151658596";// + 15151658596;
|
|
|
|
|
body.password = "123456";// + 123456;
|
|
|
|
|
body.verifyCode = 111111;
|
|
|
|
|
string loginResponse = await web.SendRequest("http://121.40.42.41:8080/snail/user/login", "POST", JsonUtility.ToJson(body));
|
2024-11-12 11:06:38 +08:00
|
|
|
|
//Debug.Log("LoginAndGetToken<65><6E>¼:"+loginResponse);
|
2024-11-11 19:48:32 +08:00
|
|
|
|
string token = getToken(loginResponse);
|
2024-11-14 18:09:37 +08:00
|
|
|
|
int userId = getUserId(loginResponse);
|
2024-11-11 19:48:32 +08:00
|
|
|
|
OnTokenReceived?.Invoke(token);
|
|
|
|
|
//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>//<2F><>ͨ<EFBFBD><CDA8>
|
|
|
|
|
}
|
|
|
|
|
public static string getToken(string json)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>ΪJObject
|
|
|
|
|
JObject parsedJson = JObject.Parse(json);
|
|
|
|
|
|
|
|
|
|
// <20><>JObject<63><74><EFBFBD><EFBFBD>ȡtoken<65>ֶε<D6B6>ֵ
|
|
|
|
|
string token = parsedJson["data"]?["token"]?.ToString();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>token
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܳ<EFBFBD><DCB3>ֵ<EFBFBD><D6B5>쳣
|
|
|
|
|
Console.WriteLine("An error occurred while parsing the JSON: " + ex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-14 18:09:37 +08:00
|
|
|
|
//<2F><>ȡuserId
|
|
|
|
|
public static int getUserId(string json)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ JObject
|
|
|
|
|
JObject parsedJson = JObject.Parse(json);
|
|
|
|
|
|
|
|
|
|
// <20><> JObject <20><><EFBFBD><EFBFBD>ȡ userId <20>ֶε<D6B6>ֵ
|
|
|
|
|
string userIdString = parsedJson["data"]?["userId"]?.ToString();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>Խ<EFBFBD> userId ת<><D7AA>Ϊ int
|
|
|
|
|
if (int.TryParse(userIdString, out int userId))
|
|
|
|
|
{
|
|
|
|
|
return userId; // <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> userId
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed to parse userId as an integer.");
|
|
|
|
|
return -1; // <20><><EFBFBD><EFBFBD> -1 <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܳ<EFBFBD><DCB3>ֵ<EFBFBD><D6B5>쳣
|
|
|
|
|
Console.WriteLine("An error occurred while parsing the JSON: " + ex.Message);
|
|
|
|
|
return -1; // <20><><EFBFBD><EFBFBD> -1 <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 19:48:32 +08:00
|
|
|
|
[Serializable]
|
|
|
|
|
public class loginbody//<2F><>¼<EFBFBD><C2BC>ע<EFBFBD><D7A2><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
public string userName;
|
|
|
|
|
public string password;
|
|
|
|
|
public int verifyCode;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*public class LoginAndGetToken : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private static LoginAndGetToken _instance;
|
|
|
|
|
public static LoginAndGetToken Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
GameObject obj = new GameObject("LoginAndGetToken");
|
|
|
|
|
_instance = obj.AddComponent<LoginAndGetToken>();
|
|
|
|
|
DontDestroyOnLoad(obj); // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ᱻ<EFBFBD><E1B1BB><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _token;
|
|
|
|
|
|
|
|
|
|
public async void Login()
|
|
|
|
|
{
|
|
|
|
|
loginbody body = new loginbody();
|
|
|
|
|
body.userName = "15151658596";
|
|
|
|
|
body.password = "123456";
|
|
|
|
|
body.verifyCode = 111111;
|
|
|
|
|
string loginResponse = await web.SendRequest("http://121.40.42.41:8080/snail/user/login", "POST", JsonUtility.ToJson(body));
|
|
|
|
|
Debug.Log(loginResponse);
|
|
|
|
|
_token = getToken(loginResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetToken()
|
|
|
|
|
{
|
|
|
|
|
return Instance._token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string getToken(string loginResponse)
|
|
|
|
|
{
|
|
|
|
|
JObject jsonObject = JObject.Parse(loginResponse);
|
|
|
|
|
string token = (string)jsonObject["data"]?["token"];
|
|
|
|
|
Debug.Log(token);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
}*/
|