using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.IO; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; using UnityEngine.UI; using XCharts.Runtime; public class ReportUploadRoot { /// /// /// public string state { get; set; } /// /// user_id参数不能为空 /// public string message { get; set; } /// /// /// public List data = new List(); } [System.Serializable] public class ReportUploadDate { /// /// /// public string user_id { get; set; } /// /// /// public string rep_name { get; set; } /// /// /// public string rep_type { get; set; } /// /// /// public string file { get; set; } } /// /// 实验报告上传 /// public class ReportUploadManager : SingletonMono { public static ReportUploadManager instance; private string url = "http://172.16.1.254:13030/Handler/Repair.ashx?action=AddReport"; private void Awake() { instance = this; } /// /// 上传报告 /// /// public IEnumerator ReportUpload(string content) { // 1. 初始化表单 WWWForm form = new WWWForm(); // 添加用户ID(确保LoginManager已正确初始化) if (LoginManager.Instance?.loginRespons?.data == null) { Debug.LogError("用户未登录,无法获取user_id"); yield break; } form.AddField("user_id", LoginManager.Instance.loginRespons.data.user_id); form.AddField("rep_name", content); bool hasSelectedToggle = false; foreach (Toggle toggle in UI_ExperimentChooesPanel.instance?.SubjectTog) { if (toggle == null) continue; if (toggle.isOn) { hasSelectedToggle = true; string toggleName = toggle.name; string imagePath = GetImagePathByToggleName(toggleName); // 加载对应图片并添加到表单 if (!string.IsNullOrEmpty(imagePath)) { LoadImageToForm(form, imagePath); } else { Debug.LogWarning($"Toggle {toggleName} 没有对应的图片路径"); } } } if (!hasSelectedToggle) { Debug.LogWarning("未选中任何实验项目,取消上传"); yield break; } string uploadUrl = LoginManager.Instance.Ipurl + "/Handler/Repair.ashx?action=AddReport"; using (UnityWebRequest request = UnityWebRequest.Post(uploadUrl, form)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { Debug.Log("报告上传成功!后端返回:" + request.downloadHandler.text); } else { Debug.LogError($"报告上传失败:{request.error},状态码:{request.responseCode}"); } } } /// /// 根据Toggle名称获取对应的图片路径 /// /// /// 图片的相对路径 private string GetImagePathByToggleName(string toggleName) { switch (toggleName) { case "5-1-1": return "实验报告图片/比例1倍_511.png"; case "5-1-2": return "实验报告图片/比例1倍_522.png"; case "5-2-1": return "实验报告图片/1uf积分_521.png"; case "5-2-2": return "实验报告图片/1uf积分_522.png"; case "5-3-1": return "实验报告图片/比例积分0.png"; case "5-3-2": return "实验报告图片/比例积分1.png"; case "5-4-1": return "实验报告图片/比例微分1.png"; case "5-4-2": return "实验报告图片/比例微分2.png"; case "5-5-1": return "实验报告图片/惯性环节1.png"; case "5-5-2": return "实验报告图片/惯性环节2.png"; default: return string.Empty; } } /// /// 加载图片并添加到表单中 /// /// 要添加的表单 /// 图片在StreamingAssets下的相对路径 private void LoadImageToForm(WWWForm form, string imageRelativePath) { // 拼接完整的图片路径 string fullImagePath = Path.Combine(Application.streamingAssetsPath, imageRelativePath.TrimStart("/实验报告图片")); // 检查文件是否存在 if (!File.Exists(fullImagePath)) { Debug.LogError($"图片文件不存在:{fullImagePath}"); return; } try { // 读取图片字节 byte[] imageBytes = File.ReadAllBytes(fullImagePath); // 将图片转换为Texture2D(可选,若需要压缩为JPG) Texture2D texture = new Texture2D(1920, 1080); if (texture.LoadImage(imageBytes)) { // 编码为JPG(质量100) byte[] jpgBytes = texture.EncodeToJPG(100); // 添加二进制数据到表单,字段名"file",文件名"report.jpg",MIME类型"image/jpeg" form.AddBinaryData("file", jpgBytes, "report.jpg", "image/jpeg"); Debug.Log($"图片 {imageRelativePath} 已添加到上传表单"); } else { Debug.LogError($"图片 {imageRelativePath} 加载失败,无法转换为Texture2D"); } // 释放Texture资源 Destroy(texture); } catch (Exception e) { Debug.LogError($"加载图片失败:{e.Message}"); } } }