246 lines
6.0 KiB
C#
246 lines
6.0 KiB
C#
using MAX.Core.Autofac;
|
|
using MAX.Core.Cryptography;
|
|
using MAX.Core.DependencyInjection;
|
|
using MAX.Core.Helpers;
|
|
using MAX.Core.Logger;
|
|
using Store.Client.Agent;
|
|
using Store.Framework;
|
|
using Store.Framework.Inerterfaces;
|
|
using Store.Framework.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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>();
|
|
protected ICurrentUser CurrentUser => LazyServiceProvider.Instance.LazyGetService<ICurrentUser>();
|
|
|
|
/// <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={Config.MainPath}/wxxd_{urlPath}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建并执行服务端请求,默认使用当前用户签名。
|
|
/// </summary>
|
|
protected HttpResult Request(string url, string postdata = "", MethodType method = MethodType.Post, bool isUserSign = true)
|
|
{
|
|
HttpItem item = new HttpItem
|
|
{
|
|
URL = url,
|
|
Method = method,
|
|
Postdata = postdata ?? string.Empty
|
|
};
|
|
|
|
ApplySign(item, isUserSign);
|
|
|
|
return Send(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 服务器请求。
|
|
/// </summary>
|
|
public HttpResult ServerRequest(HttpItem item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(item));
|
|
}
|
|
|
|
#if RELEASE
|
|
item.WebProxy = null;
|
|
#endif
|
|
ApplySign(item, false);
|
|
|
|
return Send(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行底层 HTTP 请求。
|
|
/// </summary>
|
|
protected HttpResult Send(HttpItem 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 为请求添加公共签名或用户签名。
|
|
/// </summary>
|
|
private void ApplySign(HttpItem item, bool isUserSign)
|
|
{
|
|
string sign = isUserSign
|
|
? GetUserSign("", "")
|
|
: GetSign();
|
|
|
|
SetSign(item, sign);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取公共签名。
|
|
/// </summary>
|
|
private static string GetSign()
|
|
{
|
|
long time = GetReviseUnix();
|
|
string code = Encryption(time);
|
|
return "time=" + time + "&code=" + code + "&NbCodeVersion=1";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户签名。
|
|
/// </summary>
|
|
private string GetUserSign(string shopId, string shopName)
|
|
{
|
|
UserInfo user = CurrentUser.User;
|
|
if (user == null)
|
|
{
|
|
throw new InvalidOperationException("当前用户未登录,无法生成用户签名。");
|
|
}
|
|
|
|
long time = GetReviseUnix();
|
|
string code = Encryption(time);
|
|
var dict = new Dictionary<string, string>
|
|
{
|
|
{ "time", time.ToString() },
|
|
{ "code", code },
|
|
{ "code_version", "1" },
|
|
{ "version", Config.Version },
|
|
{ "user_name", user.UserName },
|
|
{ "path", Config.MainPath }
|
|
};
|
|
if (!string.IsNullOrEmpty(AgentConfig.AgentCode))
|
|
{
|
|
dict.Add("agent_code", AgentConfig.AgentCode);
|
|
dict.Add("agent_name", AgentConfig.AgentName);
|
|
}
|
|
|
|
return string.Join("&", dict.Select(kv => $"{kv.Key}={kv.Value}"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取校正后的时间戳。
|
|
/// </summary>
|
|
protected static long GetReviseUnix()
|
|
{
|
|
return DateHelper.GetCurrentUnixTimestampInSeconds() + ServerConfig.TimeDifferent;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 签名算法。
|
|
/// </summary>
|
|
/// <param name="s">随机数</param>
|
|
/// <returns>加密签名结果</returns>
|
|
protected static string Encryption(long s)
|
|
{
|
|
string value = $"{s}sdhewu*/fhAAue48485ew5+5%@3485esGFFHFD";
|
|
return MD5Helper.Md5_32(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将签名参数拼接到请求。
|
|
/// </summary>
|
|
private static void SetSign(HttpItem item, string sign)
|
|
{
|
|
if (item.Method == MethodType.Get)
|
|
{
|
|
item.URL += item.URL.Contains("?") ? "&" : "?";
|
|
item.URL += sign;
|
|
}
|
|
else
|
|
{
|
|
item.Postdata = item.Postdata ?? string.Empty;
|
|
if (item.Postdata.Length > 0)
|
|
{
|
|
item.Postdata += "&";
|
|
}
|
|
item.Postdata += sign;
|
|
}
|
|
}
|
|
}
|
|
}
|