This commit is contained in:
chfei authored and chfei committed 2026-09-14 09:16:59 +08:00
1 parent c2dcedd238
commit 16cdd3d46e
34 files changed
+837 -481

No files matched your search

@@ -1,9 +1,15 @@
using MAX.Core.Autofac;
using MAX.Core.Autofac;
using MAX.Core.Cryptography;
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using MAX.Core.Logger;
using Store.Client.Extensions;
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;
@@ -21,6 +27,7 @@ namespace Store.Client.HttpExtension
[Autowired]
protected ILoggerFacade _logger => LazyServiceProvider.Instance.LazyGetService<ILoggerFacade>();
protected IApiServerConfig _config => LazyServiceProvider.Instance.LazyGetService<IApiServerConfig>();
protected ICurrentUser CurrentUser => LazyServiceProvider.Instance.LazyGetService<ICurrentUser>();
/// <summary>
/// 默认服务请求。
@@ -69,39 +76,36 @@ namespace Store.Client.HttpExtension
{
return Request(BuildServiceUrl(urlpath), postdata);
}
/// <summary>
/// 使用当前 Key 构建服务请求地址。
/// </summary>
protected string BuildServiceUrl(string urlPath)
{
return $"{_config.Host}/wxxd_{urlPath}?obj=29wx/wxxd_{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)
{
ServerHttpItem item = new ServerHttpItem
HttpItem item = new HttpItem
{
URL = url,
Method = method,
Postdata = postdata ?? string.Empty
};
if (isUserSign)
{
item.SignUser();
}
ApplySign(item, isUserSign);
return ServerRequest(item);
return Send(item);
}
/// <summary>
/// 服务器请求。
/// </summary>
public HttpResult ServerRequest(ServerHttpItem item)
public HttpResult ServerRequest(HttpItem item)
{
if (item == null)
{
@@ -111,26 +115,15 @@ namespace Store.Client.HttpExtension
#if RELEASE
item.WebProxy = null;
#endif
if (!item.IsSign)
{
if (item.Shop != null)
{
item.SignUser(item.Shop.Id, item.Shop.Name);
}
else
{
item.Sign();
}
}
ApplySign(item, false);
return Send(item);
}
/// <summary>
/// 执行底层 HTTP 请求。
/// </summary>
protected HttpResult Send(ServerHttpItem item)
protected HttpResult Send(HttpItem item)
{
if (item == null)
{
@@ -155,5 +148,98 @@ namespace Store.Client.HttpExtension
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;
}
}
}
}
}
+25 -25
View File
@@ -1,28 +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;
//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()
{
//namespace Store.Client.HttpExtension
//{
// public class HttpItem : HttpItem
// {
// public ShopBase Shop { get; private set; }
// public HttpItem()
// {
}
public ServerHttpItem(ShopBase shop)
{
Shop = shop;
}
/// <summary>
/// 是否已签名
/// </summary>
public bool IsSign { get; set; }
}
}
// }
// public HttpItem(ShopBase shop)
// {
// Shop = shop;
// }
// /// <summary>
// /// 是否已签名
// /// </summary>
// public bool IsSign { get; set; }
// }
//}
@@ -76,7 +76,7 @@
// /// <summary>
// /// 执行底层 HTTP 请求。
// /// </summary>
// protected HttpResult Send(ServerHttpItem item)
// protected HttpResult Send(HttpItem item)
// {
// if (item == null)
// {