Browse Source

优化获取内存共享key的流程

master
С·æ 4 years ago
parent
commit
31a13baea3
  1. 66
      JdShopListener/JdShopListener/MainWindowViewModel.cs
  2. 25
      JdShopListener/JdShopListener/MemoryHelper.cs
  3. 2
      JdShopListener/JdShopListener/Properties/PublishProfiles/FolderProfile.pubxml.user

66
JdShopListener/JdShopListener/MainWindowViewModel.cs

@ -128,7 +128,6 @@ namespace JdShopListener
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted; webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadFileTaskAsync("https://qymds.oss-cn-hangzhou.aliyuncs.com/chromium/.local-chromium.zip", System.Environment.CurrentDirectory+"\\tempZip.zip").Wait(); webClient.DownloadFileTaskAsync("https://qymds.oss-cn-hangzhou.aliyuncs.com/chromium/.local-chromium.zip", System.Environment.CurrentDirectory+"\\tempZip.zip").Wait();
} }
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
@ -163,7 +162,10 @@ namespace JdShopListener
IsInitLoding = true; IsInitLoding = true;
var result = ApiHelper.GetLabelByItemIds(); var result = ApiHelper.GetLabelByItemIds();
result.datas.ForEach(item => { AddLog($"检测到{result.datas.Count}个慧眼竞品添加!");
result.datas.ForEach(item =>
{
var last = skuList.FirstOrDefault(c => c.SkuId == item.GoodsId); var last = skuList.FirstOrDefault(c => c.SkuId == item.GoodsId);
if (last != null) if (last != null)
@ -175,17 +177,24 @@ namespace JdShopListener
} }
} }
//新增 //新增
else { else
Application.Current.Dispatcher.Invoke(() => { {
Application.Current.Dispatcher.Invoke(() =>
{
InitText = $"正在添加【{item.GoodsId}】..."; InitText = $"正在添加【{item.GoodsId}】...";
}); });
AddSku(item.GoodsId); AddSku(item.GoodsId, "慧眼竞品添加");
} }
// //
}); });
} }
finally { catch(Exception ex)
{
AddLog($"慧眼添加异常{ex.Message}\r\n{ex.StackTrace}!");
}
finally
{
IsInitLoding = false; IsInitLoding = false;
} }
}); });
@ -417,7 +426,7 @@ namespace JdShopListener
} }
private void AddSku(string newSku) private void AddSku(string newSku,string desc=null)
{ {
if (string.IsNullOrEmpty(JDCookie)) if (string.IsNullOrEmpty(JDCookie))
{ {
@ -446,7 +455,7 @@ namespace JdShopListener
{ {
SkuModel model = new SkuModel() SkuModel model = new SkuModel()
{ {
Desc = Desc, Desc = desc??Desc,
SkuId = sku["skuId"].ToString(), SkuId = sku["skuId"].ToString(),
SpuId = spuId SpuId = spuId
}; };
@ -1216,30 +1225,37 @@ namespace JdShopListener
// 下载浏览器执行程序 // 下载浏览器执行程序
// var fetcher= new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result; // 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,
string url = "https://passport.jd.com/uc/login"; //Args = new string[] { "--no-sandbox" }
// 创建一个浏览器执行实例 }).Result;
using var browser = Puppeteer.LaunchAsync(new LaunchOptions try
{ {
Headless = true, // 打开一个页面
var page = browser.NewPageAsync().Result;
//Args = new string[] { "--no-sandbox" }
}).Result;
// 打开一个页面
var page = browser.NewPageAsync().Result;
var a = page.GoToAsync(url, WaitUntilNavigation.Networkidle0).Result; var a = page.GoToAsync(url, WaitUntilNavigation.Networkidle0).Result;
var content = page.GetContentAsync().Result; var content = page.GetContentAsync().Result;
page.DeleteCookieAsync().Wait(); page.DeleteCookieAsync().Wait();
var cookies = page.GetCookiesAsync().Result; var cookies = page.GetCookiesAsync().Result;
JDCookie = string.Empty; JDCookie = string.Empty;
cookies.ToList().ForEach(c => cookies.ToList().ForEach(c =>
{
JDCookie += $"{c.Name}={c.Value};";
});
}
catch (Exception ex)
{ {
JDCookie += $"{c.Name}={c.Value};"; WpfNoticeMsg.NoticeMessage.Show("初始化内核失败,请尝试关闭跟屁虫重新启动!", "错误");
}); }
AddLog("获取到Cookie" + JDCookie); AddLog("获取到Cookie" + JDCookie);
} }

25
JdShopListener/JdShopListener/MemoryHelper.cs

@ -2,11 +2,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO.MemoryMappedFiles; using System.IO.MemoryMappedFiles;
using System.Text; using System.Text;
using System.Threading;
namespace Utils namespace Utils
{ {
public class MemoryHelper public class MemoryHelper
{ {
static int getTokentCount = 0;
public static string GetMemoryToken() public static string GetMemoryToken()
{ {
string memoryName = string.Empty; string memoryName = string.Empty;
@ -23,15 +27,30 @@ namespace Utils
if (result.isOk) if (result.isOk)
{ {
if (string.IsNullOrEmpty(result.content))
{
getTokentCount = getTokentCount + 1;
if (getTokentCount <= 3)
{
Thread.Sleep(300);
return GetMemoryToken();
}
}
return result.content; return result.content;
} }
else else
{ {
getTokentCount = getTokentCount + 1;
if (getTokentCount <= 3)
{
Thread.Sleep(300);
return GetMemoryToken();
}
System.Environment.Exit(0); System.Environment.Exit(0);
return string.Empty; return string.Empty;
} }
} }
/// <summary> /// <summary>
/// 写入映射文件 /// 写入映射文件
/// </summary> /// </summary>
@ -66,12 +85,12 @@ namespace Utils
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname); MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname);
using (var mmfStream = mmf.CreateViewStream(0, 1000, MemoryMappedFileAccess.ReadWrite)) using (var mmfStream = mmf.CreateViewStream(0, 1000, MemoryMappedFileAccess.ReadWrite))
{ {
byte[] buffer = new byte[128]; byte[] buffer = new byte[1000];
int nLength = 0; int nLength = 0;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
do do
{ {
nLength = mmfStream.Read(buffer, 0, 128); nLength = mmfStream.Read(buffer, 0, 1000);
sb.AppendLine(System.Text.ASCIIEncoding.Default.GetString(buffer)); sb.AppendLine(System.Text.ASCIIEncoding.Default.GetString(buffer));
} while (nLength > 0); } while (nLength > 0);

2
JdShopListener/JdShopListener/Properties/PublishProfiles/FolderProfile.pubxml.user

@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
--> -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<History>True|2021-11-12T07:27:50.0418057Z;True|2021-11-12T15:06:23.1220477+08:00;True|2021-11-11T18:06:38.5161322+08:00;True|2021-11-11T17:53:13.2835443+08:00;True|2021-11-11T17:52:00.9170918+08:00;True|2021-11-11T17:51:37.4852682+08:00;True|2021-11-11T17:49:30.9386192+08:00;True|2021-11-11T17:44:28.5146341+08:00;True|2021-11-11T17:42:26.8480671+08:00;True|2021-11-11T17:37:14.4108790+08:00;True|2021-11-11T17:30:25.4460722+08:00;</History> <History>True|2021-11-13T02:44:44.3567647Z;True|2021-11-13T10:42:01.8636514+08:00;True|2021-11-13T10:38:36.0623458+08:00;True|2021-11-13T10:20:29.4761762+08:00;True|2021-11-13T10:08:51.4924474+08:00;True|2021-11-12T15:27:50.0418057+08:00;True|2021-11-12T15:06:23.1220477+08:00;True|2021-11-11T18:06:38.5161322+08:00;True|2021-11-11T17:53:13.2835443+08:00;True|2021-11-11T17:52:00.9170918+08:00;True|2021-11-11T17:51:37.4852682+08:00;True|2021-11-11T17:49:30.9386192+08:00;True|2021-11-11T17:44:28.5146341+08:00;True|2021-11-11T17:42:26.8480671+08:00;True|2021-11-11T17:37:14.4108790+08:00;True|2021-11-11T17:30:25.4460722+08:00;</History>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
Loading…
Cancel
Save