2025-04-09 15:46:56 +08:00

204 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
public class LYTWebGLHelper : MonoSingleton<LYTWebGLHelper>
{
private LYTWebGLHelper() { }
public class LabData
{
public string GUID;
public string ExpID;
public string HOST = string.Empty;
public string PARA1;
public string PARA;
public string PARA2;
}
#if UNITY_WEBGL
[DllImport("__Internal")]
private static extern string GetURLParameter(string name);
#endif
string token = string.Empty;
LabData labData = new LabData();
[SerializeField]
private TextAsset RSA;
private const int RsaKeySize = 2048;
public string uploadUrl;
private void Awake()
{
Init();
}
public void Init()
{
#if UNITY_WEBGL&&!UNITY_EDITOR
token = GetURLParameter("token").Replace("%2B", "+");
#endif
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];
}
uploadUrl = Path.Combine(labData.HOST, "host/public/Exp/AddScore/");
}
/// <summary>
/// Decrypts encrypted text given a RSA private key file path.给定路径的 RSA 私钥文件解
/// </summary>
/// <param name="encryptedText">加密的密文</param>
/// <param name="pathToPrivateKey">用于加密的私钥路径.</param>
/// <returns>未加密数据的字符串</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;
}
}
}
public void UpLoadData(int totalScore, List<string> stepNames, List<int> maxScore, List<int> score)
{
var data = new UploadData();
data.GUID = labData.GUID;
int expId = 0;
int.TryParse(labData.ExpID, out expId);
data.ExpID = expId;
data.score = totalScore;
data.flag = true;
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];
}
data.ExpStepVTwoList = list.ToArray();
StartCoroutine(SendScore(JsonConvert.SerializeObject(data)));
}
IEnumerator SendScore(string json, UnityAction<string> action = null)
{
if (string.IsNullOrEmpty(uploadUrl))
{
Debug.LogError("上传接口地址错误:" + uploadUrl);
yield break;
}
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();
// 处理响应
if (request.result == UnityWebRequest.Result.ConnectionError ||
request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError($"Upload failed: {request.uri}");
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);
action?.Invoke(response.msg);
}
}
}
public class UploadData
{
public string GUID { get; set; }
// 实验 ID
public int ExpID { get; set; }
// 成绩
public int score { get; set; }
// 标志位:默认值 true
public bool flag { get; set; }
// 实验步骤列表
public Expstepvtwolist[] ExpStepVTwoList { get; set; }
}
public class Expstepvtwolist
{
// 实验步骤序号
public int seq { get; set; }
// 实验步骤名称
public string ExpStepName = "";
// 实验步骤状态
public string StepState = "";
// 实验步骤开始时间
public DateTime startTime = default;
// 实验步骤结束时间
public DateTime endTime = default;
// 实验步骤合理用时:单位秒
public int expectTime = 0;
// 实验步骤满分0 ~100百分制
public int maxScore = 100;
// 实验步骤得分0 ~100百分制
public int score = 0;
// 实验步骤操作次数
public int repeatCount = 1;
// 步骤评价200 字以内
public string evaluation = "";
// 赋分模型200 字以内
public string scoringModel = "";
// 备注
public string remarks { get; set; }
}
public class Response
{
public string msg;
public bool success;
}
}