Login.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. Debug.Log(jsonValue);
  81. StartCoroutine(SendListCoroutine(jsonValue));
  82. }
  83. public IEnumerator SendListCoroutine(string jsonValue)
  84. {
  85. Debug.Log(phoneNumber);
  86. WWWForm form = new WWWForm();
  87. form.AddField("terminal-token", GetToken());
  88. form.AddField("phoneNumber", phoneNumber);
  89. form.AddField("array", jsonValue);
  90. UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/save-train-result", form);
  91. yield return unityWebRequest.SendWebRequest();
  92. Debug.Log(unityWebRequest.downloadHandler.text);
  93. SaveLog("返回数据" + unityWebRequest.downloadHandler.text);
  94. sendListCallBack?.Invoke(unityWebRequest.downloadHandler.text);
  95. }
  96. public void InputPhoneNumber(string phoneNumber)
  97. {
  98. this.phoneNumber = phoneNumber;
  99. }
  100. public void GetList()
  101. {
  102. StartCoroutine(GetListCoroutine());
  103. }
  104. public void SaveLog(string value)
  105. {
  106. string date = ((int)((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 100000000000)).ToString() + "0000";
  107. string time = System.DateTime.Now.ToString().ToString();
  108. File.AppendAllText(Application.persistentDataPath + "/" + date + ".txt", time + "__" + value + "\n");
  109. }
  110. public IEnumerator GetListCoroutine()
  111. {
  112. Debug.Log(phoneNumber);
  113. SaveLog("获取推荐课程");
  114. WWWForm form = new WWWForm();
  115. form.AddField("terminal-token", GetToken());
  116. form.AddField("phoneNumber", phoneNumber);
  117. UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/get-train-state", form);
  118. yield return unityWebRequest.SendWebRequest();
  119. loginGameObject.SetActive(false);
  120. JObject keyValuePairs = JObject.Parse(unityWebRequest.downloadHandler.text);
  121. string code = keyValuePairs["code"].ToString();
  122. Debug.Log(unityWebRequest.downloadHandler.text);
  123. SaveLog("返回数据" + unityWebRequest.downloadHandler.text);
  124. if (code != "200")
  125. {
  126. }
  127. else
  128. {
  129. firseState = keyValuePairs["data"]["state"].ToString();
  130. switch (firseState)
  131. {
  132. case "0":
  133. tj1.SetActive(false);
  134. tj2.SetActive(false);
  135. break;
  136. case "1":
  137. tj1.SetActive(true);
  138. tj2.SetActive(false);
  139. break;
  140. default:
  141. tj1.SetActive(false);
  142. tj2.SetActive(true);
  143. break;
  144. }
  145. }
  146. }
  147. private string GetToken()
  148. {
  149. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  150. keyValuePairs.Add("clientId", id);
  151. keyValuePairs.Add("expiresTime", (DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000 + 200000);
  152. Debug.Log(keyValuePairs["expiresTime"]);
  153. string jsonString = JsonConvert.SerializeObject(keyValuePairs);
  154. string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString));
  155. string token = System.Web.HttpUtility.UrlEncode(base64 + "." + ComputeHmacSha256(base64, key));
  156. token = Regex.Replace(token, "%3d", "%3D");
  157. SaveLog("获取token:" + token);
  158. return token;
  159. }
  160. private string ComputeHmacSha256(string message, string secret)
  161. {
  162. var keyBytes = Encoding.UTF8.GetBytes(secret);
  163. var messageBytes = Encoding.UTF8.GetBytes(message);
  164. using (var hmac = new HMACSHA256(keyBytes))
  165. {
  166. var hash = hmac.ComputeHash(messageBytes);
  167. string code = Convert.ToBase64String(hash);
  168. code = Regex.Replace(code, "\\+", "-");
  169. Debug.Log(code);
  170. return code;
  171. }
  172. }
  173. }
  174. public class ArrayData
  175. {
  176. /// <summary>
  177. /// 错误数量
  178. /// </summary>
  179. public int wrongCount;
  180. /// <summary>
  181. /// 正确数量
  182. /// </summary>
  183. public int correctCount;
  184. /// <summary>
  185. /// 课程类型
  186. /// </summary>
  187. public int type;
  188. /// <summary>
  189. /// 错误选择情况
  190. /// </summary>
  191. public List<WrongQuestionsItem> wrongQuestions;
  192. }
  193. public class WrongQuestionsItem
  194. {
  195. /// <summary>
  196. /// 错误题号
  197. /// </summary>
  198. public string questionId;
  199. /// <summary>
  200. /// 错误选项
  201. /// </summary>
  202. public string userWrongOption;
  203. }
粤ICP备19079148号