250 lines
8.1 KiB
C#
250 lines
8.1 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Text;
|
|
using WxOrder.Framework.Helpers;
|
|
|
|
namespace WxOrder.Framework.PDDAPI
|
|
{
|
|
/// <summary>
|
|
/// 拼多多API请求类
|
|
/// </summary>
|
|
public class PddApi
|
|
{
|
|
private const string BASE_URL = "https://mobile.yangkeduo.com";
|
|
|
|
/// <summary>
|
|
/// 获取发票详情页
|
|
/// </summary>
|
|
/// <param name="orderSn">订单号</param>
|
|
/// <param name="cookie">用户Cookie</param>
|
|
/// <returns>返回页面HTML内容</returns>
|
|
public static string GetReturnCompensation(string orderSn, string cookie)
|
|
{
|
|
string url = $"{BASE_URL}/transac_return_compensation.html?_t_timestamp=transac_modify_invoice&order_sn={orderSn}";
|
|
|
|
var item = new HttpItem
|
|
{
|
|
URL = url,
|
|
Method = MethodType.Get,
|
|
Timeout = 30000,
|
|
ReadWriteTimeout = 30000,
|
|
KeepAlive = true,
|
|
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
ContentType = "text/html",
|
|
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
|
|
Cookie = cookie,
|
|
Referer = BASE_URL,
|
|
Allowautoredirect = true,
|
|
Encoding = Encoding.UTF8,
|
|
ResultType = ResultType.String
|
|
};
|
|
|
|
// 添加自定义Header
|
|
item.Header.Add("sec-ch-ua", "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\"");
|
|
item.Header.Add("sec-ch-ua-mobile", "?0");
|
|
item.Header.Add("sec-ch-ua-platform", "\"Windows\"");
|
|
item.Header.Add("upgrade-insecure-requests", "1");
|
|
item.Header.Add("sec-fetch-site", "none");
|
|
item.Header.Add("sec-fetch-mode", "navigate");
|
|
item.Header.Add("sec-fetch-user", "?1");
|
|
item.Header.Add("sec-fetch-dest", "document");
|
|
item.Header.Add("priority", "u=0, i");
|
|
item.ProxyIp = "127.0.0.1:8080";
|
|
var result = new HttpHelper().GetHtml(item);
|
|
|
|
if (result.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
throw new Exception($"请求失败: {result.StatusDescription}, 状态码: {result.StatusCode}");
|
|
}
|
|
|
|
return result.Html;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步获取退货补偿页面信息
|
|
/// </summary>
|
|
/// <param name="orderSn">订单号</param>
|
|
/// <param name="cookie">用户Cookie</param>
|
|
/// <returns>返回页面HTML内容</returns>
|
|
public static async System.Threading.Tasks.Task<string> GetReturnCompensationAsync(string orderSn, string cookie)
|
|
{
|
|
return await System.Threading.Tasks.Task.Run(() => GetReturnCompensation(orderSn, cookie));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取退货补偿页面信息(带完整Cookie字符串)
|
|
/// </summary>
|
|
/// <param name="orderSn">订单号</param>
|
|
/// <param name="accessToken">PDD访问令牌</param>
|
|
/// <param name="userId">用户ID</param>
|
|
/// <param name="userUin">用户UIN</param>
|
|
/// <param name="extraCookies">额外Cookie键值对</param>
|
|
/// <returns>返回页面HTML内容</returns>
|
|
public static string GetReturnCompensationWithCookies(
|
|
string orderSn,
|
|
string accessToken,
|
|
string userId,
|
|
string userUin,
|
|
System.Collections.Generic.Dictionary<string, string> extraCookies = null)
|
|
{
|
|
// 构建Cookie字符串
|
|
var cookieBuilder = new StringBuilder();
|
|
cookieBuilder.Append($"PDDAccessToken={accessToken}; ");
|
|
cookieBuilder.Append($"pdd_user_id={userId}; ");
|
|
cookieBuilder.Append($"pdd_user_uin={userUin}; ");
|
|
|
|
// 添加额外Cookie
|
|
if (extraCookies != null)
|
|
{
|
|
foreach (var kv in extraCookies)
|
|
{
|
|
cookieBuilder.Append($"{kv.Key}={kv.Value}; ");
|
|
}
|
|
}
|
|
|
|
// 添加一些常用的固定Cookie
|
|
cookieBuilder.Append("api_uid=CiNLVGqFh3IfyACAene3Ag==; ");
|
|
cookieBuilder.Append("webp=1; ");
|
|
cookieBuilder.Append("dilx=3EDqgJtFtp4E9OYvXLozM; ");
|
|
|
|
string cookie = cookieBuilder.ToString().TrimEnd(' ', ';');
|
|
|
|
return GetReturnCompensation(orderSn, cookie);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构建完整的拼多多请求Cookie
|
|
/// </summary>
|
|
/// <param name="cookies">Cookie键值对</param>
|
|
/// <returns>Cookie字符串</returns>
|
|
public static string BuildCookieString(System.Collections.Generic.Dictionary<string, string> cookies)
|
|
{
|
|
if (cookies == null || cookies.Count == 0)
|
|
return string.Empty;
|
|
|
|
var builder = new StringBuilder();
|
|
foreach (var kv in cookies)
|
|
{
|
|
builder.Append($"{kv.Key}={kv.Value}; ");
|
|
}
|
|
return builder.ToString().TrimEnd(' ', ';');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通用拼多多请求方法
|
|
/// </summary>
|
|
/// <param name="path">请求路径</param>
|
|
/// <param name="queryParams">查询参数</param>
|
|
/// <param name="cookie">Cookie字符串</param>
|
|
/// <param name="method">请求方法</param>
|
|
/// <param name="postData">POST数据</param>
|
|
/// <returns>返回结果</returns>
|
|
public static string Request(
|
|
string path,
|
|
System.Collections.Generic.Dictionary<string, string> queryParams = null,
|
|
string cookie = null,
|
|
MethodType method = MethodType.Get,
|
|
string postData = null)
|
|
{
|
|
// 构建完整URL
|
|
var urlBuilder = new StringBuilder(BASE_URL);
|
|
urlBuilder.Append(path);
|
|
|
|
if (queryParams != null && queryParams.Count > 0)
|
|
{
|
|
urlBuilder.Append('?');
|
|
int index = 0;
|
|
foreach (var kv in queryParams)
|
|
{
|
|
if (index > 0) urlBuilder.Append('&');
|
|
urlBuilder.Append($"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}");
|
|
index++;
|
|
}
|
|
}
|
|
|
|
var item = new HttpItem
|
|
{
|
|
URL = urlBuilder.ToString(),
|
|
Method = method,
|
|
Timeout = 30000,
|
|
ReadWriteTimeout = 30000,
|
|
KeepAlive = true,
|
|
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
ContentType = "application/x-www-form-urlencoded",
|
|
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
|
|
Cookie = cookie,
|
|
Referer = BASE_URL,
|
|
Allowautoredirect = true,
|
|
Encoding = Encoding.UTF8,
|
|
ResultType = ResultType.String,
|
|
Postdata = postData
|
|
};
|
|
|
|
// 添加安全相关Header
|
|
item.Header.Add("sec-ch-ua", "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\"");
|
|
item.Header.Add("sec-ch-ua-mobile", "?0");
|
|
item.Header.Add("sec-ch-ua-platform", "\"Windows\"");
|
|
item.Header.Add("upgrade-insecure-requests", "1");
|
|
item.Header.Add("sec-fetch-site", "none");
|
|
item.Header.Add("sec-fetch-mode", "navigate");
|
|
item.Header.Add("sec-fetch-dest", "document");
|
|
|
|
var result = new HttpHelper().GetHtml(item);
|
|
|
|
if (result.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
throw new Exception($"请求失败: {result.StatusDescription}, 状态码: {result.StatusCode}");
|
|
}
|
|
|
|
return result.Html;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拼多多Cookie构建帮助类
|
|
/// </summary>
|
|
public static class PddCookieHelper
|
|
{
|
|
/// <summary>
|
|
/// 从各个Cookie片段构建完整Cookie
|
|
/// </summary>
|
|
public static string BuildCookie(PddCookieInfo info)
|
|
{
|
|
var builder = new StringBuilder();
|
|
|
|
if (!string.IsNullOrEmpty(info.ApiUid))
|
|
builder.Append($"api_uid={info.ApiUid}; ");
|
|
if (!string.IsNullOrEmpty(info.NanoFp))
|
|
builder.Append($"_nano_fp={info.NanoFp}; ");
|
|
if (!string.IsNullOrEmpty(info.AccessToken))
|
|
builder.Append($"PDDAccessToken={info.AccessToken}; ");
|
|
if (!string.IsNullOrEmpty(info.UserId))
|
|
builder.Append($"pdd_user_id={info.UserId}; ");
|
|
if (!string.IsNullOrEmpty(info.UserUin))
|
|
builder.Append($"pdd_user_uin={info.UserUin}; ");
|
|
if (!string.IsNullOrEmpty(info.PddVds))
|
|
builder.Append($"pdd_vds={info.PddVds}; ");
|
|
|
|
// 添加固定值
|
|
builder.Append("webp=1; ");
|
|
builder.Append("dilx=3EDqgJtFtp4E9OYvXLozM; ");
|
|
|
|
return builder.ToString().TrimEnd(' ', ';');
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拼多多Cookie信息类
|
|
/// </summary>
|
|
public class PddCookieInfo
|
|
{
|
|
public string ApiUid { get; set; } = "CiNLVGqFh3IfyACAene3Ag==";
|
|
public string NanoFp { get; set; } = "XpmJnqgqnpCynqEYnC_vqOqYD6xToHisZXFzP~Qy";
|
|
public string AccessToken { get; set; }
|
|
public string UserId { get; set; }
|
|
public string UserUin { get; set; }
|
|
public string PddVds { get; set; }
|
|
public string Jrpl { get; set; } = "m4Tc1seqEgg0YoYo7U4GeUxzylfpG8W7";
|
|
public string Njrpl { get; set; } = "m4Tc1seqEgg0YoYo7U4GeUxzylfpG8W7";
|
|
}
|
|
} |