using ConsoleApp1; using MES.Service.service.PLM; using MES.Service.util; using Microsoft.AspNetCore.Mvc; using System.Dynamic; using System.IO; namespace MESApplication.Controllers.PLM; [ApiController] [Route("api/PLM")] public class PLMController : ControllerBase { private readonly PLMManager m = new(); private readonly WarehouseDownloadDoc wdd = new(); //RetrieveDrawings 调取图纸 [HttpPost("RetrieveDrawings")] public ResponseResult RetrieveDrawings(string ItemNo) { try { dynamic resultInfos = new ExpandoObject(); resultInfos = m.RetrieveDrawings(ItemNo); return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //RetrieveImageFile 调取上传到服务器的拍摄文件 [HttpGet("GetImageNames")] public IActionResult GetImageNames(string releaseNo) { // 拼接本地文件系统路径 string basePath = @"D:\MES_FTP\IQC\"; // 服务器D盘下的基础路径 string folderPath = Path.Combine(basePath, releaseNo); // 动态拼接单号对应的文件夹路径 // 检查文件夹是否存在 if (!Directory.Exists(folderPath)) { return NotFound($"文件夹 {folderPath} 不存在。请检查路径是否正确。"); } // 获取文件夹中的所有图片文件名称 string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; // 获取文件的完整路径,并返回一个包含文件名和完整路径的对象 var imageFiles = Directory.GetFiles(folderPath) .Where(file => imageExtensions.Any(ext => file.ToLower().EndsWith(ext))) .Select(file => new { FileName = Path.GetFileName(file), // 文件名 FilePath = file, FileBasePath = ConvertFileToBase64(file) // 文件的 Base64 数据 // 文件的完整路径 }); return Ok(new { success = true, data = imageFiles }); } private string ConvertFileToBase64(string filePath) { byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); return Convert.ToBase64String(fileBytes); } [HttpPost("DeleteImageFile")] public IActionResult DeleteImageFile([FromQuery] string filePath) { // 检查文件路径是否为空 if (string.IsNullOrWhiteSpace(filePath)) { return BadRequest("文件路径不能为空"); } // 确保路径是绝对路径(可以根据需要调整) filePath = Path.GetFullPath(filePath); // 检查文件是否存在 if (!System.IO.File.Exists(filePath)) { return NotFound("文件不存在"); } try { // 删除文件 System.IO.File.Delete(filePath); return Ok("文件删除成功"); } catch (System.Exception ex) { // 捕获异常并返回错误信息 return StatusCode(500, $"删除文件时发生错误: {ex.Message}"); } } //RetrieveDrawings 调取图纸 [HttpPost("OpenDrawings")] public IActionResult OpenDrawings(string fileId,string fName) { try { var resultInfos = wdd.SendRequest("Download", fileId); return File(resultInfos, "application/octet-stream", fName); } catch (Exception ex) { return StatusCode(500, new ResponseResult { status = 1, message = ex.Message, data = null }); } } }