Login.cs 5.0 KB

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