162 lines
4.9 KiB
C#
162 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
using static LoginAndGetToken;
|
|
|
|
|
|
|
|
public class logoPanel : MonoBehaviour
|
|
{
|
|
public InputField userNameField;
|
|
public InputField passwordField;
|
|
public InputField verifyCodeField;
|
|
public InputField mmField;
|
|
public InputField qrmmField;
|
|
public InputField yqmField;
|
|
|
|
public Button pwdBtn;
|
|
public Button yzmBtn;
|
|
public Image regImg;
|
|
|
|
public GameObject yzm;
|
|
public GameObject mm;
|
|
public GameObject qrmm;
|
|
public GameObject yqm;
|
|
|
|
|
|
public GameObject logBtn;
|
|
public Button loginBtn;
|
|
public GameObject regbtn;
|
|
public Button rigistBtn;
|
|
|
|
public Button rigBtn;
|
|
|
|
|
|
|
|
public delegate void TokenReceivedDelegate(string token);
|
|
public static event TokenReceivedDelegate OnTokenReceived;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
yzmBtn.onClick.AddListener(OnClickYzmBtn);
|
|
pwdBtn.onClick.AddListener(OnClickPwdBtn);
|
|
loginBtn.onClick.AddListener(() => StartCoroutine(OnClickLoginBtn()));
|
|
rigistBtn.onClick.AddListener(OnClickRigistBtn);
|
|
rigBtn.onClick.AddListener(() => StartCoroutine(OnClickRegBtn()));
|
|
}
|
|
|
|
public IEnumerator OnClickRegBtn()
|
|
{
|
|
loginbody body = new loginbody
|
|
{
|
|
userName = userNameField.text,
|
|
password = passwordField.text,
|
|
verifyCode = int.Parse(verifyCodeField.text)
|
|
};
|
|
|
|
string jsonBody = JsonUtility.ToJson(body);
|
|
using (UnityWebRequest webRequest = new UnityWebRequest("http://121.40.42.41:8080/snail/user/register", "POST"))
|
|
{
|
|
// 创建并设置上传和下载处理器
|
|
webRequest.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonBody));
|
|
webRequest.uploadHandler.contentType = "application/json";
|
|
webRequest.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
// 发送请求
|
|
yield return webRequest.SendWebRequest();
|
|
|
|
// 检查请求结果
|
|
if (webRequest.result == UnityWebRequest.Result.Success)
|
|
{
|
|
string registerResponse = webRequest.downloadHandler.text;
|
|
Debug.Log("Register Response: " + registerResponse);
|
|
|
|
SceneManager.LoadScene(1);
|
|
|
|
|
|
// 可在此处解析注册响应并进行后续处理
|
|
// 比如解析返回的token并触发事件
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("注册失败! " + webRequest.error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnClickRigistBtn()
|
|
{
|
|
yzm.gameObject.SetActive(true);
|
|
mm.gameObject.SetActive(true);
|
|
qrmm.gameObject.SetActive(true);
|
|
yqm.gameObject.SetActive(true);
|
|
logBtn.gameObject.SetActive(false);
|
|
regbtn.gameObject.SetActive(true);
|
|
regImg.gameObject.SetActive(true);
|
|
pwdBtn.gameObject.SetActive(false);
|
|
yzmBtn.gameObject.SetActive(false);
|
|
}
|
|
|
|
|
|
|
|
//点击登录按钮
|
|
private IEnumerator OnClickLoginBtn()
|
|
{
|
|
loginbody body = new loginbody
|
|
{
|
|
userName = userNameField.text,
|
|
password = passwordField.text,
|
|
verifyCode = int.Parse(verifyCodeField.text)
|
|
};
|
|
|
|
string jsonBody = JsonUtility.ToJson(body);
|
|
using (UnityWebRequest webRequest = new UnityWebRequest("http://121.40.42.41:8080/snail/user/login", "POST"))
|
|
{
|
|
// 创建和设置处理程序
|
|
webRequest.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonBody));
|
|
webRequest.uploadHandler.contentType = "application/json";
|
|
webRequest.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
// 发送请求
|
|
yield return webRequest.SendWebRequest();
|
|
|
|
// 检查请求结果
|
|
if (webRequest.result == UnityWebRequest.Result.Success)
|
|
{
|
|
string loginResponse = webRequest.downloadHandler.text;
|
|
Debug.Log("LoginAndGetToken登录:" + loginResponse);
|
|
string token = getToken(loginResponse);
|
|
OnTokenReceived?.Invoke(token);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Error: " + webRequest.error);
|
|
}
|
|
}
|
|
}
|
|
|
|
//点击验证码登录按钮
|
|
private void OnClickYzmBtn()
|
|
{
|
|
pwdBtn.transform.position -= new Vector3(0f, 26.4f, 0f);
|
|
yzmBtn.transform.position += new Vector3(0f, 26.4f, 0f);
|
|
yzm.gameObject.SetActive(true);
|
|
mm.gameObject.SetActive(false);
|
|
}
|
|
|
|
//点击密码登录按钮
|
|
private void OnClickPwdBtn()
|
|
{
|
|
pwdBtn.transform.position += new Vector3(0f, 26.4f, 0f);
|
|
yzmBtn.transform.position -= new Vector3(0f, 26.4f, 0f);
|
|
yzm.gameObject.SetActive(false);
|
|
mm.gameObject.SetActive(true);
|
|
}
|
|
|
|
|
|
} |