| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Text.RegularExpressions;
- using System.Security.Cryptography;
- using System.Text;
- using UnityEngine.Networking;
- using UnityEngine.XR;
- public class Login : SingletonBaseMono<Login>
- {
- public GameObject loginGameObject;
- public GameObject tj1;
- public GameObject tj2;
- public string id;
- public string key;
- private static long Jan1st1970Ms = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc).Ticks;
- [Header("朝向学生视角")]
- public Vector3 startPoint;
- public Vector3 startVector;
- [Header("朝向黑板视角")]
- public Vector3 endPoint;
- public Vector3 endVecotr;
- private string phoneNumber;
- /// <summary>
- /// 推荐课程
- /// </summary>
- private string firseState;
- private Action<string> sendListCallBack;
- private InputDevice leftHandController;
- private InputDevice rightHandController;
- void Start()
- {
- leftHandController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
- rightHandController = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 axis;
- if (leftHandController.TryGetFeatureValue(CommonUsages.primary2DAxis, out axis))
- {
- transform.Translate(new Vector3(axis.x, 0, axis.y) * Time.deltaTime);
- }
- if (rightHandController.TryGetFeatureValue(CommonUsages.primary2DAxis, out axis))
- {
- transform.Translate(new Vector3(axis.x, 0, axis.y) * Time.deltaTime);
- }
- if (transform.position.x < -5)
- {
- transform.position = new Vector3(-5, transform.position.y, transform.position.z);
- }
- if (transform.position.x > 5)
- {
- transform.position = new Vector3(5, transform.position.y, transform.position.z);
- }
- if (transform.position.z < -10)
- {
- transform.position = new Vector3(transform.position.x, transform.position.y, -10);
- }
- if (transform.position.z > 10)
- {
- transform.position = new Vector3(transform.position.x, transform.position.y, 10);
- }
- }
- /// <summary>
- /// 发送成绩
- /// </summary>
- public void SendList(ArrayData arrayData, Action<string> sendListCallBack)
- {
- this.sendListCallBack = sendListCallBack;
- List<ArrayData> arrays = new List<ArrayData>();
- arrays.Add(arrayData);
- string jsonValue = JsonConvert.SerializeObject(arrays);
- StartCoroutine(SendListCoroutine(jsonValue));
- }
- public IEnumerator SendListCoroutine(string jsonValue)
- {
- Debug.Log(phoneNumber);
- WWWForm form = new WWWForm();
- form.AddField("terminal-token", GetToken());
- form.AddField("phoneNumber", phoneNumber);
- form.AddField("array", jsonValue);
- UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/save-train-result", form);
- yield return unityWebRequest.SendWebRequest();
- Debug.Log(unityWebRequest.downloadHandler.text);
- sendListCallBack?.Invoke(unityWebRequest.downloadHandler.text);
- }
- public void InputPhoneNumber(string phoneNumber)
- {
- this.phoneNumber = phoneNumber;
- }
- public void GetList()
- {
- StartCoroutine(GetListCoroutine());
- }
- public IEnumerator GetListCoroutine()
- {
- Debug.Log(phoneNumber);
- WWWForm form = new WWWForm();
- form.AddField("terminal-token", GetToken());
- form.AddField("phoneNumber", phoneNumber);
- UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://gdsyzxlwxx.dds-ai.cn/jxpc-server/third-api/vr/api/get-train-state", form);
- yield return unityWebRequest.SendWebRequest();
- loginGameObject.SetActive(false);
- JObject keyValuePairs = JObject.Parse(unityWebRequest.downloadHandler.text);
- string code = keyValuePairs["code"].ToString();
- Debug.Log(unityWebRequest.downloadHandler.text);
- if (code != "200")
- {
- }
- else
- {
- firseState = keyValuePairs["data"]["state"].ToString();
- switch (firseState)
- {
- case "0":
- tj1.SetActive(false);
- tj2.SetActive(false);
- break;
- case "1":
- tj1.SetActive(true);
- tj2.SetActive(false);
- break;
- default:
- tj1.SetActive(false);
- tj2.SetActive(true);
- break;
- }
- }
- }
- private string GetToken()
- {
- Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
- keyValuePairs.Add("clientId", id);
- keyValuePairs.Add("expiresTime", (DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000 + 200000);
- Debug.Log(keyValuePairs["expiresTime"]);
- string jsonString = JsonConvert.SerializeObject(keyValuePairs);
- string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString));
- string token = System.Web.HttpUtility.UrlEncode(base64 + "." + ComputeHmacSha256(base64, key));
- token = Regex.Replace(token, "%3d", "%3D");
- return token;
- }
- private string ComputeHmacSha256(string message, string secret)
- {
- var keyBytes = Encoding.UTF8.GetBytes(secret);
- var messageBytes = Encoding.UTF8.GetBytes(message);
- using (var hmac = new HMACSHA256(keyBytes))
- {
- var hash = hmac.ComputeHash(messageBytes);
- return Convert.ToBase64String(hash);
- }
- }
- }
- public class ArrayData
- {
- /// <summary>
- /// 错误数量
- /// </summary>
- public int wrongCount;
- /// <summary>
- /// 正确数量
- /// </summary>
- public int correctCount;
- /// <summary>
- /// 错误选择情况
- /// </summary>
- public List<WrongQuestionsItem> wrongQuestions;
- }
- public class WrongQuestionsItem
- {
- /// <summary>
- /// 错误题号
- /// </summary>
- public string questionId;
- /// <summary>
- /// 错误选项
- /// </summary>
- public string userWrongOption;
- }
|