Login.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 : SingletonBaseMono<Login>
  12. {
  13. public GameObject loginGameObject;
  14. public GameObject tj1;
  15. public GameObject tj2;
  16. public string id;
  17. public string key;
  18. private static long Jan1st1970Ms = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc).Ticks;
  19. private string phoneNumber;
  20. /// <summary>
  21. /// 推荐课程
  22. /// </summary>
  23. private string firseState;
  24. private Action<string> sendListCallBack;
  25. void Start()
  26. {
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. Debug.Log(phoneNumber);
  32. }
  33. /// <summary>
  34. /// 发送成绩
  35. /// </summary>
  36. public void SendList(ArrayData arrayData, Action<string> sendListCallBack)
  37. {
  38. this.sendListCallBack = sendListCallBack;
  39. List<ArrayData> arrays = new List<ArrayData>();
  40. arrays.Add(arrayData);
  41. string jsonValue = JsonConvert.SerializeObject(arrays);
  42. StartCoroutine(SendListCoroutine(jsonValue));
  43. }
  44. public IEnumerator SendListCoroutine(string jsonValue)
  45. {
  46. Debug.Log(phoneNumber);
  47. WWWForm form = new WWWForm();
  48. form.AddField("terminal-token", GetToken());
  49. form.AddField("phoneNumber", phoneNumber);
  50. form.AddField("array", jsonValue);
  51. UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/save-train-result", form);
  52. yield return unityWebRequest.SendWebRequest();
  53. Debug.Log(unityWebRequest.downloadHandler.text);
  54. sendListCallBack?.Invoke(unityWebRequest.downloadHandler.text);
  55. }
  56. public void InputPhoneNumber(string phoneNumber)
  57. {
  58. this.phoneNumber = phoneNumber;
  59. }
  60. public void GetList()
  61. {
  62. StartCoroutine(GetListCoroutine());
  63. }
  64. public IEnumerator GetListCoroutine()
  65. {
  66. Debug.Log(phoneNumber);
  67. WWWForm form = new WWWForm();
  68. form.AddField("terminal-token", GetToken());
  69. form.AddField("phoneNumber", phoneNumber);
  70. UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/get-train-state", form);
  71. yield return unityWebRequest.SendWebRequest();
  72. loginGameObject.SetActive(false);
  73. JObject keyValuePairs = JObject.Parse(unityWebRequest.downloadHandler.text);
  74. string code = keyValuePairs["code"].ToString();
  75. Debug.Log(unityWebRequest.downloadHandler.text);
  76. if (code != "200")
  77. {
  78. }
  79. else
  80. {
  81. firseState = keyValuePairs["data"]["state"].ToString();
  82. switch (firseState)
  83. {
  84. case "0":
  85. tj1.SetActive(false);
  86. tj2.SetActive(false);
  87. break;
  88. case "1":
  89. tj1.SetActive(true);
  90. tj2.SetActive(false);
  91. break;
  92. default:
  93. tj1.SetActive(false);
  94. tj2.SetActive(true);
  95. break;
  96. }
  97. }
  98. }
  99. private string GetToken()
  100. {
  101. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  102. keyValuePairs.Add("clientId", id);
  103. keyValuePairs.Add("expiresTime", (DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000 + 200000);
  104. Debug.Log(keyValuePairs["expiresTime"]);
  105. string jsonString = JsonConvert.SerializeObject(keyValuePairs);
  106. string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString));
  107. string token = System.Web.HttpUtility.UrlEncode(base64 + "." + ComputeHmacSha256(base64, key));
  108. token = Regex.Replace(token, "%3d", "%3D");
  109. return token;
  110. }
  111. private string ComputeHmacSha256(string message, string secret)
  112. {
  113. var keyBytes = Encoding.UTF8.GetBytes(secret);
  114. var messageBytes = Encoding.UTF8.GetBytes(message);
  115. using (var hmac = new HMACSHA256(keyBytes))
  116. {
  117. var hash = hmac.ComputeHash(messageBytes);
  118. return Convert.ToBase64String(hash);
  119. }
  120. }
  121. }
  122. public class ArrayData
  123. {
  124. /// <summary>
  125. /// 错误数量
  126. /// </summary>
  127. public int wrongCount;
  128. /// <summary>
  129. /// 正确数量
  130. /// </summary>
  131. public int correctCount;
  132. /// <summary>
  133. /// 错误选择情况
  134. /// </summary>
  135. public List<WrongQuestionsItem> wrongQuestions;
  136. }
  137. public class WrongQuestionsItem
  138. {
  139. /// <summary>
  140. /// 错误题号
  141. /// </summary>
  142. public string questionId;
  143. /// <summary>
  144. /// 错误选项
  145. /// </summary>
  146. public string userWrongOption;
  147. }
粤ICP备19079148号