NewN_UAVPlane/Assets/Temp/Scripts/Login_panl.cs

161 lines
4.9 KiB
C#

using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using XFrame.Core.UI;
public class Login_panl : MonoBehaviour
{
string url = "http://172.16.1.254:48888";//wed端的地址
[SerializeField]//外界的类拿不到信息
public UserInfo user = new UserInfo();
public InputField username_input;//用户名输入
public InputField password_input;//密码输入
public Button login_btn;//登入按钮
public Text tips_txt;//提示文本
public GameObject main_panl;//激活主页面
public RectTransform login_panl;//失活主页面
public RectTransform Main_page;//背景界面失活
void Start()
{
login_btn.onClick.AddListener(Login);
TZ();
}
private void TZ()
{
if (GameMain.tiao == true)
{
}
else
{
login_panl.gameObject.SetActive(false);
main_panl.gameObject.SetActive(true);
Main_page.gameObject.SetActive(false);
GameMain.tiao = true;
}
}
private void Login()
{
if (username_input.text.Length == 0 || password_input.text.Length == 0)
{
tips_txt.text = "请输入密码或者账号";
Invoke("Clearaway", 1.5f);
}
else
{
string account = username_input.text;
string password = password_input.text;
string url = this.url + "/Handler/User.ashx?action=login";
url += "&login_name=";
url += account;
url += "&password=";
url += password;
StartCoroutine(Post(url, (b, s) =>
{
if (b)
{
Debug.Log(s);
LoginRoot rot = new LoginRoot();
rot = JsonMapper.ToObject<LoginRoot>(s);
//JsonData jd = JsonMapper.ToObject(s);
//string state = jd["state"].ToString();
//string message = jd["message"].ToString();
if (rot.state)
{
//string userdate = jd["data"].ToString();
//UserInfo user = new UserInfo();
user = rot.data;
//user.user_id = jd["data"]["user_id"].ToString();
//user.login_name = jd["data"]["login_name"].ToString();
//user.real_name = jd["data"]["real_name"].ToString();
//user.role_code = jd["data"]["role_code"].ToString();
//user.real_name = jd["data"]["real_name"].ToString() ;
PlayerPrefs.SetString("user_id", user.user_id);
login_panl.gameObject.SetActive(false);
main_panl.gameObject.SetActive(true);
Main_page.gameObject.SetActive(false);
}
else
{
tips_txt.text = "登陆失败:" + rot.message;
Invoke("Clearaway", 1.5f);
}
}
}));
}
}
private void Clearaway()
{
tips_txt.text = null;
}
public IEnumerator Post(string url, System.Action<bool, string> action)
{
WWWForm form = new WWWForm();
UnityWebRequest request = UnityWebRequest.Post(url, form);
yield return request.SendWebRequest();//发送HTTP等待请求响应
if ((request.isNetworkError) || (request.isHttpError))//旧版的检查网络是否发生错误
{
// Debug.LogError("登录失败!");
tips_txt.text = "登陆失败!";
action(false, null);
}
else
{
if (!string.IsNullOrEmpty(request.downloadHandler.text))//相应服务器信息不为空
action(true, request.downloadHandler.text);//request.downloadHandler.text服务器相应的信息
else
action(false, null);
}
}
}
//public class LoginRoot
//{
// /// <summary>
// ///
// /// </summary>
// public bool state { get; set; }
// /// <summary>
// ///
// /// </summary>
// public string message { get; set; }
// /// <summary>
// ///
// /// </summary>
// public UserInfo data { get; set; }
//}
//public class UserInfo
//{
// /// <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; }
//}