464 lines
28 KiB
C#
464 lines
28 KiB
C#
using AllBase.Helper;
|
|
using CefSharp;
|
|
using CefSharp.WinForms;
|
|
using NaiBa.CefHelper;
|
|
using NaiBa.CefHelper.Handler;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace LittleRabbit.LittleRabbitForms.UploadForms
|
|
{
|
|
/// <summary>
|
|
/// 主要用来设置上货佣金逻辑
|
|
/// </summary>
|
|
public partial class UploadProductForm
|
|
{
|
|
ChromiumWebBrowser webBrowser = null;
|
|
UserControls.ButtonEx btn = null;
|
|
public void InitCommissionCef()
|
|
{
|
|
|
|
Task.Run(() =>
|
|
{
|
|
while (true)
|
|
{
|
|
Thread.Sleep(500);
|
|
if (!uploadParam.isSetCommission)
|
|
continue;
|
|
|
|
if (webBrowser == null)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
webBrowser = CEFBrowser.CreateChromiumWebBrowser("about:blank", false);
|
|
webBrowser.Dock = DockStyle.Fill;
|
|
webBrowser.KeyboardHandler = new KeyboardHandler();
|
|
webBrowser.AddressChanged += WebBrowser_AddressChanged;
|
|
webBrowser.FrameLoadEnd += webBrowser_FrameLoadEnd;
|
|
webBrowser.SendToBack();
|
|
this.Controls.Add(webBrowser);
|
|
|
|
btn = new UserControls.ButtonEx();
|
|
btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
|
btn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(63)))), ((int)(((byte)(126)))), ((int)(((byte)(255)))));
|
|
btn.Cursor = System.Windows.Forms.Cursors.Hand;
|
|
btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
|
btn.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
|
|
btn.ForeColor = System.Drawing.Color.White;
|
|
btn.Location = new System.Drawing.Point(this.Width - 82, 10);
|
|
btn.Margin = new System.Windows.Forms.Padding(2);
|
|
btn.Name = "btn";
|
|
btn.Size = new System.Drawing.Size(72, 25);
|
|
btn.TabIndex = 390;
|
|
btn.Text = "隐藏";
|
|
btn.UseVisualStyleBackColor = false;
|
|
btn.Click += Btn_Click;
|
|
Controls.Add(btn);
|
|
}));
|
|
}
|
|
|
|
if (!webBrowser.IsBrowserInitialized || webBrowser.IsLoading)
|
|
continue;
|
|
|
|
var jsCode = "function closeMask() {" +
|
|
"if (document.body.innerHTML.indexOf('普通计划') < 0) return;" +
|
|
"var mask1 = document.getElementsByClassName('ant-modal-root')[0];" +
|
|
"if (mask1) mask1.parentNode.removeChild(mask1);" +
|
|
"var mask2 = document.getElementsByClassName('y-guide')[0];" +
|
|
"if (mask2) mask2.parentNode.removeChild(mask2);" +
|
|
"var mask3 = document.getElementById('browser-support-tip');" +
|
|
"if (mask3) mask3.parentNode.removeChild(mask3);" +
|
|
"}" +
|
|
"closeMask();";
|
|
//var t = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsCode);
|
|
//t.Wait(3000);
|
|
|
|
Thread.Sleep(500);
|
|
while (lstCommissionAddResult.Count > 0)
|
|
{
|
|
Thread.Sleep(10);
|
|
var commission = lstCommissionAddResult[0];
|
|
var commissions = lstCommissionAddResult.Where(c => c.ShopName == commission.ShopName && c.Rate == commission.Rate).ToList();
|
|
var shop = AllBase.Helper.GlobalConfig.DouShopLoginList.FirstOrDefault(s => s.ShopAccount == commission.ShopName);
|
|
if (shop == null)
|
|
{
|
|
lstCommissionAddResult.RemoveAll(s => s.ShopName == commission.ShopName);
|
|
NaiBa.Util.Helper.ConfigHelper.SaveConfig("upload_commission_list", lstCommissionAddResult);
|
|
}
|
|
else
|
|
{
|
|
var productIds = "";
|
|
var rate = commission.Rate;
|
|
for (int i = 0; i < 10 && i < commissions.Count; i++)
|
|
{
|
|
productIds += commissions[i].ProductId + ",";
|
|
}
|
|
var cookie = shop.Cookies;
|
|
CEFBrowser.DeleteCookie(webBrowser.RequestContext.GetCookieManager(null));
|
|
var t = CEFBrowser.SetCookieAsync("jinritemai.com", cookie, webBrowser.RequestContext.GetCookieManager(null));
|
|
t.Wait(2000);
|
|
Thread.Sleep(500);
|
|
webBrowser.Load("https://buyin.jinritemai.com/dashboard/activity/common-plan");
|
|
while (webBrowser.IsLoading)
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
Thread.Sleep(2000);
|
|
var jsStr1 = clickJsElementStr +
|
|
"function clickBtn(txt) {\r\n" +
|
|
" if (document.body.innerHTML.indexOf('加载失败') >= 0)\r\n" +
|
|
" return '加载失败';\r\n" +
|
|
" var elem = getElement(txt);\r\n" +
|
|
" if (elem) elem.click(); return 'success';\r\n" +
|
|
" return 'no_element';\r\n" +
|
|
"}\r\n" +
|
|
"clickBtn('添加商品');\r\n";
|
|
var t1 = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
t1.Wait();
|
|
if (t1.Result?.Result?.ToString() == "加载失败")
|
|
break;
|
|
if (t1.Result?.Result?.ToString() == "success")
|
|
{
|
|
Thread.Sleep(2000);
|
|
|
|
string failMsg = "";
|
|
|
|
#region 选择批量查询选项
|
|
var host = webBrowser.GetBrowser().GetHost();
|
|
int x = 0, y = 0;
|
|
jsStr1 = "function getPos() { var box = document.getElementsByClassName('search-product-select');" +
|
|
"if (box[0] == null) return '-|-';" +
|
|
"var actualLeft = box[0].offsetLeft;" +
|
|
"var actualTop = box[0].offsetTop;" +
|
|
"var current = box[0].offsetParent;" +
|
|
"while (current !== null) { actualLeft += current.offsetLeft; actualTop += current.offsetTop; current = current.offsetParent; }" +
|
|
"return '' + actualLeft + '|' + actualTop;}" +
|
|
"getPos();";
|
|
var task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
var result = task.Result?.Result?.ToString();
|
|
|
|
if (result != null && result != "-|-")
|
|
{
|
|
var arr = result.Split('|');
|
|
if (arr.Length == 2)
|
|
{
|
|
x = Convert.ToInt32(arr[0]);
|
|
y = Convert.ToInt32(arr[1]);
|
|
}
|
|
int x1 = x + new Random(DateTime.Now.Millisecond).Next(6, 30);
|
|
int y1 = y + new Random(DateTime.Now.Millisecond).Next(6, 16);
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, false, 1, CefEventFlags.None); // 按下鼠标左键
|
|
Thread.Sleep(70 + new Random(DateTime.Now.Millisecond).Next(50));
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, true, 1, CefEventFlags.None); // 松开鼠标左键
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
|
|
x1 = x + new Random(DateTime.Now.Millisecond).Next(6, 30);
|
|
y1 = y + new Random(DateTime.Now.Millisecond).Next(84, 94);
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, false, 1, CefEventFlags.None); // 按下鼠标左键
|
|
Thread.Sleep(70 + new Random(DateTime.Now.Millisecond).Next(50));
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, true, 1, CefEventFlags.None); // 松开鼠标左键
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
}
|
|
else
|
|
{
|
|
failMsg = "佣金商品列表打开失败。";
|
|
goto showFail;
|
|
}
|
|
#endregion
|
|
|
|
#region 输入商品编号并查询
|
|
jsStr1 = "function getPos() { var box = document.getElementsByClassName('ant-select-show-search');" +
|
|
"if (box[0] == null) return '-|-';" +
|
|
"var actualLeft = box[0].offsetLeft;" +
|
|
"var actualTop = box[0].offsetTop;" +
|
|
"var current = box[0].offsetParent;" +
|
|
"while (current !== null) { actualLeft += current.offsetLeft; actualTop += current.offsetTop; current = current.offsetParent; }" +
|
|
"return '' + actualLeft + '|' + actualTop;}" +
|
|
"getPos();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
result = task.Result?.Result?.ToString();
|
|
|
|
if (result != null && result != "-|-")
|
|
{
|
|
var arr = result.Split('|');
|
|
if (arr.Length == 2)
|
|
{
|
|
x = Convert.ToInt32(arr[0]);
|
|
y = Convert.ToInt32(arr[1]);
|
|
}
|
|
int x1 = x + new Random(DateTime.Now.Millisecond).Next(6, 30);
|
|
int y1 = y + new Random(DateTime.Now.Millisecond).Next(6, 16);
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, false, 1, CefEventFlags.None); // 按下鼠标左键
|
|
Thread.Sleep(70 + new Random(DateTime.Now.Millisecond).Next(50));
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, true, 1, CefEventFlags.None); // 松开鼠标左键
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
|
|
Invoke(new Action(() =>
|
|
{
|
|
Clipboard.Clear();
|
|
Clipboard.SetDataObject("" + productIds.TrimEnd(',')); // 复制要搜索的商品ID到剪贴板
|
|
}));
|
|
|
|
// 黏贴 ctrl + v
|
|
host.SendKeyEvent(new CefSharp.KeyEvent() { WindowsKeyCode = 86, Type = KeyEventType.RawKeyDown, Modifiers = CefEventFlags.ControlDown });
|
|
Thread.Sleep(250 + new Random(DateTime.Now.Millisecond).Next(250));
|
|
}
|
|
else
|
|
{
|
|
failMsg = "查询设置佣金商品失败。";
|
|
goto showFail;
|
|
}
|
|
|
|
jsStr1 = "document.getElementsByClassName('search-btns-item-first')[0].click();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
#endregion
|
|
|
|
#region 勾选商品信息
|
|
jsStr1 = getFirstElementJsStr +
|
|
"\r\nfunction getInnerHtml() {\r\n" +
|
|
" var item = document.getElementsByClassName('ant-table-wrapper activity-list-table')[0];\r\n" +
|
|
" if (item) {\r\n" +
|
|
" return item.innerHTML;\r\n" +
|
|
" }\r\n" +
|
|
" return 'no_element';\r\n" +
|
|
"}\r\n" +
|
|
"getInnerHtml();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
var idArray = productIds.TrimEnd(',').Split(',');
|
|
if (task.Result?.Result?.ToString() == null || !idArray.Any(a => task.Result.Result.ToString().Contains(a)))
|
|
{
|
|
failMsg = "设置佣金商品不存在。";
|
|
goto showFail;
|
|
}
|
|
|
|
jsStr1 = getFirstElementJsStr +
|
|
"\r\nfunction clickCheckBox() {\r\n" +
|
|
" var item = document.getElementsByClassName('ant-table-wrapper activity-list-table')[0];\r\n" +
|
|
" if (item) {\r\n" +
|
|
" var tHead = getFirstElement('THEAD', item)\r\n" +
|
|
" var tagItem = getFirstElement('INPUT', tHead)\r\n" +
|
|
" if (tagItem) tagItem.click(); return 'success';\r\n" +
|
|
" }\r\n" +
|
|
" return 'no_element';\r\n" +
|
|
"}\r\n" +
|
|
"clickCheckBox();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
if (task.Result?.Result?.ToString() != "success")
|
|
{
|
|
failMsg = "设置佣金选择商品不存在。";
|
|
goto showFail;
|
|
}
|
|
#endregion
|
|
|
|
#region 点击确定设置佣金
|
|
jsStr1 = "document.getElementsByClassName('exclusive_commodity-filter-drawer-footer-operations__operation ant-btn-primary')[0].click();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
#endregion
|
|
|
|
#region 设置佣金金额
|
|
jsStr1 = "function getPos() { var box = document.getElementsByClassName('ant-input-number-input');" +
|
|
"if (box[0] == null) return '-|-';" +
|
|
"var actualLeft = box[0].offsetLeft;" +
|
|
"var actualTop = box[0].offsetTop;" +
|
|
"var current = box[0].offsetParent;" +
|
|
"while (current !== null) { actualLeft += current.offsetLeft; actualTop += current.offsetTop; current = current.offsetParent; }" +
|
|
"return '' + actualLeft + '|' + actualTop;}" +
|
|
"getPos();";
|
|
task = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
task.Wait();
|
|
result = task.Result?.Result?.ToString();
|
|
|
|
if (result != null && result != "-|-")
|
|
{
|
|
var arr = result.Split('|');
|
|
if (arr.Length == 2)
|
|
{
|
|
x = Convert.ToInt32(arr[0]);
|
|
y = Convert.ToInt32(arr[1]);
|
|
}
|
|
int x1 = x + new Random(DateTime.Now.Millisecond).Next(30, 60);
|
|
int y1 = y + new Random(DateTime.Now.Millisecond).Next(6, 16);
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, false, 1, CefEventFlags.None); // 按下鼠标左键
|
|
Thread.Sleep(70 + new Random(DateTime.Now.Millisecond).Next(50));
|
|
host.SendMouseClickEvent(x1, y1, MouseButtonType.Left, true, 1, CefEventFlags.None); // 松开鼠标左键
|
|
Thread.Sleep(150 + new Random(DateTime.Now.Millisecond).Next(150));
|
|
|
|
// 模拟输入退格键
|
|
host.SendKeyEvent(new CefSharp.KeyEvent() { WindowsKeyCode = 8, Type = KeyEventType.RawKeyDown, Modifiers = CefEventFlags.None });
|
|
Thread.Sleep(250 + new Random(DateTime.Now.Millisecond).Next(250));
|
|
// 模拟输入退格键
|
|
host.SendKeyEvent(new CefSharp.KeyEvent() { WindowsKeyCode = 8, Type = KeyEventType.RawKeyDown, Modifiers = CefEventFlags.None });
|
|
Thread.Sleep(250 + new Random(DateTime.Now.Millisecond).Next(250));
|
|
|
|
Invoke(new Action(() =>
|
|
{
|
|
Clipboard.Clear();
|
|
Clipboard.SetDataObject("" + rate); // 复制要搜索的商品ID到剪贴板
|
|
}));
|
|
|
|
// 黏贴 ctrl + v
|
|
host.SendKeyEvent(new CefSharp.KeyEvent() { WindowsKeyCode = 86, Type = KeyEventType.RawKeyDown, Modifiers = CefEventFlags.ControlDown });
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
}
|
|
else
|
|
{
|
|
failMsg = "佣金金额设置失败。";
|
|
goto showFail;
|
|
}
|
|
#endregion
|
|
|
|
#region 最后点击确定按钮设置佣金
|
|
//apply-sample-modal-footer
|
|
jsStr1 = clickJsElementStr +
|
|
"function clickOkBtn(txt) {\r\n" +
|
|
" var elem = getElement(txt, document.getElementsByClassName('apply-sample-modal-footer')[0]);\r\n" +
|
|
" if (elem) elem.click(); return 'success';\r\n" +
|
|
" return 'no_element';\r\n" +
|
|
"}\r\n" +
|
|
"clickBtn('<span>确定</span>');\r\n";
|
|
t1 = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
|
|
t1.Wait();
|
|
Thread.Sleep(500 + new Random(DateTime.Now.Millisecond).Next(500));
|
|
#endregion
|
|
|
|
Thread.Sleep(2000);
|
|
|
|
//bool isRetry = false;
|
|
//Retry:
|
|
//var url = "https://buyin.jinritemai.com/api/promotion/createXdPromotions";
|
|
////if (!string.IsNullOrEmpty(sign))
|
|
//// url += "?_signature=" + sign;
|
|
//var request = new CommonSimRequest();
|
|
//var referer = "https://buyin.jinritemai.com/dashboard/activity/common-plan";
|
|
//var data = "cos_ratio=" + rate + "&apply_style=1&product_ids=" + productIds.Trim(',');
|
|
//Dictionary<string, string> headers = new Dictionary<string, string>();
|
|
//headers.Add("Origin", @"https://buyin.jinritemai.com");
|
|
//var html = request.Request_Post(url, referer, headers, shop.Cookies, data);
|
|
//if (html.Contains("\"msg\":\"success\"") && html.Contains("\"failed_ids\":{}"))
|
|
//{
|
|
//}
|
|
//else
|
|
//{
|
|
// if (!isRetry)
|
|
// {
|
|
// isRetry = true;
|
|
// goto Retry;
|
|
// }
|
|
showFail:
|
|
if (!string.IsNullOrEmpty(failMsg))
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
pnlShowError.Visible = true;
|
|
pnlShowError.Text = " 设置佣金部分失败:" + failMsg + " " + productIds.Trim(',') + " ";
|
|
this.pnlShowError.BringToFront();
|
|
Task.Run(() =>
|
|
{
|
|
Thread.Sleep(8000);
|
|
if (this.Created)
|
|
Invoke(new Action(() =>
|
|
{
|
|
this.pnlShowError.Visible = false;
|
|
}));
|
|
});
|
|
}));
|
|
}
|
|
//}
|
|
|
|
var html = "";
|
|
lock (lstCommissionAddResult)
|
|
{
|
|
try
|
|
{
|
|
//if (html.Contains("\"msg\":\"success\"") && html.Contains("\"failed_ids\":{}"))
|
|
{
|
|
lstCommissionAddResult.RemoveAll(s => productIds.Contains(s.ProductId.ToString()) || s.ProductId == 0);
|
|
NaiBa.Util.Helper.ConfigHelper.SaveConfig("upload_commission_list", lstCommissionAddResult);
|
|
}
|
|
}
|
|
catch (Exception ex) { }
|
|
}
|
|
for (int k = 0; k < 600; k++)
|
|
{
|
|
Thread.Sleep(1000);
|
|
if (lstCommissionAddResult.Count > 10 || Work.BLL.Upload.UploadProductBLL.ProductStock.Count < 1)
|
|
break;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void Btn_Click(object sender, EventArgs e)
|
|
{
|
|
this.webBrowser.SendToBack();
|
|
btn.SendToBack();
|
|
}
|
|
|
|
private void WebBrowser_AddressChanged(object sender, CefSharp.AddressChangedEventArgs e)
|
|
{
|
|
}
|
|
|
|
private void webBrowser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
|
|
{
|
|
}
|
|
|
|
public string clickJsElementStr = "function getElement(txt, elem) {\r\n" +
|
|
" var child = document.body.firstElementChild;\r\n" +
|
|
" if (elem) child = elem\r\n" +
|
|
" var element1 = null;\r\n" +
|
|
" while (child) {\r\n" +
|
|
" if (child.innerHTML.indexOf(txt) >= 0) {\r\n" +
|
|
" element1 = child;\r\n" +
|
|
" if (child.firstElementChild) {\r\n" +
|
|
" child = child.firstElementChild; continue;\r\n" +
|
|
" } else break;\r\n" +
|
|
" }\r\n" +
|
|
" child = child.nextElementSibling;\r\n" +
|
|
" }\r\n" +
|
|
" if (element1) {\r\n" +
|
|
" return element1;\r\n" +
|
|
" } else return error;\r\n" +
|
|
"}\r\n";
|
|
|
|
public string getFirstElementJsStr = "function getFirstElement(tag_name, elem) {\r\n" +
|
|
" var nodes = [];\r\n" +
|
|
" var child = document.body.firstElementChild;\r\n" +
|
|
" if (elem) child = elem\r\n" +
|
|
" nodes.push(child);\r\n" +
|
|
" for (i = 0; i < nodes.length; i++) {\r\n" +
|
|
" if (nodes[i].tagName === tag_name) return nodes[i];\r\n" +
|
|
" for (j = 0; j < nodes[i].childNodes.length; j++) {\r\n" +
|
|
" var node = nodes[i].childNodes[j];\r\n" +
|
|
" if ((node.nodeType != 3) && (node.nodeName != 'SCRIPT')) {\r\n" +
|
|
" nodes.push(node);\r\n" +
|
|
" }\r\n" +
|
|
" }\r\n" +
|
|
" }\r\n" +
|
|
" return error;\r\n" +
|
|
"}\r\n";
|
|
}
|
|
}
|