using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using BootstrapBlazor.Components; using Microsoft.JSInterop; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; namespace WtmBlazorUtils { public class ApiClient { public HttpClient Client { get; } private IJSRuntime js { get; } public ApiClient(HttpClient client, IJSRuntime jsr) { Client = client; js = jsr; } public void SetToken(string token) { Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); } #region CallApi public async Task> CallAPI(string url, HttpMethodEnum method, HttpContent content, int? timeout = null, string proxy = null) where T : class { var token = await js.InvokeAsync("localStorageFuncs.get", "wtmtoken"); Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); ApiResult rv = new ApiResult(); try { if (string.IsNullOrEmpty(url)) { return rv; } url = url.AppendQuery("culture=" + System.Threading.Thread.CurrentThread.CurrentUICulture.Name); //如果配置了代理,则使用代理 //设置超时 if (timeout.HasValue) { Client.Timeout = new TimeSpan(0, 0, 0, timeout.Value, 0); } //填充表单数据 HttpResponseMessage res = null; switch (method) { case HttpMethodEnum.GET: res = await Client.GetAsync(url); break; case HttpMethodEnum.POST: res = await Client.PostAsync(url, content); break; case HttpMethodEnum.PUT: res = await Client.PutAsync(url, content); break; case HttpMethodEnum.DELETE: res = await Client.DeleteAsync(url); break; default: break; } if (res == null) { return rv; } rv.StatusCode = res.StatusCode; if (res.IsSuccessStatusCode == true) { Type dt = typeof(T); if (dt == typeof(byte[])) { rv.Data = await res.Content.ReadAsByteArrayAsync() as T; } else { string responseTxt = await res.Content.ReadAsStringAsync(); if (dt == typeof(string)) { rv.Data = responseTxt as T; } else { rv.Data = JsonSerializer.Deserialize(responseTxt, CoreProgram.DefaultJsonOption); } } } else { if (res.StatusCode == System.Net.HttpStatusCode.Unauthorized) { await js.InvokeVoidAsync("localStorageFuncs.remove", "wtmtoken"); await js.InvokeVoidAsync("urlFuncs.redirect","/login"); } string responseTxt = await res.Content.ReadAsStringAsync(); if (res.StatusCode == System.Net.HttpStatusCode.BadRequest) { try { rv.Errors = JsonSerializer.Deserialize(responseTxt, CoreProgram.DefaultJsonOption); } catch { } } rv.ErrorMsg = responseTxt; } return rv; } catch (Exception ex) { rv.ErrorMsg = ex.ToString(); return rv; } } /// /// 使用Get方法调用api /// /// /// Appsettings中配置的Domain key /// 调用地址 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, int? timeout = null, string proxy = null) where T : class { HttpContent content = null; //填充表单数据 return await CallAPI(url, HttpMethodEnum.GET, content, timeout, proxy); } /// /// /// /// /// Appsettings中配置的Domain key /// 调用地址 /// 调用方式 /// 提交字段 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, HttpMethodEnum method, IDictionary postdata, int? timeout = null, string proxy = null) where T : class { HttpContent content = null; //填充表单数据 if (!(postdata == null || postdata.Count == 0)) { List> paras = new List>(); foreach (string key in postdata.Keys) { paras.Add(new KeyValuePair(key, postdata[key])); } if (paras.Any()) { url = url.AppendQuery(paras); } content = new FormUrlEncodedContent(paras); } return await CallAPI(url, method, content, timeout, proxy); } public async Task> CallAPI(string url, HttpMethodEnum method, IDictionary postdata, byte[] filedata, string filename, int? timeout = null, string proxy = null) where T : class { MultipartFormDataContent content = new MultipartFormDataContent(); //填充表单数据 if (!(postdata == null || postdata.Count == 0)) { List> paras = new List>(); foreach (string key in postdata.Keys) { if (postdata[key] != null) { content.Add(new StringContent(postdata[key]), key); } } } content.Add(new ByteArrayContent(filedata), "File", filename); return await CallAPI(url, method, content, timeout, proxy); } /// /// /// /// /// Appsettings中配置的Domain key /// 调用地址 /// 调用方式 /// 提交的object,会被转成json提交 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, HttpMethodEnum method, object postdata, int? timeout = null, string proxy = null) where T : class { HttpContent content = new StringContent(JsonSerializer.Serialize(postdata, CoreProgram.DefaultPostJsonOption), System.Text.Encoding.UTF8, "application/json"); return await CallAPI(url, method, content, timeout, proxy); } public async Task> CallAPI(string url, HttpMethodEnum method, HttpContent content, int? timeout = null, string proxy = null) { return await CallAPI(url, method, content, timeout, proxy); } /// /// 使用Get方法调用api /// /// Appsettings中配置的Domain key /// 调用地址 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, int? timeout = null, string proxy = null) { return await CallAPI(url, timeout, proxy); } /// /// /// /// Appsettings中配置的Domain key /// 调用地址 /// 调用方式 /// 提交字段 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, HttpMethodEnum method, IDictionary postdata, int? timeout = null, string proxy = null) { return await CallAPI(url, method, postdata, timeout, proxy); } /// /// /// /// Appsettings中配置的Domain key /// 调用地址 /// 调用方式 /// 提交的object,会被转成json提交 /// 超时时间,单位秒 /// 代理地址 /// public async Task> CallAPI(string url, HttpMethodEnum method, object postdata, int? timeout = null, string proxy = null) { return await CallAPI(url, method, postdata, timeout, proxy); } #endregion public async Task>> CallSearchApi(string url, BaseSearcher searcher, QueryPageOptions options) where T : class, new() { searcher.Page = options.PageIndex; searcher.Limit = options.PageItems; if (string.IsNullOrEmpty(options.SortName) == false && options.SortOrder != SortOrder.Unset) { searcher.SortInfo = new SortInfo { Property = options.SortName, Direction = options.SortOrder == SortOrder.Desc ? SortDir.Desc : SortDir.Asc }; } else { searcher.SortInfo = null; } var rv = await CallAPI>(url, HttpMethodEnum.POST, searcher); return rv; } public async Task> CallItemsApi(string url, HttpMethodEnum method = HttpMethodEnum.GET, object postdata = null, int? timeout = null, string proxy = null, string placeholder = null,List values = null) { var result = await CallAPI>(url, method, postdata, timeout, proxy); List rv = new List(); if(values == null) { values = new List(); } if (result.StatusCode == System.Net.HttpStatusCode.OK) { if (result.Data != null) { foreach (var item in result.Data) { rv.Add(new SelectedItem { Text = item.Text, Value = item.Value.ToString(), Active = values.Any(x=>x.Contains(item.Value.ToString())) }); } } if(string.IsNullOrEmpty(placeholder) == false) { rv.Insert(0, new SelectedItem { Text = placeholder, Value = "" }); } } return rv; } } public class WtmApiResult { public List Data { get; set; } public int Count { get; set; } public int Page { get; set; } public int PageCount { get; set; } public string Msg { get; set; } public int Code { get; set; } } }