增加项目,分层
This commit is contained in:
1 parent
6e995a5bdb
commit
0a3e5af6a9
234 files changed
+26994
-1
No files matched your search
@@ -0,0 +1,159 @@
|
||||
using MAX.Core.Autofac;
|
||||
using MAX.Core.DependencyInjection;
|
||||
using MAX.Core.Helpers;
|
||||
using MAX.Core.Logger;
|
||||
using Store.Client.Extensions;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace Store.Client.HttpExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Store 基础 API 请求基类。
|
||||
/// 项目 API 可以继承该基类并重写 ServiceConfigKey,使用项目自己的服务配置。
|
||||
/// </summary>
|
||||
public class AppApiRequestBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志。
|
||||
/// </summary>
|
||||
[Autowired]
|
||||
protected ILoggerFacade _logger => LazyServiceProvider.Instance.LazyGetService<ILoggerFacade>();
|
||||
protected IApiServerConfig _config => LazyServiceProvider.Instance.LazyGetService<IApiServerConfig>();
|
||||
|
||||
/// <summary>
|
||||
/// 默认服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult Post(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl(urlpath), postdata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 公共签名服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult PostPublic(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl(urlpath), postdata, isUserSign: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拼多多服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult PostPdd(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl("bxx_" + urlpath), postdata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult PostWx(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl("wxxd_" + urlpath), postdata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信图片服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult PostWxImg(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl("wxxd_" + urlpath), postdata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 默认兼容服务请求。
|
||||
/// </summary>
|
||||
protected HttpResult PostDefault(string urlpath, string postdata = "")
|
||||
{
|
||||
return Request(BuildServiceUrl(urlpath), postdata);
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用当前 Key 构建服务请求地址。
|
||||
/// </summary>
|
||||
protected string BuildServiceUrl(string urlPath)
|
||||
{
|
||||
return $"{_config.Host}/wxxd_{urlPath}?obj=29wx/wxxd_{urlPath}";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建并执行服务端请求,默认使用当前用户签名。
|
||||
/// </summary>
|
||||
protected HttpResult Request(string url, string postdata = "", MethodType method = MethodType.Post, bool isUserSign = true)
|
||||
{
|
||||
ServerHttpItem item = new ServerHttpItem
|
||||
{
|
||||
URL = url,
|
||||
Method = method,
|
||||
Postdata = postdata ?? string.Empty
|
||||
};
|
||||
|
||||
if (isUserSign)
|
||||
{
|
||||
item.SignUser();
|
||||
}
|
||||
|
||||
return ServerRequest(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 服务器请求。
|
||||
/// </summary>
|
||||
public HttpResult ServerRequest(ServerHttpItem item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
}
|
||||
|
||||
#if RELEASE
|
||||
item.WebProxy = null;
|
||||
#endif
|
||||
if (!item.IsSign)
|
||||
{
|
||||
if (item.Shop != null)
|
||||
{
|
||||
item.SignUser(item.Shop.Id, item.Shop.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.Sign();
|
||||
}
|
||||
}
|
||||
|
||||
return Send(item);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行底层 HTTP 请求。
|
||||
/// </summary>
|
||||
protected HttpResult Send(ServerHttpItem item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(item.URL))
|
||||
{
|
||||
throw new ArgumentException("请求地址不能为空。", nameof(item));
|
||||
}
|
||||
|
||||
if (!item.URL.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.URL = _config.Host + "/" + item.URL.TrimStart('/');
|
||||
}
|
||||
|
||||
item.Encoding = Encoding.UTF8;
|
||||
HttpResult result = HttpHelper.Request(item);
|
||||
if (result.StatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(result.Html) && result.Html[0] == 65279)
|
||||
{
|
||||
result.Html = result.Html.Remove(0, 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
using MAX.Core.Helpers;
|
||||
using Store.Framework.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Store.Client.HttpExtension
|
||||
{
|
||||
public class ServerHttpItem : HttpItem
|
||||
{
|
||||
public ShopBase Shop { get; private set; }
|
||||
public ServerHttpItem()
|
||||
{
|
||||
|
||||
}
|
||||
public ServerHttpItem(ShopBase shop)
|
||||
{
|
||||
Shop = shop;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否已签名
|
||||
/// </summary>
|
||||
public bool IsSign { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
//using MAX.Core.Autofac;
|
||||
//using MAX.Core.Helpers;
|
||||
//using System;
|
||||
//using System.Net;
|
||||
//using System.Text;
|
||||
|
||||
//namespace Store.Client.HttpExtension
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 服务请求公共基类。
|
||||
// /// 该基类只负责按 Key 获取服务配置以及执行底层 HTTP 请求,不绑定具体业务签名规则。
|
||||
// /// </summary>
|
||||
// public abstract class ServiceRequestBase
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 服务配置提供器。由 Autofac 通过属性注入。
|
||||
// /// </summary>
|
||||
// [Autowired]
|
||||
// protected IServiceConfigProvider ServiceConfigProvider { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// /// 当前请求类使用的服务配置 Key。
|
||||
// /// 项目 API 可以重写该属性以使用自己的服务地址。
|
||||
// /// </summary>
|
||||
// protected virtual string ServiceConfigKey => ServiceConfigKeys.Default;
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取当前服务配置。
|
||||
// /// </summary>
|
||||
// protected IApiServerConfig GetServiceConfig()
|
||||
// {
|
||||
// return GetServiceConfig(ServiceConfigKey);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取指定 Key 的服务配置。
|
||||
// /// </summary>
|
||||
// protected IApiServerConfig GetServiceConfig(string serviceConfigKey)
|
||||
// {
|
||||
// if (ServiceConfigProvider == null)
|
||||
// {
|
||||
// throw new InvalidOperationException("IServiceConfigProvider 未注入,请通过 DI 容器创建请求服务。");
|
||||
// }
|
||||
|
||||
// return ServiceConfigProvider.Get(serviceConfigKey);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 使用当前 Key 构建服务请求地址。
|
||||
// /// </summary>
|
||||
// protected string BuildServiceUrl(string urlPath)
|
||||
// {
|
||||
// return BuildServiceUrl(ServiceConfigKey, urlPath);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 使用指定 Key 构建服务请求地址。
|
||||
// /// </summary>
|
||||
// protected string BuildServiceUrl(string serviceConfigKey, string urlPath)
|
||||
// {
|
||||
// IApiServerConfig config = GetServiceConfig(serviceConfigKey);
|
||||
// return config.Host + "?" + config.ObjUrl + (urlPath ?? string.Empty);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取指定 Key 的基础地址。
|
||||
// /// </summary>
|
||||
// protected string GetServiceHost(string serviceConfigKey = null)
|
||||
// {
|
||||
// IApiServerConfig config = string.IsNullOrWhiteSpace(serviceConfigKey)
|
||||
// ? GetServiceConfig()
|
||||
// : GetServiceConfig(serviceConfigKey);
|
||||
// return config.Host;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 执行底层 HTTP 请求。
|
||||
// /// </summary>
|
||||
// protected HttpResult Send(ServerHttpItem item)
|
||||
// {
|
||||
// if (item == null)
|
||||
// {
|
||||
// throw new ArgumentNullException(nameof(item));
|
||||
// }
|
||||
// if (string.IsNullOrWhiteSpace(item.URL))
|
||||
// {
|
||||
// throw new ArgumentException("请求地址不能为空。", nameof(item));
|
||||
// }
|
||||
|
||||
// if (!item.URL.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
||||
// {
|
||||
// item.URL = GetServiceConfig().Host + "/" + item.URL.TrimStart('/');
|
||||
// }
|
||||
|
||||
// item.Encoding = Encoding.UTF8;
|
||||
// HttpResult result = HttpHelper.Request(item);
|
||||
// if (result.StatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(result.Html) && result.Html[0] == 65279)
|
||||
// {
|
||||
// result.Html = result.Html.Remove(0, 1);
|
||||
// }
|
||||
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
Reference in new issue
Block a user