221 lines
6.7 KiB
C#
Raw Normal View History

2025-03-31 10:10:29 +08:00
using Newtonsoft.Json;
using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
2025-04-09 15:46:56 +08:00
public class LYTWebGLHelper : MonoSingleton<LYTWebGLHelper>
2025-03-31 10:10:29 +08:00
{
2025-04-09 13:59:32 +08:00
private LYTWebGLHelper() { }
2025-03-31 10:10:29 +08:00
public class LabData
{
public string GUID;
public string ExpID;
2025-04-09 15:46:56 +08:00
public string HOST = string.Empty;
2025-03-31 10:10:29 +08:00
public string PARA1;
public string PARA;
public string PARA2;
}
2025-04-09 15:46:56 +08:00
#if UNITY_WEBGL
2025-03-31 10:10:29 +08:00
[DllImport("__Internal")]
2025-04-10 09:31:37 +08:00
private static extern IntPtr GetURLParameter(string name);
2025-04-09 15:46:56 +08:00
#endif
2025-03-31 10:10:29 +08:00
string token = string.Empty;
LabData labData = new LabData();
[SerializeField]
private TextAsset RSA;
private const int RsaKeySize = 2048;
public string uploadUrl;
2025-04-09 15:46:56 +08:00
private void Awake()
{
Init();
}
2025-03-31 10:10:29 +08:00
public void Init()
{
2025-04-10 09:31:37 +08:00
#if UNITY_WEBGL && !UNITY_EDITOR
var paramPtr = GetURLParameter("token");
if (paramPtr != IntPtr.Zero)
{
string value = Marshal.PtrToStringUTF8(paramPtr);
//token = value.Replace("%2B", "+");
token = value.Replace(" ", "+");
Marshal.FreeHGlobal(paramPtr); // <20>ͷŷ<CDB7><C5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
}
Debug.LogError("<22><>ȡ<EFBFBD>ģ<EFBFBD>" + token);
2025-04-09 15:46:56 +08:00
#endif
2025-04-10 09:31:37 +08:00
2025-04-09 15:46:56 +08:00
RSA = Resources.Load<TextAsset>("LYTWebGL/RSA");
if (string.IsNullOrEmpty(token) == false)
{
string urlData = Decrypt(token);
var datas = urlData.Split("&");
labData.GUID = datas[0];
labData.ExpID = datas[1];
labData.HOST = datas[2];
labData.PARA1 = datas[3];
labData.PARA = datas[4];
labData.PARA2 = datas[5];
}
2025-04-24 14:54:32 +08:00
uploadUrl = Path.Combine(labData.HOST, "public/Exp/AddScore/");
2025-03-31 10:10:29 +08:00
}
/// <summary>
/// Decrypts encrypted text given a RSA private key file path.<2E><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD> RSA ˽Կ<CBBD>ļ<EFBFBD><C4BC><EFBFBD>
/// </summary>
/// <param name="encryptedText"><3E><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="pathToPrivateKey"><3E><><EFBFBD>ڼ<EFBFBD><DABC>ܵ<EFBFBD>˽Կ·<D4BF><C2B7>.</param>
/// <returns>δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5>ַ<EFBFBD><D6B7><EFBFBD></returns>
public string Decrypt(string encryptedText)
{
using (var rsa = new RSACryptoServiceProvider(RsaKeySize))
{
try
{
string privateXmlKey = RSA.text;
rsa.FromXmlString(privateXmlKey);
Debug.Log("encryptedText " + encryptedText);
var bytesEncrypted = Convert.FromBase64String(encryptedText);
string b = Encoding.UTF8.GetString(bytesEncrypted);
Debug.Log("byte " + b);
//var bytesPlainText = rsa.Decrypt(bytesEncrypted, false);
var bytesPlainText = rsa.Decrypt(bytesEncrypted, false);
Debug.Log("bytesPlainText " + bytesPlainText);
return System.Text.Encoding.UTF8.GetString(bytesPlainText);
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
}
2025-04-24 14:54:32 +08:00
public void UpLoadData(int totalScore, Expstepvtwolist[] datas)
2025-03-31 10:10:29 +08:00
{
2025-04-24 14:54:32 +08:00
if (string.IsNullOrEmpty(labData.GUID))
{
return;
}
2025-03-31 10:10:29 +08:00
var data = new UploadData();
data.GUID = labData.GUID;
2025-04-09 15:46:56 +08:00
int expId = 0;
int.TryParse(labData.ExpID, out expId);
data.ExpID = expId;
2025-03-31 10:10:29 +08:00
data.score = totalScore;
data.flag = true;
2025-04-24 14:54:32 +08:00
//var list = new List<Expstepvtwolist>();
//for (int i = 0; i < stepNames.Count; i++)
//{
// var step = new Expstepvtwolist();
// step.ExpStepName = stepNames[i];
// step.maxScore = maxScore[i];
// step.score = score[i];
// step.seq = i + 1;
// list.Add(step);
//}
data.ExpStepVTwoList = datas;
2025-03-31 10:10:29 +08:00
2025-04-09 15:46:56 +08:00
StartCoroutine(SendScore(JsonConvert.SerializeObject(data)));
2025-03-31 10:10:29 +08:00
}
2025-04-09 13:59:32 +08:00
IEnumerator SendScore(string json, UnityAction<string> action = null)
2025-03-31 10:10:29 +08:00
{
if (string.IsNullOrEmpty(uploadUrl))
{
Debug.LogError("<22>ϴ<EFBFBD><CFB4>ӿڵ<D3BF>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>:" + uploadUrl);
yield break;
}
2025-04-10 09:31:37 +08:00
Debug.LogError("<22>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:" + json);
2025-03-31 10:10:29 +08:00
using (UnityWebRequest request = new UnityWebRequest(uploadUrl, "POST"))
{
request.SetRequestHeader("Content-Type", "application/json");
request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(json));
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
if (request.result == UnityWebRequest.Result.ConnectionError ||
request.result == UnityWebRequest.Result.ProtocolError)
{
2025-04-09 15:46:56 +08:00
Debug.LogError($"Upload failed: {request.uri}");
2025-03-31 10:10:29 +08:00
Debug.LogError($"Upload failed: {request.error}");
Debug.LogError($"Response Code: {request.responseCode}");
}
else
{
Debug.Log("Upload complete!");
Debug.Log($"Response Code: {request.responseCode}");
Debug.Log($"Server Response: {request.downloadHandler.text}");
Response response = JsonConvert.DeserializeObject<Response>(request.downloadHandler.text);
Debug.LogError(response.msg);
2025-04-09 13:59:32 +08:00
action?.Invoke(response.msg);
2025-03-31 10:10:29 +08:00
}
}
}
2025-04-09 13:59:32 +08:00
public class UploadData
{
public string GUID { get; set; }
// ʵ<><CAB5> ID
public int ExpID { get; set; }
// <20>ɼ<EFBFBD>
public int score { get; set; }
// <20><>־λ<D6BE><CEBB>Ĭ<EFBFBD><C4AC>ֵ true
public bool flag { get; set; }
// ʵ<><EFBFBD><E9B2BD><EFBFBD>б<EFBFBD>
public Expstepvtwolist[] ExpStepVTwoList { get; set; }
}
2025-03-31 10:10:29 +08:00
2025-04-09 13:59:32 +08:00
public class Expstepvtwolist
{
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public int seq { get; set; }
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public string ExpStepName = "";
// ʵ<><EFBFBD><E9B2BD>״̬
public string StepState = "";
// ʵ<><EFBFBD>ʼʱ<CABC><CAB1>
2025-04-24 14:54:32 +08:00
public string startTime;
2025-04-09 13:59:32 +08:00
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2025-04-24 14:54:32 +08:00
public string endTime;
2025-04-09 13:59:32 +08:00
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
public int expectTime = 0;
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>0 ~100<30><30><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD>
public int maxScore = 100;
// ʵ<><EFBFBD><E9B2BD><EFBFBD>÷֣<C3B7>0 ~100<30><30><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD>
public int score = 0;
// ʵ<><EFBFBD><E9B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public int repeatCount = 1;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۣ<EFBFBD>200 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public string evaluation = "";
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD>ͣ<EFBFBD>200 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public string scoringModel = "";
// <20><>ע
public string remarks { get; set; }
}
2025-03-31 10:10:29 +08:00
2025-04-09 13:59:32 +08:00
public class Response
{
public string msg;
public bool success;
}
2025-03-31 10:10:29 +08:00
}
2025-04-09 13:59:32 +08:00