Files
29WxOrder/Work/MulitServicerSet.cs
2026-06-11 15:17:50 +08:00

280 lines
11 KiB
C#

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<string, ShopChatBase> Shops = new Dictionary<string, ShopChatBase>();
public static List<ShopChatBase> ShopsTemp = new List<ShopChatBase>();
public static List<ShopChatBase> ChatShopsTemp = new List<ShopChatBase>();
//public static Dictionary<int, WebSocketClient> Chats = new Dictionary<int, WebSocketClient>();
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, @"<meta [^>]*?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 "";
}
}
/// <summary>
/// 生成新的cookies
/// </summary>
/// <param name="cookies"></param>
/// <param name="setCookies"></param>
/// <returns></returns>
public string SetCookies(string cookies, string setCookies)
{
if (string.IsNullOrEmpty(setCookies))
return cookies;
Dictionary<string, string> cook = new Dictionary<string, string>();
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<string, string> 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<ShopChatBase>(html);
// return tp;
// }
}