123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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<GetSecretKeyResult>(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<TextAsset>("Assets/Bundles/Config/WhiteList");
- whiteList = JsonMapper.ToObject<WhiteList>(txtAsset.text);
- }
- }
- /// <summary>
- /// 账号和Mac地址
- /// </summary>
- class Userdata
- {
- public string account;
- public string mac;
- }
- /// <summary>
- /// 输入账号
- /// </summary>
- class AccountMsg
- {
- public string Account;
- }
- struct GetSecretKeyResult
- {
- public int code;
- public string message;
- public string data;
- }
- class WhiteList
- {
- public string[] table;
- }
- }
|