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

+156 -29
View File
@@ -1,21 +1,26 @@
using System;
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;
namespace Store.Framework.Models
{
/// <summary>
/// Cookie集合,支持普通 Cookie 字符串和 API 返回的 Cookie JSON。
/// </summary>
[JsonConverter(typeof(CookiesJsonConverter))]
public class Cookies : Dictionary<string, string>
{
private readonly string oldCookie;
public Cookies(string cookie)
{
oldCookie = cookie;
SplitCookie(oldCookie);
Load(cookie);
}
/// <summary>
/// Cookie字符串
/// </summary>
@@ -26,28 +31,6 @@ namespace Store.Framework.Models
return GetCookieString();
}
}
/// <summary>
/// 拆分Cookie
/// </summary>
private void SplitCookie(string cookie)
{
this.Clear();
Regex regCookie = new Regex("(?<key>.+?)=(?<value>.+?)(;|$)");
if (regCookie.IsMatch(cookie))
{
MatchCollection matchs = regCookie.Matches(cookie);
foreach (Match item in matchs)
{
string key = item.Groups["key"].Value.Trim();
string value = item.Groups["value"].Value.Trim();
if (!ContainsKey(key))
{
this.Add(key.Trim(), value.Trim());
}
}
}
}
/// <summary>
/// 获取指定值
@@ -63,6 +46,7 @@ namespace Store.Framework.Models
}
return defvalue;
}
/// <summary>
/// 添加或修改
/// </summary>
@@ -79,6 +63,7 @@ namespace Store.Framework.Models
this[key] = value;
}
}
/// <summary>
/// 获取Cookies字符串
/// </summary>
@@ -89,14 +74,156 @@ namespace Store.Framework.Models
return string.Join(";", list);
}
/// <summary>
/// 转为浏览器 Cookie 字符串。
/// </summary>
public override string ToString()
{
var list = this.Select(a => { return $"{a.Key}={a.Value}"; });
return string.Join("; ", list);
}
/// <summary>
/// 隐式转换
/// </summary>
/// <param name="cookie"></param>
public static implicit operator string (Cookies cookie)
public static implicit operator string(Cookies cookie)
{
return cookie.GetCookieString();
}
internal static Cookies FromToken(JToken token)
{
Cookies cookies = new Cookies(string.Empty);
if (token == null || token.Type == JTokenType.Null)
return cookies;
if (token.Type == JTokenType.String)
return new Cookies(token.ToString());
cookies.AddToken(token);
return cookies;
}
private void Load(string cookie)
{
Clear();
if (string.IsNullOrWhiteSpace(cookie))
return;
try
{
AddToken(JToken.Parse(cookie));
if (Count > 0)
return;
}
catch (JsonException)
{
}
SplitCookie(Decode(cookie));
}
private void AddToken(JToken token)
{
if (token == null || token.Type == JTokenType.Null)
return;
if (token is JArray array)
{
foreach (JToken item in array)
{
AddCookie(item);
}
return;
}
AddCookie(token);
}
private void AddCookie(JToken token)
{
if (token == null || token.Type == JTokenType.Null)
return;
if (token.Type == JTokenType.String)
{
string value = token.ToString();
int index = value.IndexOf('=');
if (index <= 0)
return;
AddOrUpdate(value.Substring(0, index).Trim(), value.Substring(index + 1).Trim());
return;
}
JToken name = token["Name"] ?? token["name"];
JToken valueToken = token["Value"] ?? token["value"];
if (name == null || valueToken == null)
return;
AddOrUpdate(name.ToString(), valueToken.ToString());
}
/// <summary>
/// 拆分普通 Cookie 字符串。
/// </summary>
private void SplitCookie(string cookie)
{
Clear();
if (string.IsNullOrEmpty(cookie))
return;
Regex regCookie = new Regex("(?<key>.+?)=(?<value>.+?)(;|$)");
if (regCookie.IsMatch(cookie))
{
MatchCollection matchs = regCookie.Matches(cookie);
foreach (Match item in matchs)
{
string key = item.Groups["key"].Value.Trim();
string value = item.Groups["value"].Value.Trim();
if (!ContainsKey(key))
{
this.Add(key, value);
}
}
}
}
private static string Decode(string value)
{
return (value ?? string.Empty)
.Replace("&", "&")
.Replace("%", "%")
.Replace("+", "+")
.Replace(""", "\"");
}
}
}
/// <summary>
/// Cookie JSON 转换器。
/// </summary>
public class CookiesJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Cookies).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
if (reader.TokenType == JsonToken.String)
return new Cookies(reader.Value?.ToString());
return Cookies.FromToken(JToken.Load(reader));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Cookies cookies = value as Cookies;
writer.WriteValue(cookies?.GetCookieString());
}
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace Store.Framework.Models
{
/// <summary>
/// 店铺信息。
/// </summary>
public class Shop : ShopBase
{
}
}
+17 -6
View File
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -9,35 +10,45 @@ namespace Store.Framework.Models
/// <summary>
/// 商铺信息
/// </summary>
public class ShopBase
public abstract class ShopBase
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("shop_account")]
public string Name { get; set; }
[JsonProperty("login_pass")]
public string Password { get; set; }
/// <summary>
/// Cookie信息
public Cookies Cookie { get; set; }
/// </summary>
[JsonProperty("cookie")]
public Cookies Cookie { get; set; }
/// <summary>
/// 分组名称
/// </summary>
[JsonProperty("group_name")]
public string GroupName { get; set; }
/// <summary>
/// 商铺提示信息
/// </summary>
[JsonProperty("login_status")]
public string Msg { get; set; }
}
/// <summary>
/// 商铺信息
/// </summary>
/// <typeparam name="TStatus">状态码</typeparam>
public class ShopBase<TStatus> : ShopBase
public abstract class ShopBase<TStatus> : ShopBase
{
/// <summary>
/// 状态
/// </summary>
public TStatus Status { get; set; }
}
}
}