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

+10 -6
View File
@@ -10,11 +10,11 @@ using System.Threading.Tasks;
namespace Store.Framework.Services;
public record UserNotification(UserInfo user) : INotification;
//public record UserNotification(UserInfo user) : INotification;
/// <summary>
/// 当前用户
/// </summary>
internal class CurrentUserService : ICurrentUser, ISingletonDependency, INotificationHandler<UserNotification>
internal class CurrentUserService : ICurrentUser, ISingletonDependency//, INotificationHandler<UserNotification>
{
public string UserName => User?.UserName;
@@ -28,10 +28,14 @@ internal class CurrentUserService : ICurrentUser, ISingletonDependency, INotific
{
return User.UserAuthority.Contains(code);
}
Task INotificationHandler<UserNotification>.Handle(UserNotification notification, CancellationToken cancellationToken)
internal void SetUser(UserInfo user)
{
User = notification.user;
return Task.CompletedTask;
User = user;
}
//Task INotificationHandler<UserNotification>.Handle(UserNotification notification, CancellationToken cancellationToken)
//{
// User = notification.user;
// return Task.CompletedTask;
//}
}
+224
View File
@@ -0,0 +1,224 @@
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Store.Client.Apis;
using Store.Framework.Apis;
using Store.Framework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace Store.Client.Services
{
/// <summary>
/// 商铺管理服务
/// </summary>
public class ShopManager : ITransientDependency
{
private const int DefaultPageSize = 100;
private const int MaxPageCount = 10000;
private readonly ShopApi _shopApi;
public ShopManager(ShopApi shopApi)
{
_shopApi = shopApi ?? throw new ArgumentNullException(nameof(shopApi));
}
/// <summary>
/// 获取当前用户的所有商铺。
/// </summary>
/// <param name="pageSize">每页数量。</param>
/// <returns>商铺列表。</returns>
public List<Shop> GetShops(int pageSize = DefaultPageSize)
{
return GetShops(false, pageSize);
}
/// <summary>
/// 获取商铺列表。
/// </summary>
/// <param name="isAdmin">是否调用管理端接口。</param>
/// <param name="pageSize">每页数量。</param>
/// <returns>商铺列表。</returns>
public List<Shop> GetShops(bool isAdmin, int pageSize = DefaultPageSize)
{
List<Shop> shops = new List<Shop>();
if (pageSize <= 0)
pageSize = DefaultPageSize;
int page = 0;
while (page < MaxPageCount)
{
page++;
string postdata = "page=" + page + "&page_size=" + pageSize;
HttpResult result = isAdmin
? _shopApi.AdminGetShopList(postdata)
: _shopApi.GetShopList(postdata);
ApiResponse<List<Shop>> response = DeserializeShopResponse(result);
if (response == null || response.Code != 200 || response.Data == null)
break;
if (response.Data.Count == 0)
break;
shops.AddRange(response.Data);
if (response.Data.Count < pageSize)
break;
}
return shops;
}
/// <summary>
/// 获取服务端商铺列表,兼容旧调用。
/// </summary>
public List<Shop> GetServerShop()
{
return GetShops();
}
/// <summary>
/// 从服务端刷新商铺 Cookie。
/// </summary>
/// <param name="shops">待刷新商铺。</param>
/// <returns>服务端原始响应。</returns>
public string GetShopCookie(List<Shop> shops)
{
if (shops == null || shops.Count == 0)
return string.Empty;
string shopJson = JsonConvert.SerializeObject(shops);
string postdata = "shops=" + Uri.EscapeDataString(shopJson);
HttpResult result = _shopApi.GetPddShopCookiesList(postdata);
if (result == null || result.StatusCode != HttpStatusCode.OK || string.IsNullOrEmpty(result.Html))
return string.Empty;
MatchCollection matches = Regex.Matches(
result.Html,
@"<shop\s+(?:(?:id|name)=""(?<key>[^""]*?)""[^>]*)>(?<json>[\w\W]*?)</shop>",
RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
string key = match.Groups["key"].Value;
Shop shop = shops.FirstOrDefault(s => s.Id == key || s.Name == key);
if (shop == null)
continue;
string cookie = ParseCookie(match.Groups["json"].Value);
if (cookie != null)
shop.Cookie = new Cookies(DecodeCookie(cookie));
}
return result.Html;
}
/// <summary>
/// 检测商铺登录状态。
/// </summary>
/// <param name="shop">商铺。</param>
public void CheckShopStatus(Shop shop)
{
if (shop == null)
throw new ArgumentNullException(nameof(shop));
if (shop.Cookie == null)
{
shop.Msg = "Cookie为空";
return;
}
HttpItem item = new HttpItem
{
URL = "https://myseller.taobao.com/ajaxProxy.do?_ksTS=1548152154745_13&callback=seller_layout_head&action=FrameworkLayoutAction&event_submit_do_layout_data=true",
Referer = "https://myseller.taobao.com",
Method = MethodType.Post,
Cookie = shop.Cookie.GetCookieString()
};
HttpResult result = HttpHelper.Request(item);
shop.Msg = result != null
&& result.StatusCode == HttpStatusCode.OK
&& !string.IsNullOrEmpty(result.Html)
&& result.Html.Contains("\"shopName\":\"")
? "登录成功"
: "会话已过期";
}
/// <summary>
/// 删除服务端商铺。
/// </summary>
/// <param name="loginName">店铺登录名。</param>
/// <returns>服务端原始响应。</returns>
public string DeleteShop(string loginName)
{
if (string.IsNullOrWhiteSpace(loginName))
throw new ArgumentException("店铺登录名不能为空。", nameof(loginName));
HttpResult result = _shopApi.DelShop("login_name=" + Uri.EscapeDataString(loginName));
return result?.Html ?? string.Empty;
}
/// <summary>
/// 删除服务端店铺,兼容旧方法命名。
/// </summary>
public string DelServerDouShop(string shopId)
{
return DeleteShop(shopId);
}
private static ApiResponse<List<Shop>> DeserializeShopResponse(HttpResult result)
{
if (result == null || result.StatusCode != HttpStatusCode.OK || string.IsNullOrWhiteSpace(result.Html))
return null;
try
{
return JsonConvert.DeserializeObject<ApiResponse<List<Shop>>>(result.Html);
}
catch (JsonException)
{
return null;
}
}
private static string ParseCookie(string json)
{
if (string.IsNullOrWhiteSpace(json))
return null;
try
{
JObject root = JObject.Parse(json);
return GetString(root, "Cookies") ?? GetString(root, "cookie") ?? GetString(root, "Cookie");
}
catch (JsonException)
{
return null;
}
}
private static string GetString(JObject source, string propertyName)
{
JToken token = source?[propertyName];
return token == null || token.Type == JTokenType.Null
? null
: token.ToString();
}
private static string DecodeCookie(string cookie)
{
return (cookie ?? string.Empty)
.Replace("&", "&")
.Replace("%", "%")
.Replace("+", "+")
.Replace(""", "\"");
}
}
}
-134
View File
@@ -1,134 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
//using FkServices = LittleCow.Framework.Services;
namespace Store.Client.Services
{
///// <summary>
///// 商铺服务
///// </summary>
//internal class ShopService : ITransientDependency
//{
// private readonly ShopApi _shopApi;
// private readonly IEventBus _eventBus;
// public ShopService(ShopApi shopApi, IEventBus eventBus)
// {
// _shopApi = shopApi;
// _eventBus = eventBus;
// }
// /// <summary>
// /// 获取所有商铺信息
// /// </summary>
// /// <typeparam name="T"></typeparam>
// /// <returns></returns>
// public List<T> GetShops<T>() where T : ShopBase
// {
// HttpResult result = _shopApi.GetShops();
// if (result.StatusCode == System.Net.HttpStatusCode.OK)
// {
// StringBuilder sb = new StringBuilder();
// sb.Append("[");
// Regex regShopList = new Regex("<shop([^>]+?)>");
// if (regShopList.IsMatch(result.Html))
// {
// int index = 0;
// MatchCollection matchs = regShopList.Matches(result.Html);
// foreach (Match item in matchs)
// {
// string value = item.Value;
// value = value.Replace("<shop '", "{\"");
// value = value.Replace("'>", "\"}");
// value = value.Replace("':'", "\":\"");
// value = value.Replace("' '", "\",\"");
// if (index > 0)
// {
// sb.Append(",");
// }
// sb.Append(value);
// index++;
// }
// sb.Append("]");
// List<T> list = JsonConvert.DeserializeObject<List<T>>(sb.ToString());
// list.RemoveAll(a => string.IsNullOrEmpty(a.Name));
// return list;
// }
// }
// return new List<T>();
// }
// public string GetShopCookie(List<ShopBase> shops)
// {
// HttpResult result = _shopApi.GetShopsCookie(shops);
// if (result.StatusCode == System.Net.HttpStatusCode.OK)
// {
// MatchCollection matchs = Regex.Matches(result.Html, @"<shop id=""([^""]+?)"">([\w\W]+?)</shop>");
// if (matchs != null)
// {
// foreach (Match item in matchs)
// {
// string shopId = item.Groups[1].Value;
// string json = item.Groups[2].Value;
// var rootToken = JToken.Parse(json);
// string cookie = rootToken["Cookies"]?.ToString() ?? "";
// var shop = shops.FirstOrDefault(a => a.Id == shopId);
// if (shop != null)
// {
// shop.Cookie = new Cookies(cookie);
// }
// }
// }
// }
// return null;
// }
// /// <summary>
// /// 检测商铺登录状态
// /// </summary>
// public void CheckShopStatus(ShopBase shop)
// {
// HttpItem item = new HttpItem();
// item.URL = "https://myseller.taobao.com/ajaxProxy.do?_ksTS=1548152154745_13&callback=seller_layout_head&action=FrameworkLayoutAction&event_submit_do_layout_data=true";
// item.Referer = "https://myseller.taobao.com";
// item.Method = MethodType.Post;
// item.Cookie = shop.Cookie.GetCookieString();
// HttpResult result = HttpHelper.Request(item);
// if (result.StatusCode == System.Net.HttpStatusCode.OK)
// {
// string a = result.Html.Contains("\"shopName\":\"") ? "登录成功" : "会话已过期";
// }
// //string urlx =
// //string refere =
// //string htmlx = GetPostTaobaoHtml.GetUrl(urlx, refere, true, "", Shop.Cookies);
// //Shop.MsgForCookiesLogin = htmlx.Contains("\"shopName\":\"") ? "登录成功" : "会话已过期";
// //GetTaobaoShops.ShopsTemp.Add(Shop);
// }
// /// <summary>
// /// 获取值
// /// </summary>
// /// <param name="html"></param>
// /// <param name="reg"></param>
// /// <returns></returns>
// private string GetUserValue(string html, string reg)
// {
// string temp = Regex.Match(html, @"[""]" + reg + @"[""][\s]*:[\s]*[""]([^""]*?)[""]")?.Value;
// if (string.IsNullOrEmpty(temp))
// temp = Regex.Match(html, @"[']" + reg + @"['][\s]*:[\s]*[']([^']*?)[']")?.Groups[1].Value;
// if (string.IsNullOrEmpty(temp))
// temp = Regex.Match(html, @"['""]" + reg + @"['""][\s]*:[\s]*([\d]+)")?.Value;
// return temp;
// }
//}
}
+7 -1
View File
@@ -5,7 +5,9 @@ using MAX.Core.Helpers;
using Newtonsoft.Json;
using Store.Client.Apis;
using Store.Framework.Apis;
using Store.Framework.Inerterfaces;
using Store.Framework.Models;
using Store.Framework.Services;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -21,10 +23,12 @@ namespace Store.Client.Services
public class UserService : ITransientDependency
{
private readonly UserApi _userApi;
private readonly ICurrentUser _currentUser;
public UserService(UserApi userApi)
public UserService(UserApi userApi, ICurrentUser currentUser)
{
_userApi = userApi;
_currentUser = currentUser;
}
/// <summary>
@@ -75,6 +79,8 @@ namespace Store.Client.Services
ret.Success = true;
ret.Msg = apiResponse.Msg;
ret.Data = result;
(_currentUser as CurrentUserService).SetUser(user);
//_mediator.Publish(new UserNotification(user));
return ret;
}