118 lines
4.8 KiB
C#
118 lines
4.8 KiB
C#
|
using Admin.NET.Core;
|
|||
|
using Admin.NET.Core.Service;
|
|||
|
using Furion.DependencyInjection;
|
|||
|
using Furion.DynamicApiController;
|
|||
|
using Microsoft.Extensions.Caching.Memory;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Reflection.Metadata;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Vistar.Application.SolidWorksManageService.Dto;
|
|||
|
|
|||
|
namespace Vistar.Application.SolidWorksManageService;
|
|||
|
/// <summary>
|
|||
|
/// SolidWorksManage服务
|
|||
|
/// </summary>
|
|||
|
public class SolidWorksManageService : IDynamicApiController, ITransient
|
|||
|
{
|
|||
|
private static readonly HttpClient client = new HttpClient();
|
|||
|
private readonly SysConfigService _sysConfigService;
|
|||
|
public SolidWorksManageService(SysConfigService sysConfigService)
|
|||
|
{
|
|||
|
_sysConfigService = sysConfigService;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 获取 token
|
|||
|
/// </summary>
|
|||
|
/// <returns>授权响应对象</returns>
|
|||
|
public async Task<AuthorizationResponse> Authenticate()
|
|||
|
{
|
|||
|
// 获取 SolidWorksManage 地址
|
|||
|
string apiUrl = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageApiUrl);
|
|||
|
// 拼接接口地址
|
|||
|
string url = $"{apiUrl}/api/Authentication/Authenticate";
|
|||
|
|
|||
|
// 构建请求参数字典
|
|||
|
var parameter = new Dictionary<string, string>
|
|||
|
{
|
|||
|
["username"] = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageUserName),
|
|||
|
["password"] = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManagePassword),
|
|||
|
["licenseId"] = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageLicenseId),
|
|||
|
["apikey"] = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageApiKey)
|
|||
|
};
|
|||
|
|
|||
|
// 将请求参数格式化为 URL 格式的字符串
|
|||
|
var formattingParameter = string.Join("&", parameter.Select(kvp => $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"));
|
|||
|
// 构建完整的请求 URL
|
|||
|
var requestUrl = $"{url}?{formattingParameter}";
|
|||
|
|
|||
|
// 使用 using 语句确保 HttpResponseMessage 资源被正确释放
|
|||
|
using (var response = await client.PostAsync(requestUrl, null))
|
|||
|
{
|
|||
|
if (response.IsSuccessStatusCode)
|
|||
|
{
|
|||
|
// 读取响应内容并反序列化为授权响应对象
|
|||
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|||
|
return JsonConvert.DeserializeObject<AuthorizationResponse>(responseBody);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 读取响应内容并反序列化为授权响应对象(通常表示请求失败,但这里也尝试反序列化以便进一步处理错误)
|
|||
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|||
|
return JsonConvert.DeserializeObject<AuthorizationResponse>(responseBody);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 创建记录
|
|||
|
/// </summary>
|
|||
|
/// <param name="input"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task<ManageResponse> CreateRecord(CreateRecordInput input)
|
|||
|
{
|
|||
|
// 获取 SolidWorksManage 地址
|
|||
|
string apiUrl = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageApiUrl);
|
|||
|
// 拼接接口地址
|
|||
|
string url = $"{apiUrl}/api/PropertyCard/CreateRecord";
|
|||
|
//获取token
|
|||
|
var token = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SolidWorksManageAuthorization);
|
|||
|
// 将参数对象序列化为 JSON 字符串
|
|||
|
string jsonParameters = JsonConvert.SerializeObject(input);
|
|||
|
// 设置请求内容
|
|||
|
var content = new StringContent(jsonParameters, Encoding.UTF8, "application/json");
|
|||
|
client.DefaultRequestHeaders.Remove("Authorization");
|
|||
|
// 添加 Authorization 到请求头
|
|||
|
client.DefaultRequestHeaders.Add("Authorization", token);
|
|||
|
// 发送 POST 请求
|
|||
|
HttpResponseMessage response = await client.PostAsync(url, content);
|
|||
|
// 处理响应
|
|||
|
if (response.IsSuccessStatusCode)
|
|||
|
{
|
|||
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|||
|
// 解析 JSON 数据
|
|||
|
var result = JsonConvert.DeserializeObject<ManageResponse>(responseBody);
|
|||
|
return result;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|||
|
|
|||
|
var errorResponse = new ManageResponse()
|
|||
|
{
|
|||
|
Success = false,
|
|||
|
Message = responseBody,
|
|||
|
Data="",
|
|||
|
CustomAction="",
|
|||
|
CustomAttributes=""
|
|||
|
};
|
|||
|
return errorResponse;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|