1688商品采集
This commit is contained in:
1 parent
53736cff6e
commit
c489c30bf1
35 files changed
+4095
-254
No files matched your search
@@ -0,0 +1,300 @@
|
||||
using AllBase;
|
||||
using AllBase.Helper;
|
||||
using AllBase.Model.Result;
|
||||
using CefSharp;
|
||||
using CefSharp.WinForms;
|
||||
//using TemuKJD.DashBoardForms;
|
||||
using TemuKJD.Helper;
|
||||
using TemuKJD.BaseForms;
|
||||
using Microsoft.Win32;
|
||||
using NaiBa.CefHelper;
|
||||
using NaiBa.CefHelper.Handler;
|
||||
using NaiBa.Util.Extension;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
//using System.Web.Hosting;
|
||||
using System.Windows.Forms;
|
||||
using Work;
|
||||
using Work.BLL.Tools;
|
||||
using TemuKJD.BLL;
|
||||
using AllBase.Model.Result.PDDResult;
|
||||
using NaiBa.Util;
|
||||
|
||||
namespace TemuKJD.ToolForms
|
||||
{
|
||||
public partial class WebForm : BaseGeneralForm
|
||||
{
|
||||
public int CloseAfterSecond { get; set; }
|
||||
public ChromiumWebBrowser cefWebBrowser = null;
|
||||
public ShopLoginInfo cookieShop = null;
|
||||
public string InitUrl = "about:blank";
|
||||
private string currentUrl = "";
|
||||
public static WebForm instance = null;
|
||||
|
||||
public WebForm(ShopLoginInfo shopInfo, string url)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
instance = this;
|
||||
this.cookieShop = shopInfo;
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
InitUrl = url;
|
||||
}
|
||||
|
||||
private void ShopLoginForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
this.Size = new Size(1680, 900);
|
||||
this.CenterToScreen();
|
||||
|
||||
this.splitContainer3.SplitterDistance = this.splitContainer3.Height - 62;
|
||||
this.splitContainer1.SplitterDistance = this.splitContainer1.Width - 5;
|
||||
|
||||
if (CloseAfterSecond > 0)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
Thread.Sleep(1000 * CloseAfterSecond);
|
||||
if (this.Created)
|
||||
{
|
||||
Invoke(new Action(() => { this.Close(); }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initCefAndMainShop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化CEF并登录主店铺
|
||||
/// </summary>
|
||||
public void initCefAndMainShop()
|
||||
{
|
||||
if (cookieShop == null || cookieShop.Cookies == null)
|
||||
return;
|
||||
|
||||
if (cefWebBrowser == null)
|
||||
{
|
||||
cefWebBrowser = CEFBrowser.CreateChromiumWebBrowser(InitUrl, false);
|
||||
CustomResourceRequestHandlerLocal resourceRequestHandler = new CustomResourceRequestHandlerLocal();
|
||||
resourceRequestHandler.ResourceLoadCompleteEvent += RequestHandler_ResourceLoadComplete;
|
||||
resourceRequestHandler.BeforeResourceLoadEvent += RequestHandler_BeforeResourceLoad;
|
||||
cefWebBrowser.RequestHandler = new CustomRequestHandler(resourceRequestHandler);
|
||||
var lifeSapnHandler = new LifeSpanHandlerLocal(PopWindowType.SelfWindow);
|
||||
cefWebBrowser.LifeSpanHandler = lifeSapnHandler;
|
||||
cefWebBrowser.KeyboardHandler = new KeyboardHandler();
|
||||
cefWebBrowser.FrameLoadEnd += CefWebBrowser_FrameLoadEnd;
|
||||
cefWebBrowser.AddressChanged += CefWebBrowser_AddressChanged;
|
||||
cefWebBrowser.LoadError += CefWebBrowser_LoadError;
|
||||
cefWebBrowser.Dock = DockStyle.Fill;
|
||||
|
||||
//js绑定C#对象
|
||||
WebFormObj webObj = new WebFormObj();
|
||||
webObj.OnInfoReceived += ShopObj_OnInfoReceived;
|
||||
CefSharpSettings.WcfEnabled = true;
|
||||
cefWebBrowser.JavascriptObjectRepository.Register(
|
||||
name: "webObj",
|
||||
objectToBind: webObj,
|
||||
isAsync: false,
|
||||
options: new BindingOptions { Binder = BindingOptions.DefaultBinder.Binder /*CamelCaseJavascriptNames = false*/ /*cef79*/ }
|
||||
);
|
||||
|
||||
List<Cookie> cookieLst = new List<Cookie>();
|
||||
try
|
||||
{
|
||||
if (cookieShop != null)
|
||||
{
|
||||
cookieLst = JsonHelper.DeserializeJsonToList<CefSharp.Cookie>(cookieShop.Cookies.ParseToString());
|
||||
if (cookieLst == null)
|
||||
cookieLst = new List<Cookie>();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
splitContainer1.Panel1.Controls.Add(cefWebBrowser);
|
||||
}));
|
||||
// cef138必须init后方可设置或删除cookie
|
||||
while (!cefWebBrowser.IsBrowserInitialized || !cefWebBrowser.GetBrowser().MainFrame.IsValid)
|
||||
Thread.Sleep(20);
|
||||
CEFBrowser.DeleteCookie(cefWebBrowser.GetCookieManager(null));
|
||||
|
||||
//cef 138 如果没有设置RequestContext 可能为空,直接getCookieManager
|
||||
//异步设置cookie
|
||||
//var t = CEFBrowser.SetCookieAsync(JsonHelper.SerializeObject(cookieLst), cefWebBrowser.GetCookieManager(null));
|
||||
string address = InitUrl;
|
||||
foreach (var cook in cookieLst)
|
||||
{
|
||||
// 同步设置cookie
|
||||
cefWebBrowser.GetCookieManager(null).SetCookie(address, cook);
|
||||
//var t = CEFBrowser.SetCookieAsync(cook.Domain, cook.Name + "=" + cook.Value + ";", cefWebBrowser.GetCookieManager(null));
|
||||
//t.Wait(5000);
|
||||
}
|
||||
cefWebBrowser.Load(InitUrl);
|
||||
});
|
||||
}
|
||||
else
|
||||
cefWebBrowser.Load(InitUrl);
|
||||
}
|
||||
|
||||
public void ClickWangwang()
|
||||
{
|
||||
|
||||
var host = cefWebBrowser.GetBrowser().GetHost();
|
||||
|
||||
int x1 = 520;
|
||||
int y1 = 520;
|
||||
var rx = -15 + Randomx.GetId(31);
|
||||
var ry = -15 + Randomx.GetId(31);
|
||||
// 随机按下点的位置,提高仿真性
|
||||
int x = x1 + rx;
|
||||
int y = y1 + ry;
|
||||
|
||||
// 按下鼠标左键
|
||||
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
|
||||
Thread.Sleep(100 + Randomx.GetId(200));
|
||||
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
|
||||
|
||||
string jsStr = @"var aLst = document.getElementsByTagName('a');
|
||||
for (i=0; i<aLst.length; i++){
|
||||
if (aLst[i].innerHTML.indexOf('6000000004654-2-tps-28-28') >= 0)
|
||||
aLst[i].click();
|
||||
if (aLst[i].innerHTML.indexOf('6000000001299-2-tps-134-28.png') >= 0)
|
||||
aLst[i].click();
|
||||
}";
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
jsStr = @"function clickFunc() {
|
||||
var aLst = document.getElementsByTagName('span');
|
||||
for (i=0; i<aLst.length; i++) {
|
||||
if (aLst[i].innerHTML.indexOf('咨询') >= 0) {
|
||||
aLst[i].parentElement.click();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
clickFunc();";
|
||||
//
|
||||
var brow = cefWebBrowser.GetBrowser();
|
||||
var response = cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr);
|
||||
response.Wait(5000);
|
||||
if (response.Result.Result.ParseToBool())
|
||||
break;
|
||||
Thread.Sleep(Randomx.GetId(250, 750));
|
||||
}
|
||||
}
|
||||
|
||||
private void ShopObj_OnInfoReceived(string information)
|
||||
{
|
||||
//BaseMsgForm.ShowInfo(information);
|
||||
if (!string.IsNullOrEmpty(information))
|
||||
cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync("alert('" + information + "');");
|
||||
}
|
||||
|
||||
private void CefWebBrowser_LoadError(object sender, CefSharp.LoadErrorEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
private void CefWebBrowser_AddressChanged(object sender, CefSharp.AddressChangedEventArgs e)
|
||||
{
|
||||
if (this.Created && !this.Disposing && !this.IsDisposed)
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
currentUrl = e.Address;
|
||||
txtAddress.Text = e.Address;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
private void CefWebBrowser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
|
||||
{
|
||||
//js绑定C#对象 html页面内声明绑定的C#对象
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("(function(){");
|
||||
sb.Append("CefSharp.BindObjectAsync('webObj');");
|
||||
sb.Append("})();");
|
||||
cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(sb.ToString());
|
||||
}
|
||||
|
||||
public string offerLoginId = "";
|
||||
//public Func<object, string, string, request, bool> BeforeResourceLoadEvent; // sender, requestUrl, postData, request, true(表示取消执行)
|
||||
private bool RequestHandler_BeforeResourceLoad(object sender, string url, string postData, IRequest request)
|
||||
{
|
||||
if (url.StartsWith("https://detail.1688.com/hh.gif"))
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
offerLoginId = BaseRegex.GetStringJson(ref postData, "offerLoginId");
|
||||
Thread.Sleep(500);
|
||||
//this.Load
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void RequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData, IRequest request)
|
||||
{
|
||||
}
|
||||
|
||||
private void ShopLoginForm_FormClosed(object sender, FormClosedEventArgs e)
|
||||
{
|
||||
//OnSaveComplete(shop);
|
||||
Task.Run(() => {
|
||||
Thread.Sleep(500);
|
||||
if (!this.Disposing && !this.IsDisposed)
|
||||
{
|
||||
MainForm.instance.Invoke(new Action(() => { this.Dispose(); }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void txtAddress_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter)
|
||||
{
|
||||
if (cefWebBrowser != null)
|
||||
{
|
||||
cefWebBrowser.Load(txtAddress.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnPrev_Click(object sender, EventArgs e)
|
||||
{
|
||||
cefWebBrowser.Back();
|
||||
}
|
||||
|
||||
private void btnNext_Click(object sender, EventArgs e)
|
||||
{
|
||||
cefWebBrowser.Forward();
|
||||
}
|
||||
|
||||
private void btnGo_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (cefWebBrowser != null)
|
||||
{
|
||||
cefWebBrowser.Load(txtAddress.Text);
|
||||
}
|
||||
}
|
||||
|
||||
private void WebForm_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
e.Cancel = true;
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user