增加apiclient模块,增加js静态数据方法和类
This commit is contained in:
		
							parent
							
								
									9e5d7480dc
								
							
						
					
					
						commit
						7c1a14b87f
					
				
							
								
								
									
										22
									
								
								Falcon.SugarApi/ApiClient/ApiClientException.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Falcon.SugarApi/ApiClient/ApiClientException.cs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								Falcon.SugarApi/ApiClient/ApiClientOptions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								Falcon.SugarApi/ApiClient/ApiClientOptions.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										68
									
								
								Falcon.SugarApi/ApiClient/HttpClientHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								Falcon.SugarApi/ApiClient/HttpClientHelper.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								Falcon.SugarApi/ApiClient/IHttpClientHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Falcon.SugarApi/ApiClient/IHttpClientHelper.cs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										34
									
								
								JS/StaticData.js
									
									
									
									
									
										Normal 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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user