京东慧眼
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
16 KiB

using CefSharp;
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace .UserControls
{
/// <summary>
/// BrowerControl.xaml 的交互逻辑
/// </summary>
public partial class BrowerControl : UserControl
{
public static BrowerControl Main = null;
public ChromiumWebBrowser web;
public BrowerControl()
{
InitializeComponent();
web = new ChromiumWebBrowser("https://www.taobao.com")
{
BrowserSettings =
{
DefaultEncoding = "UTF-8",
Plugins= CefState.Enabled,
//关于跨域限制
WebSecurity = CefState.Disabled,
ApplicationCache=CefState.Enabled,
LocalStorage= CefState.Enabled
},
};
grid.Children.Add(web);
Loaded += BrowerControl_Loaded;
web.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
web.JavascriptObjectRepository.Register("hyCoreModel", new CefAsyncJS(), BindingOptions.DefaultBinder);
Main = this;
}
private void BrowerControl_Loaded(object sender, RoutedEventArgs e)
{
Thread t = new Thread(() => {
IWebBrowser webBrowser = null;
Application.Current.Dispatcher.Invoke(() => {
webBrowser = web.WebBrowser;
});
while (webBrowser == null)
{
Thread.Sleep(100);
Application.Current.Dispatcher.Invoke(() => {
webBrowser = web.WebBrowser;
});
}
Application.Current.Dispatcher.Invoke(() => {
web.WebBrowser.LoadingStateChanged += WebBrowser_LoadingStateChanged;
});
});
t.Start();
}
private string lastUrl = string.Empty;
private void WebBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (lastUrl != e.Browser.MainFrame.Url)
{
lastUrl = e.Browser.MainFrame.Url;
//开始单页查询
if (lastUrl.StartsWith("https://s.taobao.com/search"))
{
StartPage();
}
}
}
object doWorkObj = new object();
private void StartPage()
{
Thread t = new Thread(() =>
{
lock (doWorkObj)
{
AddJquery();
while (true)
{
Task<JavascriptResponse> task = null;
Application.Current.Dispatcher.Invoke(() =>
{
task = web.EvaluateScriptAsPromiseAsync("return $($('div#mainsrp-itemlist .items .item')[0]).html();");
});
var result = task.Result;
//判断是否加载完成
if (result.Success)
{
AddOptionDiv();
CheckPageItem();
lastUrl = string.Empty;
break;
}
}
}
});
t.Start();
}
/// <summary>
/// 检测页面商品
/// </summary>
/// <returns></returns>
private void CheckPageItem()
{
var res = DoJavaScript(@"var ids=[]; $('div#mainsrp-itemlist .items .item .J_ItemPicA').each(function(){ids.push($(this).attr(""data-nid""))});return ids;");
List<string> itemIds = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(Newtonsoft.Json.JsonConvert.SerializeObject(res.result));
var result= ApiHelper.GetLabelByItemIds(itemIds, Models.ItemPlatform.Taobao);
if (result.isOk)
{
foreach (var item in result.datas)
{
//判断是否团队过滤
if (item.IsFilter)
{
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.myitemState').addClass(""falseBg"")");
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
else
{
//判断是否集团过滤
if (item.HasFilter)
{
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.myitemState').prepend('<div class=""smallfalseBg""></div>')");
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
}
if (item.IsCompeting)
{
//<div style=""width:20%;height:20%"" class=""bgYellow"">竞</div>
if ((int)BrowerControl.Main.DoJavaScript(@$"return $($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateYellow').length").result <= 0)
{
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.myitemState').prepend('<div style=""width:20%;height:20%"" class=""stateYellow"">竞</div>')");
}
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
if (item.IsScreening)
{
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.myitemState').addClass(""trueBg"")");
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
if (item.IsAdded)
{
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.myitemState').addClass(""popBg"")");
DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{item.GoodsId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
}
}
}
/// <summary>
/// 添加jq操作类
/// </summary>
/// <returns></returns>
private bool AddJquery()
{
return DoJavaScript("var script=document.createElement('script'); script.type='text/javascript'; script.src='https://code.jquery.com/jquery-1.12.4.min.js'; document.getElementsByTagName('head')[0].appendChild(script); ").isOk;
}
/// <summary>
/// 添加操作面板
/// </summary>
/// <returns></returns>
private bool AddOptionDiv()
{
///添加js事件
DoJavaScript(@"
window.optClick= function (ele,type){
var itemid= $(ele).parent().parent().find(""a"").attr(""data-nid"");
var infoDiv=$($(ele).parent().parent().parent().parent().find("".J_IconMoreNew""));
var price=infoDiv.find("".g_price"").text();
var sales=infoDiv.find("".deal-cnt"").text();
var title=infoDiv.find("".title"").text();
var img=$(ele).parent().parent().find(""img"").attr(""src"");
hyCoreModel.setItemData({itemid:itemid,price:price,sales:sales,title:title,img:img,type:type});
};
");
DoJavaScript(@"$('div#mainsrp-itemlist .items .item').find('.myitemState').remove();$('div#mainsrp-itemlist .items .item .pic').before('<div class=""myitemState""><div class=""stateGraydiv""></div></div><div class=""myopdiv"">
<div class=""bgGreen"" onclick=""optClick(this,0)""></div>
<div class=""bgPig"" onclick=""optClick(this,1)""></div>
<div class=""bgZs"" onclick=""optClick(this,2)""></div>
<div class=""bgYellow"" style=""display:flex"" onclick=""optClick(this,3)""></div>
</div>')");
///添加css
return DoJavaScript(@"$('head').append('<style>
.myitemState
{
width:100%;
height:100%;
position: absolute;
left: 0px;
top: 0px;
}
.trueBg
{
background: url(""nacollector://home/img/true.png"") no-repeat;
background-size: 100% 100%;
}
.popBg
{
background: url(""nacollector://home/img/pop.png"") no-repeat;
background-size: 100% 100%;
}
.falseBg
{
background: url(""nacollector://home/img/close.png"") no-repeat;
background-size: 100% 100%;
}
.smallfalseBg
{
width:78px;
height:78px;
bottom: 0px;
left: 0px;
top: unset;
background: url(""nacollector://home/img/close.png"") no-repeat;
background-size: 100% 100%;
position: absolute;
}
.stateGraydiv
{
width:100%;
height:100%;
display:none;
background-color: rgba(85, 85, 85, 0.45);
}
.divshow
{
display:block;
}
.myopdiv
{
width:85%;
height:100%;
display:none;
position: absolute;
top: 0px;
right:0px;
}
.myopdiv div {
width: 23%;
height: 20%;
display: inline-block;
cursor: pointer;
}
.pic-box:hover .myopdiv
{
display:unset;
}
.pic-box:hover .myitemState
{
display:none;
}
.bgGreen
{
background: rgba(99, 161, 3, 0.8);
}
.bgPig
{
background: rgba(236, 128, 141, 0.8);
}
.bgZs
{
background: rgba(194, 128, 255, 0.8);
}
.bgYellow
{
background: rgba(245, 154, 35, 0.8);
font-size:20px;
color:red;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
float: right;
font-family: ""global-iconfont"";
font-weight: 999;
}
.stateYellow
{
background: rgba(245, 154, 35, 0.8);
font-size:20px;
color:red;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
float: right;
font-family: ""global-iconfont"";
font-weight: 999;
}
</style>')".Replace("\n", "").Replace("\r\n", "")).isOk;
}
/// <summary>
/// 执行js
/// </summary>
/// <param name="js"></param>
/// <returns></returns>
public (bool isOk, object result) DoJavaScript(string js)
{
js = js.Replace("\n", "").Replace("\r\n", "").Replace(System.Environment.NewLine, "").Replace("\r", "");
Task<JavascriptResponse> task = null;
Application.Current.Dispatcher.Invoke(() =>
{
task = web.EvaluateScriptAsPromiseAsync(js);
});
var result = task.Result;
//判断是否加载完成
return (result.Success,result.Result);
}
}
public class CefAsyncJS
{
public string getTest()
{
return "我草";
}
public bool setItemData(dynamic data)
{
int type = data.type;
string price = data.price;
price = price.Replace("¥", "").Trim();
string sales = data.sales;
string title = data.title;
title = title.Trim();
string img = data.img;
string itemId = data.itemid;
var result = ApiHelper.SetItemLabel(new Models.ItemLabelDto()
{
Img = img,
ItemId = itemId,
Platform = Models.ItemPlatform.Taobao,
Price = Convert.ToDecimal(price),
Sales = sales,
Status = type,
Title = title
});
if (result.isOk)
{
//判断是否团队过滤
if (type == 1)
{
BrowerControl.Main.DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.myitemState').addClass(""falseBg"")");
}
if (type == 3)
{
int row = (int)BrowerControl.Main.DoJavaScript(@$"return $($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.myitemState').find('.stateYellow').length").result;
if (row < 1)
{
BrowerControl.Main.DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.myitemState').prepend('<div style=""width:20%;height:20%"" class=""stateYellow"">竞</div>')");
}
}
if (type == 0)
{
BrowerControl.Main.DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.myitemState').addClass(""trueBg"")");
}
if (type == 2)
{
BrowerControl.Main.DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.myitemState').addClass(""popBg"")");
}
BrowerControl.Main.DoJavaScript($@"$($('div#mainsrp-itemlist .items .item').find('a[data-nid=""{itemId}""]').parent().parent()).find('.stateGraydiv').addClass(""divshow"")");
}
WpfNoticeMsg.NoticeMessage.Show(result.msg, "提示");
return result.isOk;
}
}
}