From 7c1a14b87f417f58d1e56a14636de80b159b9b24 Mon Sep 17 00:00:00 2001
From: FalconFly <12919280+falconfly@user.noreply.gitee.com>
Date: Mon, 22 May 2023 17:49:05 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0apiclient=E6=A8=A1=E5=9D=97?=
=?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0js=E9=9D=99=E6=80=81=E6=95=B0?=
=?UTF-8?q?=E6=8D=AE=E6=96=B9=E6=B3=95=E5=92=8C=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ApiClient/ApiClientException.cs | 22 ++++++
Falcon.SugarApi/ApiClient/ApiClientOptions.cs | 27 ++++++++
Falcon.SugarApi/ApiClient/HttpClientHelper.cs | 68 +++++++++++++++++++
.../ApiClient/IHttpClientHelper.cs | 17 +++++
.../ApiClient/IServiceCollentionExtend.cs | 24 +++++++
JS/StaticData.js | 34 ++++++++++
6 files changed, 192 insertions(+)
create mode 100644 Falcon.SugarApi/ApiClient/ApiClientException.cs
create mode 100644 Falcon.SugarApi/ApiClient/ApiClientOptions.cs
create mode 100644 Falcon.SugarApi/ApiClient/HttpClientHelper.cs
create mode 100644 Falcon.SugarApi/ApiClient/IHttpClientHelper.cs
create mode 100644 Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs
create mode 100644 JS/StaticData.js
diff --git a/Falcon.SugarApi/ApiClient/ApiClientException.cs b/Falcon.SugarApi/ApiClient/ApiClientException.cs
new file mode 100644
index 0000000..6d18e55
--- /dev/null
+++ b/Falcon.SugarApi/ApiClient/ApiClientException.cs
@@ -0,0 +1,22 @@
+using System;
+
+namespace Falcon.SugarApi.ApiClient
+{
+ ///
+ /// api客户端异常
+ ///
+ public class ApiClientException:Exception
+ {
+ ///
+ /// 通过提供异常消息,请求地址,请求数据,和内部异常构造异常
+ ///
+ /// 异常消息
+ /// 请求地址
+ /// 请求数据
+ /// 内部异常
+ public ApiClientException(string? message,string url,string data,Exception? innerException) : base(message,innerException) {
+ this.Data.Add("url",url);
+ this.Data.Add("data",data);
+ }
+ }
+}
diff --git a/Falcon.SugarApi/ApiClient/ApiClientOptions.cs b/Falcon.SugarApi/ApiClient/ApiClientOptions.cs
new file mode 100644
index 0000000..2cc4518
--- /dev/null
+++ b/Falcon.SugarApi/ApiClient/ApiClientOptions.cs
@@ -0,0 +1,27 @@
+using Microsoft.Extensions.Options;
+
+namespace Falcon.SugarApi.ApiClient
+{
+ ///
+ /// api客户端配置
+ ///
+ public class ApiClientOptions:IOptions
+ {
+ ///
+ /// 配置对象
+ ///
+ public ApiClientOptions Value => this;
+ ///
+ /// 客户端请求超时。秒
+ ///
+ public int ClientTimeout { get; set; } = 10;
+ ///
+ /// 媒体类型
+ ///
+ public string MediaType { get; set; } = "application/json";
+ ///
+ /// 保持链接
+ ///
+ public bool KeepAlive { get; set; }=true;
+ }
+}
diff --git a/Falcon.SugarApi/ApiClient/HttpClientHelper.cs b/Falcon.SugarApi/ApiClient/HttpClientHelper.cs
new file mode 100644
index 0000000..3acc2a5
--- /dev/null
+++ b/Falcon.SugarApi/ApiClient/HttpClientHelper.cs
@@ -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
+{
+ ///
+ /// 通过HttpClient调用api
+ ///
+ public class HttpClientHelper:IHttpClientHelper
+ {
+ ///
+ /// 实例化帮助对象
+ ///
+ /// 客户端配置
+ public HttpClientHelper(IOptions options) {
+ Options = options.Value;
+ }
+
+ ///
+ /// 记录日期的ILogger
+ ///
+ public ILogger Log { get; }
+
+ ///
+ /// 客户端配置
+ ///
+ public ApiClientOptions Options { get; set; }
+
+ ///
+ /// 向服务器发送POST请求
+ ///
+ /// api服务器url地址
+ /// 要发送的数据
+ /// 响应数据
+ /// 请求api时候发生的异常
+ 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 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;
+ }
+
+ }
+}
diff --git a/Falcon.SugarApi/ApiClient/IHttpClientHelper.cs b/Falcon.SugarApi/ApiClient/IHttpClientHelper.cs
new file mode 100644
index 0000000..8f0a839
--- /dev/null
+++ b/Falcon.SugarApi/ApiClient/IHttpClientHelper.cs
@@ -0,0 +1,17 @@
+namespace Falcon.SugarApi.ApiClient
+{
+ ///
+ /// 通过HttpClient调用api
+ ///
+ public interface IHttpClientHelper
+ {
+ ///
+ /// 向服务器发送POST请求
+ ///
+ /// api服务器url地址
+ /// 要发送的数据
+ /// 响应数据
+ /// 请求api时候发生的异常
+ string Post(string url,string data);
+ }
+}
\ No newline at end of file
diff --git a/Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs b/Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs
new file mode 100644
index 0000000..8192fa5
--- /dev/null
+++ b/Falcon.SugarApi/ApiClient/IServiceCollentionExtend.cs
@@ -0,0 +1,24 @@
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Falcon.SugarApi.ApiClient
+{
+ ///
+ /// 服务扩展,注册帮助方法
+ ///
+ public static class IServiceCollentionExtend
+ {
+ ///
+ /// 注册IHttpClientHelper实现
+ ///
+ /// 服务集合
+ /// 应用配置
+ /// 服务集合
+ public static IServiceCollection AddHttpClientHelper(this IServiceCollection services,IConfiguration config) {
+ //var sect = config.GetValue>("ApiClient");
+ services.Configure(config.GetSection("ApiClient"));
+ services.AddSingleton();
+ return services;
+ }
+ }
+}
\ No newline at end of file
diff --git a/JS/StaticData.js b/JS/StaticData.js
new file mode 100644
index 0000000..289cf1b
--- /dev/null
+++ b/JS/StaticData.js
@@ -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);
+ }
+ }
+}
\ No newline at end of file