ND_SimulationAutomaticControl/Assets/Scripts/ReportUploadManager.cs

201 lines
5.9 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 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
{
/// <summary>
///
/// </summary>
public string state { get; set; }
/// <summary>
/// user_id参数不能为空
/// </summary>
public string message { get; set; }
/// <summary>
///
/// </summary>
public List<ReportUploadDate> data = new List<ReportUploadDate>();
}
[System.Serializable]
public class ReportUploadDate
{
/// <summary>
///
/// </summary>
public string user_id { get; set; }
/// <summary>
///
/// </summary>
public string rep_name { get; set; }
/// <summary>
///
/// </summary>
public string rep_type { get; set; }
/// <summary>
///
/// </summary>
public string file { get; set; }
}
/// <summary>
/// 实验报告上传
/// </summary>
public class ReportUploadManager : SingletonMono<LoginManager>
{
public static ReportUploadManager instance;
private string url = "http://172.16.1.254:13030/Handler/Repair.ashx?action=AddReport";
private void Awake()
{
instance = this;
}
/// <summary>
/// 上传报告
/// </summary>
/// <returns></returns>
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}");
}
}
}
/// <summary>
/// 根据Toggle名称获取对应的图片路径
/// </summary>
/// <param name="toggleName"></param>
/// <returns>图片的相对路径</returns>
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;
}
}
/// <summary>
/// 加载图片并添加到表单中
/// </summary>
/// <param name="form">要添加的表单</param>
/// <param name="imageRelativePath">图片在StreamingAssets下的相对路径</param>
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}");
}
}
}