using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; using AllBase; using CefSharp.WinForms; using Work.Pinduoduo; namespace Work { public class MulitServicerSet { public static Dictionary Shops = new Dictionary(); public static List ShopsTemp = new List(); public static List ChatShopsTemp = new List(); //public static Dictionary Chats = new Dictionary(); public string Name; public static int ThisCount = 0, MaxCount = 0, WaitTime = 5; public MulitServicerSet(string name) { Name = name; } private string GetUid() { string url = "http://mms.pinduoduo.com/Pdd.html"; string cookies = ""; string html = GetWeb(url, url, ref cookies, ""); return cookies; } public string GetWeb(string url, string refere, ref string cookies, string data) { try { string userAgent = BaseClass.ChromeUserAgent; WebClientx wb = new WebClientx(); wb.Timeout = 30; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); //wb.Headers.Add("X-Requested-With", "XMLHttpRequest"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = null; string rehead = wb.Headers.ToString(); var encode = Encoding.UTF8; byte[] b, postData = data == null ? new byte[0] : encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data)) { b = wb.DownloadData(url); } else { wb.Headers.Add("Content-Type", "application/json; charset=UTF-8"); wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); string setCookies = wb.ResponseHeaders["Set-Cookie"]; cookies = SetCookies(cookies, setCookies); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } /// /// 生成新的cookies /// /// /// /// public string SetCookies(string cookies, string setCookies) { if (string.IsNullOrEmpty(setCookies)) return cookies; Dictionary cook = new Dictionary(); string[] l = cookies.Split(';'); for (int i = 0; i < l.Length; i++) { if (string.IsNullOrEmpty(l[i]) || !l[i].Contains("=")) continue; string key = l[i].Substring(0, l[i].IndexOf("=")); string value = l[i].Substring(key.Length + 1).Trim(); if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) continue; if (cook.ContainsKey(key)) cook[key] = value; else cook.Add(key, value); } l = setCookies.Split(';'); for (int i = 0; i < l.Length; i++) { if (string.IsNullOrEmpty(l[i]) || !l[i].Contains("=")) continue; string key = l[i].Substring(0, l[i].IndexOf("=")).Trim(); if (key == "expires" || key == "domain" || key == "path" || key == "Max-Age") continue; string value = l[i].Substring(key.Length + 1); if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) continue; if (cook.ContainsKey(key)) cook[key] = value; else { cook.Add(key, value); } } StringBuilder s = new StringBuilder(); foreach (KeyValuePair key in cook) { s.Append(key.Key + "=" + key.Value + "; "); } return s.ToString(); } public static string tokenMessage = ""; //public static bool LoginChat(ShopChatBase shop, out string message) //{ // //WebSocketClient websocket = new WebSocketClient(shop); // //if (websocket.client == null) // //{ // // message = "新版客服登录失败"; // // shop.Message = "新版客服登录失败"; // // if (!string.IsNullOrEmpty(tokenMessage)) // // { // // string url = "https://mms.pinduoduo.com/chats/malls/" + shop.MallId + "/enableChat"; // // string refere = "hhttps://mms.pinduoduo.com/chat-merchant/index.html"; // // string html = GetPostTaobaoHtml.UploadTaobao(url, refere, true, shop.Cookies); // // if (html.Contains("{\"success\":true")) // // { // // websocket = new WebSocketClient(shop); // // } // // else // // { // // message = tokenMessage; // // } // // } // // return false; // //} // //if (!Chats.ContainsKey(shop.MallId)) // //{ // // Chats.Add(shop.MallId, websocket); // //} // //else // //{ // // if (Chats[shop.MallId].client == null) // // { // // Chats[shop.MallId] = websocket; // // message = "新版客服登录失败"; // // shop.Message = "新版客服登录失败"; // // return false; // // } // // if (Chats[shop.MallId].client.ReadyState != WebSocketState.Open || Chats[shop.MallId].client.ReadyState != WebSocketState.Closing) // // { // // Chats[shop.MallId].client.Connect(); // // } // // Chats[shop.MallId].client.Connect(); // //} // //message = "新版客服登录成功"; // //shop.Message = "新版客服登录成功"; // //if (shop.client != null && shop.client.ReadyState != WebSocketState.Open) // //{ // // return true; // //} // //else // //{ // // return false; // //} //} } //public class ShopChatBase //{ // public string ExpireTime = ""; // public string ShopId = ""; // public string ShopName = ""; // public string Sub_Account_Name = "";//子账号 // public string Name = ""; // public string UserName = ""; // public string UserInfo = ""; // public string PassId = ""; // public int ChatCount = 0; // public string Cookies = ""; // public string CookiesUid = ""; // public string CookiesPid = ""; // public string Password = ""; // public string Message = "";//授权状态 // public string onlineStatus = "";//在线状态 // public bool Showed = false; // public bool CanUpload = true; // public bool Login = false; // public int X = 0; // public int Y = 0; // public int MallId = 0; // public int MainCatId = 0; // public int ReceiverId = 0; // public string MainCatName = ""; // public string UserCatName = ""; // public string ServiceUserName = ""; // public string BuyUserName = ""; // public string UploadUserName = ""; // public int UpCount = 0; // public long ExpressId = 0; // public int WaitTimes = 0; // public string AccessToken = ""; // public string RefreshToken = ""; // public string ClientId = ""; // public string AccessTokenOrder = ""; // public string RefreshTokenOrder = ""; // public DateTime Expiration = DateTime.Now.AddDays(-1); // public DateTime EndDate = DateTime.Now.AddDays(-1); // public int allWait = 0;//待拼团 // public int allBuying = 0;//待拍单 // [Newtonsoft.Json.JsonIgnore] // public WebSocket client = null; // [Newtonsoft.Json.JsonIgnore] // public ChromiumWebBrowser Chat = null;//店铺聊天用 // [Newtonsoft.Json.JsonIgnore] // public ChromiumWebBrowser MainIndex = null;//店铺后台用 // public bool CusIsLogin = false;//店铺是否已经登录--客服集成 // public string ownId = ""; // public string verify_auth_token = ""; // public ShopChatBase Clone() // { // string html = JsonHelper.SerializeObject(this); // ShopChatBase tp = JsonHelper.DeserializeJsonToObject(html); // return tp; // } }