using AR.Core; using LitJson; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; using TMPro; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; namespace Assets { public class DlgLogin : BasePanel { public TMP_InputField inputField; public Button confirmBtn; WhiteList whiteList; string url = "http://172.18.10.189:8000/swagger/index.html"; public override void OnCloseWindow() { } public override void OnDestroyWindow() { } public override void OnInitWindow() { LoadJson(); GetMacAddress(); confirmBtn.onClick.AddListener(async () => { if (whiteList.table.Contains(inputField.text)) { StartCoroutine(GetSecretKey()); } else { await UIManager.Instance.ShowUI(WindowID.DlgLoginFaild); } }); } public override void OnShowWindow() { } IEnumerator GetSecretKey() { string url = "http://172.18.10.189:8000/api/Account/SecretKey"; AccountMsg accountMsg = new AccountMsg(); accountMsg.Account = inputField.text; string json = JsonMapper.ToJson(accountMsg); byte[] postBytes = System.Text.Encoding.Default.GetBytes(json); UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST); request.uploadHandler =new UploadHandlerRaw(postBytes); request.SetRequestHeader("Content-Type", "application/json"); request.timeout = 5; yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { Debug.Log(request.downloadHandler.text); GetSecretKeyResult result = JsonMapper.ToObject(request.downloadHandler.text); StartCoroutine(Login(result.message)); } else { Debug.Log(request.error); } } IEnumerator Login(string secretKey) { string url = "http://172.18.10.189:8000/api/Account/Login"; Userdata userInfo = new Userdata(); userInfo.account = inputField.text; userInfo.mac = DesEncryptHelper.Encrypt(GetMacAddress(), secretKey); string json = JsonMapper.ToJson(userInfo); byte[] postBytes = System.Text.Encoding.Default.GetBytes(json); UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST); request.uploadHandler = new UploadHandlerRaw(postBytes); request.SetRequestHeader("Content-Type", "application/json"); request.timeout = 5; yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { Debug.Log(request.downloadHandler.text); UIManager.Instance.HideUI(WindowID.DlgLogin); UIManager.Instance.ShowUI(WindowID.FaceBuildPanel); } else { Debug.Log(request.error); } } private string GetMacAddress() { string physicalAddress = ""; NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adaper in nice) { if (adaper.Description == "en0") { physicalAddress = adaper.GetPhysicalAddress().ToString(); break; } else { physicalAddress = adaper.GetPhysicalAddress().ToString(); if (physicalAddress != "") { break; }; } } return physicalAddress; } public async void LoadJson() { var txtAsset = await YooAssetManager.Instance.LoadAsset("Assets/Bundles/Config/WhiteList"); whiteList = JsonMapper.ToObject(txtAsset.text); } } /// /// 账号和Mac地址 /// class Userdata { public string account; public string mac; } /// /// 输入账号 /// class AccountMsg { public string Account; } struct GetSecretKeyResult { public int code; public string message; public string data; } class WhiteList { public string[] table; } }