using System.Collections.Generic;
namespace APT.Infrastructure.Core
{
public class WebUtils
{
///
/// 通过唯一编号获得数据表
///
///
///
///
///
public static T Get(string url, string id, string token, string tenant = "")
{
BaseFilter baseFilter = new BaseFilter();
baseFilter.FilterGroup.Rules.Add(new FilterRule("ID", id));
return Get(url, baseFilter, token, SendType.Post, tenant);
}
public static T Get(string url, string key, string value, string token = "", string tenant = "")
{
return Get(url + $"?{key}={value}", token, tenant);
}
public static string Get(string url, string key, string value, string token = "", string tenant = "")
{
return Get(url + $"?{key}={value}", token, tenant);
}
public static string Get(string url, string token, string tenant = "")
{
var body = new ApiBody();
body.SendType = SendType.Get;
body.ApiUrl = url;
body.SendObj = null;
body.Token = token;
body.ResolvingResult = false;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
var msg = "";
var result = ApiHelper.GetApiData(body, ref msg);
return result;
}
public static T Get(string url, string token, string tenant = "")
{
var body = new ApiBody();
body.SendType = SendType.Get;
body.ApiUrl = url;
body.SendObj = null;
body.Token = token;
body.ResolvingResult = false;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
///
/// 通过过滤条件获取一条数据
///
///
///
///
/// 是否直接转换对象
///
public static T Get(string url, BaseFilter filter, string token, SendType type = SendType.Post,
string tenant = "", bool isResolvingResult = false)
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = filter;
body.Token = token;
body.ResolvingResult = isResolvingResult;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
public static bool Save(string url, T t, string token, string tenant = "")
{
string msg = string.Empty;
return Save(url, t, token, ref msg, tenant);
}
public static bool Save(string url, T t, string token, ref string msg, string tenant = "")
{
var body = new ApiBody();
body.SendType = SendType.Post;
body.ApiUrl = url;
body.SendObj = t;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount, ref msg);
return result;
}
///
/// 执行某个接口
///
///
///
///
public static bool Do(string url, BaseFilter filter, string token, SendType type = SendType.Post, string tenant = "")
{
string ms = string.Empty;
return Do(url, filter, token, ref ms, type, tenant);
}
public static bool Do(string url, BaseFilter filter, string token, ref string msg, SendType type = SendType.Post, string tenant = "")
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = filter;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount, ref msg);
return result;
}
///
///
///
///
///
///
///
public static bool Delete(string url, string id, string token, string tenant = "")
{
var body = new ApiBody();
body.SendType = SendType.Get;
body.ApiUrl = url;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
IDictionary parameters = new Dictionary();
body.Params = parameters;
parameters.Add("id", id);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
public static bool BatchDelete(string url, string[] id, string token, string tenant = "")
{
var body = new ApiBody();
var sendStr = string.Join(",", id);
body.SendType = SendType.Get;
body.ApiUrl = url;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
IDictionary parameters = new Dictionary();
parameters.Add("ids", sendStr);
body.Params = parameters;
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
public static List List(string url, BaseFilter filter, string token,
SendType type = SendType.Post, string tenant = "")
{
int totalCount = 0;
return List(url, filter, ref totalCount, token, type, tenant);
}
public static List List(string url, BaseFilter filter, ref int totalCount,
string token, SendType type = SendType.Post, string tenant = "")
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = filter;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
var result = ApiHelper.GetApiData>(body, ref totalCount);
return result;
}
public static PagedList PageList(string url, BasePageFilter filter,
string token, SendType type = SendType.Post, string tenant = "")
{
var startIndex = filter.PageIndex > 0 ? filter.PageIndex : filter.Start / filter.Limit + 1;
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = filter;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData>(body, ref totalCount);
PagedList pageList = null;
if (result != null)
pageList = new PagedList(result, startIndex, filter.Limit, totalCount);
return pageList;
}
///
/// 通过过滤条件获取一条数据
///
///
///
///
///
public static T Execute(string url, object sendObj, string token,
SendType type = SendType.Post, IDictionary headParams = null,
bool isResolvingResult = false, string tenant = "")
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = sendObj;
body.Token = token;
body.HeadParams = headParams;
body.ResolvingResult = isResolvingResult;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
///
/// 获取二进制图片
///
///
///
///
///
///
///
///
///
public static byte[] GetPicture(string url, object sendObj, string token,
SendType type = SendType.Post, IDictionary headParams = null,
bool isResolvingResult = false, string tenant = "")
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.SendObj = sendObj;
body.Token = token;
body.HeadParams = headParams;
body.ResolvingResult = isResolvingResult;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
var result = ApiHelper.ApiPicture(body);
return result;
}
///
/// isResolvingResult=true
///
///
///
///
///
///
///
///
public static T ExecuteByResolving(string url, object sendObj, string token, SendType type = SendType.Post,
IDictionary headParams = null, string tenant = "")
{
return Execute(url, sendObj, token, type, headParams, true, tenant);
}
///
///
///
///
///
///
///
///
///
///
///
public static T Execute(string url, IDictionary bodyParams,
string token, SendType type = SendType.Post, IDictionary headParams = null,
bool isResolvingResult = false, string tenant = "")
{
var body = new ApiBody();
body.SendType = type;
body.ApiUrl = url;
body.Params = bodyParams;
body.HeadParams = headParams;
body.ResolvingResult = isResolvingResult;
body.Token = token;
if (!string.IsNullOrEmpty(tenant))
body.HeadParams.Add("Tenant", tenant);
int totalCount = 0;
var result = ApiHelper.GetApiData(body, ref totalCount);
return result;
}
///
/// isResolvingResult=true
///
///
///
///
///
///
///
///
public static T ExecuteByResolving(string url, IDictionary bodyParams,
string token, SendType type = SendType.Post, IDictionary headParams = null, string tenant = "")
{
return Execute(url, bodyParams, token, type, headParams, true, tenant);
}
}
}