Login.cs 6.8 KB

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