@ -11,6 +11,7 @@ using System.Windows;
using System.Linq ;
using System.Linq ;
using System.Threading ;
using System.Threading ;
using Newtonsoft.Json.Linq ;
using Newtonsoft.Json.Linq ;
using PuppeteerSharp ;
namespace JdShopListener
namespace JdShopListener
{
{
@ -26,7 +27,7 @@ namespace JdShopListener
}
}
}
}
public string JDCookie { get ; set ; }
public MainWindowViewModel ( ) {
public MainWindowViewModel ( ) {
ProList = new System . Collections . ObjectModel . ObservableCollection < ProModel > ( ) {
ProList = new System . Collections . ObjectModel . ObservableCollection < ProModel > ( ) {
new ProModel ( ) { Name = "价格" , Type = ProType . Price } ,
new ProModel ( ) { Name = "价格" , Type = ProType . Price } ,
@ -231,6 +232,13 @@ namespace JdShopListener
{
{
try
try
{
{
if ( string . IsNullOrEmpty ( JDCookie ) )
{
AddLog ( "开始获取Cookie!" ) ;
InitLoginCookie ( ) ;
}
var detail = GetItemDetail ( Sku ) ;
var detail = GetItemDetail ( Sku ) ;
JArray list = detail . product . colorSize as JArray ;
JArray list = detail . product . colorSize as JArray ;
@ -380,11 +388,19 @@ namespace JdShopListener
IsStart = true ;
IsStart = true ;
Thread t = new Thread ( ( ) = >
Thread t = new Thread ( ( ) = >
{
{
LastItemChangeList . Clear ( ) ;
LastItemChangeList = DbHelper . Db . GetLastItemChanges ( ) ;
while ( IsStart = = true )
while ( IsStart = = true )
{
{
if ( string . IsNullOrEmpty ( JDCookie ) )
{
AddLog ( "开始获取Cookie!" ) ;
InitLoginCookie ( ) ;
}
LastItemChangeList . Clear ( ) ;
LastItemChangeList = DbHelper . Db . GetLastItemChanges ( ) ;
AddLog ( "开始sku定时监控!" ) ;
AddLog ( "开始sku定时监控!" ) ;
DoWork ( ) ;
DoWork ( ) ;
@ -629,8 +645,18 @@ namespace JdShopListener
try
try
{
{
HttpClient http = new HttpClient ( ) ;
HttpClient http = new HttpClient ( ) ;
http . DefaultRequestHeaders . Add ( "Cookie" , JDCookie ) ;
http . DefaultRequestHeaders . Add ( "User-Agent" , "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0" ) ;
string html = http . GetStringAsync ( $"https://item.jd.com/{skuId}.html" ) . Result ;
string html = http . GetStringAsync ( $"https://item.jd.com/{skuId}.html" ) . Result ;
if ( html . Contains ( "passport.jd.com/new/login.aspx" ) )
{
AddLog ( "登录失效,触发验证!!重新尝试获取登录状态" ) ;
InitLoginCookie ( ) ;
return GetItemDetail ( skuId ) ;
}
int start = html . IndexOf ( "var pageConfig" ) ;
int start = html . IndexOf ( "var pageConfig" ) ;
int end = html . IndexOf ( "};" , start ) ;
int end = html . IndexOf ( "};" , start ) ;
@ -839,7 +865,19 @@ namespace JdShopListener
string url = $"https://item-soa.jd.com/getWareBusiness?callback=jQuery6834366&skuId={model.SkuId}&cat={model.Cat}&area=19_1666_1669_0&shopId={model.ShopId}&venderId={model.VenderId}¶mJson=%7B%22platform2%22%3A%221%22%2C%22colType%22%3A0%2C%22specialAttrStr%22%3A%22p0ppp1pppppp3ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1" ;
string url = $"https://item-soa.jd.com/getWareBusiness?callback=jQuery6834366&skuId={model.SkuId}&cat={model.Cat}&area=19_1666_1669_0&shopId={model.ShopId}&venderId={model.VenderId}¶mJson=%7B%22platform2%22%3A%221%22%2C%22colType%22%3A0%2C%22specialAttrStr%22%3A%22p0ppp1pppppp3ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1" ;
HttpClient client = new HttpClient ( ) ;
HttpClient client = new HttpClient ( ) ;
client . DefaultRequestHeaders . Add ( "Cookie" , JDCookie ) ;
client . DefaultRequestHeaders . Add ( "User-Agent" , "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0" ) ;
var json = client . GetStringAsync ( url ) . Result ;
var json = client . GetStringAsync ( url ) . Result ;
if ( json . Contains ( "passport.jd.com/new/login.aspx" ) )
{
AddLog ( "登录失效,触发验证!!重新尝试获取登录状态" ) ;
InitLoginCookie ( ) ;
return GetWareBusiness ( model ) ;
}
json = json . Remove ( 0 , "jQuery6834366(" . Length ) ;
json = json . Remove ( 0 , "jQuery6834366(" . Length ) ;
json = json . Remove ( json . Length - 1 , 1 ) ;
json = json . Remove ( json . Length - 1 , 1 ) ;
@ -859,6 +897,45 @@ namespace JdShopListener
return date ;
return date ;
}
}
/// <summary>
/// 刷新Cookie
/// </summary>
private void InitLoginCookie ( )
{
// 下载浏览器执行程序
// var fetcher= new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;
string url = "https://passport.jd.com/uc/login" ;
// 创建一个浏览器执行实例
using var browser = Puppeteer . LaunchAsync ( new LaunchOptions
{
Headless = true ,
//Args = new string[] { "--no-sandbox" }
} ) . Result ;
// 打开一个页面
var page = browser . NewPageAsync ( ) . Result ;
var a = page . GoToAsync ( url , WaitUntilNavigation . Networkidle0 ) . Result ;
var content = page . GetContentAsync ( ) . Result ;
page . DeleteCookieAsync ( ) . Wait ( ) ;
var cookies = page . GetCookiesAsync ( ) . Result ;
JDCookie = string . Empty ;
cookies . ToList ( ) . ForEach ( c = >
{
JDCookie + = $"{c.Name}={c.Value};" ;
} ) ;
}
}
}