140 lines
4.7 KiB
C#
140 lines
4.7 KiB
C#
using JSMachine.WMS.Infrastructure.Helper;
|
|
using JSMachine.WMS.RPC.PaperMESServerRPC.Bo;
|
|
using Newtonsoft.Json;
|
|
using RestSharp;
|
|
|
|
namespace JSMachine.WMS.RPC.Common.Base
|
|
{
|
|
/// <summary>
|
|
/// RPC 通用 HTTP 服务基类,封装 JSON 请求、响应反序列化和错误日志记录。
|
|
/// </summary>
|
|
/// <typeparam name="T">远程接口使用的业务数据类型。</typeparam>
|
|
public class BaseService<T> where T : class, new()
|
|
{
|
|
/// <summary>
|
|
/// 添加
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="url"></param>
|
|
/// <param name="method"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddAsync(T t, string url, Method method)
|
|
{
|
|
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(t));
|
|
if (string.IsNullOrEmpty(resStr))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
ApiResultBo<bool> apiResultDto = JsonConvert.DeserializeObject<ApiResultBo<bool>>(resStr);
|
|
return apiResultDto.Data;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="url"></param>
|
|
/// <param name="method"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EditAsync(T t, string url, Method method)
|
|
{
|
|
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(t));
|
|
if (string.IsNullOrEmpty(resStr))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
ApiResultBo<bool> apiResultDto = JsonConvert.DeserializeObject<ApiResultBo<bool>>(resStr);
|
|
return apiResultDto.Data;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除单个
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="url"></param>
|
|
/// <param name="method"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DelteOneAsync(Guid id,string url, Method method)
|
|
{
|
|
string resStr = await HttpRequestHelper.RequestByDic(url, method, new Dictionary<string, object> { { "id", id } });
|
|
if (string.IsNullOrEmpty(resStr))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
ApiResultBo<bool> apiResultDto = JsonConvert.DeserializeObject<ApiResultBo<bool>>(resStr);
|
|
return apiResultDto.Data;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除多个
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <param name="url"></param>
|
|
/// <param name="method"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DelteManyAsync(List<Guid> ids, string url, Method method)
|
|
{
|
|
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(ids));
|
|
if (string.IsNullOrEmpty(resStr))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
ApiResultBo<bool> apiResultDto = JsonConvert.DeserializeObject<ApiResultBo<bool>>(resStr);
|
|
return apiResultDto.Data;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<T>> GetAllAsync(string url)
|
|
{
|
|
string resStr = await HttpRequestHelper.RequestByDic(url, Method.GET);
|
|
if (string.IsNullOrEmpty(resStr))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
ApiResultBo<List<T>> apiResultDto = JsonConvert.DeserializeObject<ApiResultBo<List<T>>>(resStr);
|
|
return apiResultDto.Data;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|