| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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;
- public class Login : MonoBehaviour
- {
- 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;
- private string phoneNumber;
- /// <summary>
- /// ÍÆ¼ö¿Î³Ì
- /// </summary>
- private string firseState;
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- public void InputPhoneNumber(string phoneNumber)
- {
- this.phoneNumber = phoneNumber;
- }
- public void GetList()
- {
- StartCoroutine(GetListCoroutine());
- }
- public IEnumerator GetListCoroutine()
- {
- 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();
- gameObject.SetActive(false);
- JObject keyValuePairs = JObject.Parse(unityWebRequest.downloadHandler.text);
- string code = keyValuePairs["code"].ToString();
- 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 + 100000);
- 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);
- }
- }
- }
|