using Newtonsoft.Json; using System; using System.Collections; using System.Net.Http; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; [AddComponentMenu("EncryptionScript/密钥")] public class EncryptionScript : MonoBehaviour { public string encryptionKey = "defaultKey"; private string url_key = "http://172.16.1.253:8087/priceRule.json"; [SerializeField] private string inputKey = null; /// /// 密钥是否过期 /// public static bool isOverdueKey = true; /// /// 密钥运行时间 /// private float elapsedTime = 0; /// /// 密钥有效时间 /// private float averageSecondsInMonth = 5270400f; private /*async*/ void Start() { //await GetKey();//读取JSON获取密钥 encryptionKey = (KeyGenerator.GenerateKey("123")); averageSecondsInMonth = 5f; } private /*async*/ void Update() { // 等待用户输入密钥 if (elapsedTime > averageSecondsInMonth || Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) { //await GetKey();//读取JSON更新密钥 if (elapsedTime > averageSecondsInMonth) { encryptionKey = (KeyGenerator.GenerateKey("123"));//更新密钥 isOverdueKey = true; } if (isOverdueKey && encryptionKey.Equals(inputKey)) { ResetTimer(); isOverdueKey = false; Debug.Log("密钥正确,继续运行程序"); } else if (isOverdueKey) { Debug.Log("密钥错误,程序无法运行"); Application.Quit(); // 密钥错误,退出应用程序 } } /*else if (isOverdueKey && Input.GetKeyDown(KeyCode.Backspace)) { if (inputKey.Length >= 1) inputKey = inputKey.Substring(0, inputKey.Length - 1); else return; } else if (isOverdueKey && Input.anyKeyDown) { inputKey += Input.inputString; }*/ else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) { if (isOverdueKey && Input.GetKeyDown(KeyCode.V)) { string pasteText = GUIUtility.systemCopyBuffer; // 获取系统粘贴板的内容 inputKey = pasteText; // 将粘贴的值赋给Input字段 } } } private void LateUpdate() { //elapsedTime += Time.deltaTime; //Debug.Log("elapsedTime: " + elapsedTime); } /// /// 重置计时器、秘钥有效期更新 /// public void ResetTimer() { elapsedTime = 0f; averageSecondsInMonth = 5270400f; } private IEnumerator Get_Api(string url_pice, Action callback) { string apiDoc_p = null; yield return StartCoroutine(GetApiDocumentation(url_pice, (result) => { apiDoc_p = result; })); //var apiDoc_p = await GetApiDocumentation(url_pice); callback.Invoke(apiDoc_p); } private IEnumerator GetApiDocumentation(string apiUrl, Action callback) { using (UnityWebRequest request = UnityWebRequest.Get(apiUrl)) { yield return request.SendWebRequest(); if (request.isNetworkError || request.isHttpError) { Debug.LogError(request.error); callback(null); } else { string responseBody = request.downloadHandler.text; // 处理响应内容 callback(responseBody); } } } [System.Serializable] public class Data { /// /// /// public double flat; /// /// /// public double tip; /// /// /// public double valley; /// /// /// public double peak; } [System.Serializable] public class Root { /// /// /// public Data data; /// /// /// public int code; /// /// /// public string msg; } public IEnumerator GetKey() { string apiData = null; yield return StartCoroutine(Get_Api(url_key, (result) => { apiData = result; })); try { //var apiData = await Get_Api(url_key); if (apiData != null) { Debug.Log("API CabinetList: " + apiData); Root root = JsonConvert.DeserializeObject(apiData); encryptionKey = root.data.flat.ToString(); } else { Debug.Log("API请求失败或返回空数据。"); } } catch (Exception e) { Debug.LogError("API请求期间发生错误: " + e.Message); } } }