😎 SolidWorksManage 导出相关文件(全部)服务

This commit is contained in:
bairubing 2024-12-20 16:53:03 +08:00
parent f949b8f616
commit 540e958898
3 changed files with 78 additions and 19 deletions

View File

@ -0,0 +1,18 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vistar.Application.SolidWorksManageService.Dto;
public class ExportRelatedFilesInput
{
public long record_id { get; set; }
public int object_id { get; set; }
}

View File

@ -0,0 +1,20 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Vistar.Application.SolidWorksManageService.Dto;
public class ExportRelatedFilesOutput
{
public string State { get; set; }
public string FileName { get; set; }
public string FileBate { get; set; }
}

View File

@ -1,14 +1,18 @@
using Admin.NET.Core;
using Admin.NET.Core.Service;
using COSXML.Network;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.Cms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
@ -23,7 +27,7 @@ namespace Vistar.Application.SolidWorksManageService;
/// </summary>
public class SolidWorksManageService : IDynamicApiController, ITransient
{
private static readonly HttpClient client = new HttpClient();
private static readonly System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
private readonly SysConfigService _sysConfigService;
public SolidWorksManageService(SysConfigService sysConfigService)
{
@ -317,12 +321,12 @@ public class SolidWorksManageService : IDynamicApiController, ITransient
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<ManageResponse> ExportRelatedFiles(CheckDataInput input)
public async Task<ExportRelatedFilesOutput> ExportRelatedFiles(ExportRelatedFilesInput input)
{
// 获取 SolidWorksManage 地址
string apiUrl = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageApiUrl);
// 拼接接口地址
string url = $"{apiUrl}/api/File/ExportRelatedFiles?object_id=138&record_id=4";
string url = $"{apiUrl}/api/File/ExportRelatedFiles?object_id=" + input.object_id + "&record_id=" + input.record_id;
//获取token
var token = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageAuthorization);
//// 将参数对象序列化为 JSON 字符串
@ -334,29 +338,46 @@ public class SolidWorksManageService : IDynamicApiController, ITransient
client.DefaultRequestHeaders.Add("Authorization", token);
// 发送 POST 请求
HttpResponseMessage response = await client.GetAsync(url);
var output = new ExportRelatedFilesOutput();
// 处理响应
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
// 解析 JSON 数据
var result = JsonConvert.DeserializeObject<ManageResponse>(responseBody);
return result;
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
string fileName;
string fileContentBase64;
// 将字节数据加载到内存流
using (var memoryStream = new MemoryStream(bytes))
using (var zipInputStream = new ZipInputStream(memoryStream))
{
ZipEntry entry;
while ((entry = zipInputStream.GetNextEntry()) != null)
{
fileName = entry.Name;
output.FileName = fileName;
// 读取解压后的文件内容
using (var decompressedStream = new MemoryStream())
{
zipInputStream.CopyTo(decompressedStream);
byte[] fileData = decompressedStream.ToArray();
// 将解压后的文件内容转为 Base64
fileContentBase64 = Convert.ToBase64String(fileData);
output.FileBate = fileContentBase64;
}
}
}
output.State = "成功";
}
else
{
string responseBody = await response.Content.ReadAsStringAsync();
output.State = "失败";
}
return output;
}
var errorResponse = new ManageResponse()
{
Success = false,
Message = responseBody,
Data = "",
CustomAction = "",
CustomAttributes = ""
};
return errorResponse;
}
}
/// <summary>
/// 添加相关文件