Login.cs 4.7 KB

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