Login.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Text.RegularExpressions;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using UnityEngine.Networking;
  11. public class Login : MonoBehaviour
  12. {
  13. public GameObject tj1;
  14. public GameObject tj2;
  15. public string id;
  16. public string key;
  17. private static long Jan1st1970Ms = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc).Ticks;
  18. private string phoneNumber;
  19. /// <summary>
  20. /// ÍÆ¼ö¿Î³Ì
  21. /// </summary>
  22. private string firseState;
  23. void Start()
  24. {
  25. }
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. }
  30. public void InputPhoneNumber(string phoneNumber)
  31. {
  32. this.phoneNumber = phoneNumber;
  33. }
  34. public void GetList()
  35. {
  36. StartCoroutine(GetListCoroutine());
  37. }
  38. public IEnumerator GetListCoroutine()
  39. {
  40. WWWForm form = new WWWForm();
  41. form.AddField("terminal-token", GetToken());
  42. form.AddField("phoneNumber", phoneNumber);
  43. UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/get-train-state", form);
  44. yield return unityWebRequest.SendWebRequest();
  45. gameObject.SetActive(false);
  46. JObject keyValuePairs = JObject.Parse(unityWebRequest.downloadHandler.text);
  47. string code = keyValuePairs["code"].ToString();
  48. if (code != "200")
  49. {
  50. }
  51. else
  52. {
  53. firseState = keyValuePairs["data"]["state"].ToString();
  54. switch (firseState)
  55. {
  56. case "0":
  57. tj1.SetActive(false);
  58. tj2.SetActive(false);
  59. break;
  60. case "1":
  61. tj1.SetActive(true);
  62. tj2.SetActive(false);
  63. break;
  64. default:
  65. tj1.SetActive(false);
  66. tj2.SetActive(true);
  67. break;
  68. }
  69. }
  70. }
  71. private string GetToken()
  72. {
  73. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  74. keyValuePairs.Add("clientId", id);
  75. keyValuePairs.Add("expiresTime", (DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000 + 100000);
  76. Debug.Log(keyValuePairs["expiresTime"]);
  77. string jsonString = JsonConvert.SerializeObject(keyValuePairs);
  78. string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString));
  79. string token = System.Web.HttpUtility.UrlEncode(base64 + "." + ComputeHmacSha256(base64, key));
  80. token = Regex.Replace(token, "%3d", "%3D");
  81. return token;
  82. }
  83. private string ComputeHmacSha256(string message, string secret)
  84. {
  85. var keyBytes = Encoding.UTF8.GetBytes(secret);
  86. var messageBytes = Encoding.UTF8.GetBytes(message);
  87. using (var hmac = new HMACSHA256(keyBytes))
  88. {
  89. var hash = hmac.ComputeHash(messageBytes);
  90. return Convert.ToBase64String(hash);
  91. }
  92. }
  93. }
粤ICP备19079148号