增加apiclient模块,增加js静态数据方法和类

This commit is contained in:
FalconFly 2023-05-22 17:49:05 +08:00
parent 1dcb18e77a
commit 4998e5588f
6 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,22 @@
using System;
namespace Falcon.SugarApi.ApiClient
{
/// <summary>
/// api客户端异常
/// </summary>
public class ApiClientException:Exception
{
/// <summary>
/// 通过提供异常消息,请求地址,请求数据,和内部异常构造异常
/// </summary>
/// <param name="message">异常消息</param>
/// <param name="url">请求地址</param>
/// <param name="data">请求数据</param>
/// <param name="innerException">内部异常</param>
public ApiClientException(string? message,string url,string data,Exception? innerException) : base(message,innerException) {
this.Data.Add("url",url);
this.Data.Add("data",data);
}
}
}

View File

@ -0,0 +1,27 @@
using Microsoft.Extensions.Options;
namespace Falcon.SugarApi.ApiClient
{
/// <summary>
/// api客户端配置
/// </summary>
public class ApiClientOptions:IOptions<ApiClientOptions>
{
/// <summary>
/// 配置对象
/// </summary>
public ApiClientOptions Value => this;
/// <summary>
/// 客户端请求超时。秒
/// </summary>
public int ClientTimeout { get; set; } = 10;
/// <summary>
/// 媒体类型
/// </summary>
public string MediaType { get; set; } = "application/json";
/// <summary>
/// 保持链接
/// </summary>
public bool KeepAlive { get; set; }=true;
}
}

View File

@ -0,0 +1,68 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace Falcon.SugarApi.ApiClient
{
/// <summary>
/// 通过HttpClient调用api
/// </summary>
public class HttpClientHelper:IHttpClientHelper
{
/// <summary>
/// 实例化帮助对象
/// </summary>
/// <param name="options">客户端配置</param>
public HttpClientHelper(IOptions<ApiClientOptions> options) {
Options = options.Value;
}
/// <summary>
/// 记录日期的ILogger
/// </summary>
public ILogger<HttpClientHelper> Log { get; }
/// <summary>
/// 客户端配置
/// </summary>
public ApiClientOptions Options { get; set; }
/// <summary>
/// 向服务器发送POST请求
/// </summary>
/// <param name="url">api服务器url地址</param>
/// <param name="data">要发送的数据</param>
/// <returns>响应数据</returns>
/// <exception cref="Falcon.SugarApi.ApiClient.ApiClientException">请求api时候发生的异常</exception>
public string Post(string url,string data) {
var result = "";
using(HttpClient client = new HttpClient()) {
try {
client.Timeout = TimeSpan.FromSeconds(this.Options.ClientTimeout);
HttpContent content = new StringContent(data);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(Options.MediaType);
if(this.Options.KeepAlive) {
client.DefaultRequestHeaders.Connection.Add("keep-alive");
}
Task<HttpResponseMessage> res = client.PostAsync(url,content);
if(res.Result.StatusCode == System.Net.HttpStatusCode.OK) {
result = res.Result.Content.ReadAsStringAsync().Result;
}
else {
throw new ApiClientException("访问webapi方法状态码错误",url,data,null);
}
}
catch(Exception ex) {
throw new ApiClientException("访问webapi方法异常",url,data,ex);
}
finally {
client.Dispose();
}
}
return result;
}
}
}

View File

@ -0,0 +1,17 @@
namespace Falcon.SugarApi.ApiClient
{
/// <summary>
/// 通过HttpClient调用api
/// </summary>
public interface IHttpClientHelper
{
/// <summary>
/// 向服务器发送POST请求
/// </summary>
/// <param name="url">api服务器url地址</param>
/// <param name="data">要发送的数据</param>
/// <returns>响应数据</returns>
/// <exception cref="Falcon.SugarApi.ApiClient.ApiClientException">请求api时候发生的异常</exception>
string Post(string url,string data);
}
}

View File

@ -0,0 +1,24 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Falcon.SugarApi.ApiClient
{
/// <summary>
/// 服务扩展,注册帮助方法
/// </summary>
public static class IServiceCollentionExtend
{
/// <summary>
/// 注册IHttpClientHelper实现
/// </summary>
/// <param name="services">服务集合</param>
/// <param name="config">应用配置</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddHttpClientHelper(this IServiceCollection services,IConfiguration config) {
//var sect = config.GetValue<IOptions<ApiClientOptions>>("ApiClient");
services.Configure<ApiClientOptions>(config.GetSection("ApiClient"));
services.AddSingleton<IHttpClientHelper,HttpClientHelper>();
return services;
}
}
}

34
JS/StaticData.js Normal file
View File

@ -0,0 +1,34 @@
//存储静态数据
function createSetStaticData() {
if (window.sessionStorage) {
return (k, v) => window.sessionStorage.setItem(k, v);
} else if ($ && $.cookie) {
return (k, v) => $.cookie(k, v);
} else {
throw new Error("没有找到可以支持的存储方法!");
}
}
var setStaticData = createSetStaticData();
//读取存储的静态数据
function CreateGetStaticData() {
if (window.sessionStorage) {
return (k) => window.sessionStorage.getItem(k);
} else if ($ && $.cookie) {
return (k) => $.cookie(k);
} else {
throw new Error("没有找到可以支持的存储方法!");
}
}
var getStaticData = CreateGetStaticData();
//静态数据存取类
class StaticData {
constructor(k, v) {
this.key = k;
this.value = v;
if (v) {
setStaticData(k, v);
} else {
this.value = getStaticData(k);
}
}
}