Login.cs 6.2 KB

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