DlgLogin.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using AR.Core;
  2. using LitJson;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.NetworkInformation;
  10. using TMPro;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. using UnityEngine.UI;
  14. namespace Assets
  15. {
  16. public class DlgLogin : BasePanel
  17. {
  18. public TMP_InputField inputField;
  19. public Button confirmBtn;
  20. WhiteList whiteList;
  21. string url = "http://172.18.10.189:8000/swagger/index.html";
  22. public override void OnCloseWindow()
  23. {
  24. }
  25. public override void OnDestroyWindow()
  26. {
  27. }
  28. public override void OnInitWindow()
  29. {
  30. LoadJson();
  31. GetMacAddress();
  32. confirmBtn.onClick.AddListener(async () =>
  33. {
  34. if (whiteList.table.Contains(inputField.text))
  35. {
  36. StartCoroutine(GetSecretKey());
  37. }
  38. else
  39. {
  40. await UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
  41. }
  42. });
  43. }
  44. public override void OnShowWindow()
  45. {
  46. }
  47. IEnumerator GetSecretKey()
  48. {
  49. string url = "http://172.18.10.189:8000/api/Account/SecretKey";
  50. AccountMsg accountMsg = new AccountMsg();
  51. accountMsg.Account = inputField.text;
  52. string json = JsonMapper.ToJson(accountMsg);
  53. byte[] postBytes = System.Text.Encoding.Default.GetBytes(json);
  54. UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST);
  55. request.uploadHandler =new UploadHandlerRaw(postBytes);
  56. request.SetRequestHeader("Content-Type", "application/json");
  57. request.timeout = 5;
  58. yield return request.SendWebRequest();
  59. if (request.result == UnityWebRequest.Result.Success)
  60. {
  61. Debug.Log(request.downloadHandler.text);
  62. GetSecretKeyResult result = JsonMapper.ToObject<GetSecretKeyResult>(request.downloadHandler.text);
  63. StartCoroutine(Login(result.message));
  64. }
  65. else
  66. {
  67. Debug.Log(request.error);
  68. }
  69. }
  70. IEnumerator Login(string secretKey)
  71. {
  72. string url = "http://172.18.10.189:8000/api/Account/Login";
  73. Userdata userInfo = new Userdata();
  74. userInfo.account = inputField.text;
  75. userInfo.mac = DesEncryptHelper.Encrypt(GetMacAddress(), secretKey);
  76. string json = JsonMapper.ToJson(userInfo);
  77. byte[] postBytes = System.Text.Encoding.Default.GetBytes(json);
  78. UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST);
  79. request.uploadHandler = new UploadHandlerRaw(postBytes);
  80. request.SetRequestHeader("Content-Type", "application/json");
  81. request.timeout = 5;
  82. yield return request.SendWebRequest();
  83. if (request.result == UnityWebRequest.Result.Success)
  84. {
  85. Debug.Log(request.downloadHandler.text);
  86. UIManager.Instance.HideUI(WindowID.DlgLogin);
  87. UIManager.Instance.ShowUI(WindowID.FaceBuildPanel);
  88. }
  89. else
  90. {
  91. Debug.Log(request.error);
  92. }
  93. }
  94. private string GetMacAddress()
  95. {
  96. string physicalAddress = "";
  97. NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
  98. foreach (NetworkInterface adaper in nice)
  99. {
  100. if (adaper.Description == "en0")
  101. {
  102. physicalAddress = adaper.GetPhysicalAddress().ToString();
  103. break;
  104. }
  105. else
  106. {
  107. physicalAddress = adaper.GetPhysicalAddress().ToString();
  108. if (physicalAddress != "")
  109. {
  110. break;
  111. };
  112. }
  113. }
  114. return physicalAddress;
  115. }
  116. public async void LoadJson()
  117. {
  118. var txtAsset = await YooAssetManager.Instance.LoadAsset<TextAsset>("Assets/Bundles/Config/WhiteList");
  119. whiteList = JsonMapper.ToObject<WhiteList>(txtAsset.text);
  120. }
  121. }
  122. /// <summary>
  123. /// 账号和Mac地址
  124. /// </summary>
  125. class Userdata
  126. {
  127. public string account;
  128. public string mac;
  129. }
  130. /// <summary>
  131. /// 输入账号
  132. /// </summary>
  133. class AccountMsg
  134. {
  135. public string Account;
  136. }
  137. struct GetSecretKeyResult
  138. {
  139. public int code;
  140. public string message;
  141. public string data;
  142. }
  143. class WhiteList
  144. {
  145. public string[] table;
  146. }
  147. }