224 lines
5.8 KiB
C#
224 lines
5.8 KiB
C#
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(""", "\"");
|
||
}
|
||
}
|
||
} |