using System; using System.Collections.Generic; using System.Text; using System.Threading; using AllBase; using System.IO; using System.Windows.Forms; using Work.Tips; using System.Threading.Tasks; using System.Linq; using Work.Pinduoduo; using Work.Taobao; using Work.BuyUsers; namespace Work { public class AutoConfirmExpress { public static List ExpressList = new List(); public int BuyType; public string Cookies = ""; public static List ordersx = new List();//存储已发货的订单--拍单 public static Dictionary beforeOrderx = new Dictionary(); public static List ordersp = new List();//存储已发货的订单--拼单 public static Dictionary beforeOrderp = new Dictionary(); public Dictionary Province = new Dictionary(); public AutoConfirmExpress() { } public AutoConfirmExpress(string cookies) { Cookies = cookies; } public AutoConfirmExpress(int buyType, string cookies) { Cookies = cookies; BuyType = buyType; if (ExpressList.Count > 0) { return; } var m = BaseRegex.GetMatchRegex(ref ExpressNames, @"([^<]+?)"); for (int i = 0; i < m.Count; i++) { string key = m[i].Groups[1].Value; string value = m[i].Groups[2].Value; KeyValueBase k = new KeyValueBase(key, value); ExpressList.Add(k); } } public AutoConfirmExpress(int buyType) { BuyType = buyType; if (ExpressList.Count > 0) { return; } var m = BaseRegex.GetMatchRegex(ref ExpressNames, @"([^<]+?)"); for (int i = 0; i < m.Count; i++) { string key = m[i].Groups[1].Value; string value = m[i].Groups[2].Value; KeyValueBase k = new KeyValueBase(key, value); ExpressList.Add(k); } } public static List TaobaoOrder = new List(); public void GetTaobao(string cookies) { //Dictionary>> chars = new Dictionary>>(); string url = "https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&tabCode=waitConfirm"; string refere = url; string html = GetPostTaobaoHtml.UploadTaobao(url, refere, false, cookies); html = GetHtml.Decode(html); if (!string.IsNullOrEmpty(html)) html = html.Replace("\\\"", "\"").Replace("\\/", "/"); int page = BaseRegex.GetIntJson(ref html, "totalPage"); for (int k = 1; k <= page; k++) { if (k != 1) { //string data = // "buyerNick=&dateBegin=0&dateEnd=0&lastStartRow=&logisticsService=&options=0&orderStatus=&pageNum=" + k // + "&pageSize=15&queryBizType=&queryOrder=desc&rateStatus=&refund=&sellerNick=&prePageNo=2"; string data = "lastStartRow=&options=0&orderStatus=SEND&pageNum=" + k + "&pageSize=15&queryBizType=&queryOrder=desc&tabCode=waitConfirm&prePageNo=" + (k - 1); url = "https://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&_input_charset=utf8"; html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies); Thread.Sleep(5 * 1000); } var m = BaseRegex.GetMatchRegex(ref html, @"\{""extra"":([\w\W]+?)""quantity"":""([\d]+)"""); for (int i = 0; m != null && i < m.Count; i++) { string list = m[i].Groups[0].Value; string oid = BaseRegex.GetLongJson(ref list, "id").ToString(); if (oid == "553074309923804790") { } //if (!beforeOrder.ContainsKey(oid)) //{ // continue; //} //根据上家的订单从数据库获取此订单 string status = BaseRegex.GetStringJson(ref list, "tradeStatus"); if (status != "WAIT_BUYER_CONFIRM_GOODS" && status != "TRADE_BUYER_SIGNED" && status != "TRADE_CLOSED" && status != "TRADE_FINISHED") { continue; } string sell = BaseRegex.GetMatchRegexString(ref list, @"""seller"":\{([^\}]+?)\}"); long shopId = BaseRegex.GetLongJson(ref sell, "id"); url = "https://wuliu.taobao.com/user/order_detail_new.htm?trade_id=" + oid + "&seller_id=" + shopId; html = GetPostTaobaoHtml.UploadTaobao(url, refere, false); Thread.Sleep(1500); string no = BaseRegex.GetMatchRegexString(ref html, @"\s+([^<]+?)"); string company = BaseRegex.GetMatchRegexString(ref html, @"\s+([^<]+?)"); if (string.IsNullOrEmpty(no) || string.IsNullOrEmpty(company)) { continue; } no = no.Trim(); company = company.Trim(); if (company == "中国邮政") { company = "邮政快递包裹"; } var express = GetExpressId(company); if (express == null) { LogSave.Add("没有找到快递公司"); continue; } company = express.Value; int companyId = int.Parse(express.Key); //string orderId = beforeOrder[oid]; } } } public KeyValueBase GetExpressId(string company) { if (ExpressList.Count == 0) { var m = BaseRegex.GetMatchRegex(ref ExpressNames, @"([^<]+?)"); for (int i = 0; i < m.Count; i++) { string key = m[i].Groups[1].Value; string value = m[i].Groups[2].Value; KeyValueBase k = new KeyValueBase(key, value); ExpressList.Add(k); } } for (int i = 0; i < ExpressList.Count; i++) { if (ExpressList[i].Value == company || ExpressList[i].Value.Replace("快递", "速递") == company || ExpressList[i].Value.Replace("快递", "速运") == company) return ExpressList[i]; } for (int i = 0; i < ExpressList.Count; i++) { int subNum = 2;//截取的长度 if (ExpressList[i].Value.Contains("邮政") && ExpressList[i].Value.Length > 3 && company.Length > 3) { subNum = 4; } if (ExpressList[i].Value.Length > 1 && company.Length > 1 && ExpressList[i].Value.Substring(0, subNum) == company.Substring(0, subNum)) return ExpressList[i]; } return null; } #region 新方式获取买手号拍单物流信息 #endregion #region 从拼多多买手获取上家发货的物流信息后,自动购买快递单 public string IsBuyEmptyBag(string orderId) { string data = UserLogin.GetSignaturex(); StringBuilder s = new StringBuilder(); s.Append("&order_id=" + orderId); string url = BaseClass.BuildObjString("pay_group_order&act=get_empty_id"); string html = GetHtml.PostUtf8(url, data + s); return html; } #region 将上家Id存到txt public static void PreserveBuyEmptyBags(string orderId) { string path = Environment.CurrentDirectory + "\\temp\\emptybag_temp"; try { //判断文件路径是否存在 DirectoryInfo directory = new DirectoryInfo(path); bool isHas = directory.Exists; if (!isHas)//若文件路径不存在则创建 { isHas = FileBase.DirectoryCreate(path); } if (isHas) { string file = "BuyEmptyBag.txt"; string filePath = Environment.CurrentDirectory + "\\temp\\emptybag_temp\\" + file; //FileStream fileStream = new FileStream(filePath, FileMode.Create); StreamWriter streamWriter = new StreamWriter(filePath, true); string content = orderId; streamWriter.Write(content + "\r\n"); streamWriter.Flush(); streamWriter.Close(); //fileStream.Close(); } } catch (Exception) { //throw; } } #endregion #endregion #region 从服务端获取已发货的订单 public void GetConfirmOrders() { int page = 1; string url, html; for (int p = 0; p < page; p++) { Thread.Sleep(1000); string data = UserLogin.GetSignaturex(); url = BaseClass.BuildObjString("pay_group_order&act=get_confirm_order&buy_type=" + BuyType + "&page=" + p); html = GetHtml.PostUtf8(url, data); page = BaseRegex.GetIntValue(html, "page"); if (string.IsNullOrEmpty(html)) { LogSave.Add("拼单", "获取订单列表网络异常,请稍后重试"); return; } var mm = BaseRegex.GetMatchRegex(ref html, "]+?)>"); for (int i = 0; i < mm.Count; i++) { string list = mm[i].Groups[0].Value; string orderId = BaseRegex.GetStringJson(ref list, "order_id_buy"); string orderIdb = BaseRegex.GetStringJson(ref list, "order_id"); if (string.IsNullOrEmpty(orderId)) { continue; } if (BuyType == 1) { var isHas = ordersp.Find(x => x != null && x == orderId); if (isHas == null) ordersp.Add(orderId); if (!beforeOrderp.ContainsKey(orderId)) beforeOrderp.Add(orderId, orderIdb); } else { var isHas = ordersx.Find(x => x != null && x == orderId); if (isHas == null) ordersx.Add(orderId); if (!beforeOrderx.ContainsKey(orderId)) beforeOrderx.Add(orderId, orderIdb); } } } } #endregion /// /// 订单确认收货 /// /// /// public void ConfirmOrder(string oid, string goods_id) { string url = "http://apiv4.yangkeduo.com/order/" + oid + "/received?pdduid=" + BaseClass.PddUid + "&is_back=1"; string data = "{}"; string refere = BaseClass.PddMobileUrlx + "orders.html?refer_page_name=login&refer_page_id=login_1513005785399_B4Dxk8TYgA&type=3&order_index=0"; string html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, Cookies); refere = BaseClass.PddMobileUrlx + "comments.html?order_sn=" + oid + "&goods_id=" + goods_id + "&page_el_sn=99664&type=3&refer_page_name=my_order&refer_page_id=my_order_1513006052449_n43miXeWuI&refer_page_sn=10032&refer_page_el_sn=99664"; url = "http://apiv4.yangkeduo.com/v2/order/goods/review?pdduid=" + BaseClass.PddUid; data = "{\"goods_id\":" + goods_id + ",\"order_sn\":\"" + oid + "\",\"desc_score\":5,\"logistics_score\":5,\"service_score\":5,\"comment\":\"\",\"pictures\":[]}"; html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, Cookies); if (html.Contains("rating_id")) { LogSave.Add("拼单", "确认收货成功:" + oid); } Thread.Sleep(300); } public bool UpdateServer(string order_id, string shippingName, string trackingNumber, int expressId, string oId, string status) { StringBuilder s = new StringBuilder(); if (status != "发货成功" && status != "快递单发货成功" && status != "错误单发货成功" && status != "单号修改成功" && !status.Contains("单号修改失败"))//已发货订单不改成上家发货 { s.Append("&status=" + 8);//上家发货 } s.Append("&oid=" + order_id); s.Append("&express_name=" + shippingName); s.Append("&express_id=" + expressId); s.Append("&express_no=" + trackingNumber); string now = BaseClass.GetServerTimeString().ToString("yyyy-MM-dd HH:mm:ss"); s.Append("&get_up_express_time=" + now); s.Append("&get_time=1"); string data = UserLogin.GetSignaturex(); string url = BaseClass.BuildObjString("pay_group_order&act=updatex&buy_type=" + BuyType); string html = GetHtml.PostUtf8(url, data + s); if (html != "ok") { Thread.Sleep(10 * 1000); data = UserLogin.GetSignaturex(); html = GetHtml.PostUtf8(url, data + s); } if (html != "ok") { Thread.Sleep(30 * 1000); data = UserLogin.GetSignaturex(); html = GetHtml.PostUtf8(url, data + s); } if (html != "ok") { //LogSave.Add("拼单", "自动提交物流信息失败,请检查网络"); string msg = "【订单:" + order_id + "】 获取上家物流更新到服务器异常" + html + "\r\n\r\n"; FileBase.WriteTxt("\\temp\\UpdUpExpLog", "updUpExpLog", msg, false); return false; } else { return true; } } #region 获取进价 public string CheckOrderPrice(string orderId_buy, string orderId, string payMobile) { string buyPrice = ""; //string provinceName = ""; //string cityName = ""; //string districtName = ""; //string shippingAddress = ""; string addressDetail = ""; string receiveName = ""; string mobile = ""; if (orderId_buy.Contains("-")) { var payAccount = PddBuyUser.buyUserCookies.First(x => x.Value.UserName == payMobile); string cookies = ""; for (int j = 0; j < payAccount.Value.Cookies.Count; j++) { cookies += payAccount.Value.Cookies[j].Name + "=" + payAccount.Value.Cookies[j].Value + "; "; } string url = BaseClass.PddMobileUrl + "order.html?order_sn=" + orderId_buy + "&refer_page_name=my_order&refer_page_id=my_order_1513009667550_P4xCgmERE6&refer_page_sn=10032"; string refere = BaseClass.PddMobileUrl; string html = GetPostTaobaoHtml.UploadTaobao(url, refere, true, cookies); html = GetHtml.UrlDecode(html); buyPrice = BaseRegex.GetMatchRegexString(ref html, "¥([\\s\\S]+?)"); if (html.Contains("window.rawData=null;")) { MessageBox.Show("买手号已掉线,无法读取"); return ""; } if (string.IsNullOrEmpty(buyPrice)) { decimal buyPriceTemp = BaseRegex.GetDecimalJson(ref html, "displayAmount"); if (buyPriceTemp > 0) { buyPrice = (buyPriceTemp / 100).ToString(); } } string provinceName = BaseRegex.GetStringJson(ref html, "provinceName"); string cityName = BaseRegex.GetStringJson(ref html, "cityName"); string districtName = BaseRegex.GetStringJson(ref html, "districtName"); string shippingAddress = BaseRegex.GetStringJson(ref html, "shippingAddress"); addressDetail = provinceName + cityName + districtName + shippingAddress; receiveName = BaseRegex.GetStringJson(ref html, "receiveName"); mobile = BaseRegex.GetStringJson(ref html, "mobile"); } else { TbHttpReauest tbRequest = new TbHttpReauest(); var payAccount = TaoBaoBuyUser.buyUserCookies.First(x => x.Value.UserName == payMobile); if (payAccount.Key.site_id==3)//1688读进货价 { tbRequest.url = "https://trade.1688.com/order/new_step_order_detail.htm?orderId=" + orderId_buy; tbRequest.cookies = Cookies; string html = tbRequest.MakeRequests(); //把 GBK 数据 转换为 URL 编码的字符串 html = GetHtml.UrlDecode(html).Replace("\\", ""); System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("(?<=(total-price\">))[.\\s\\S]*?(?=())", System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.Singleline); buyPrice = rg.Match(html).Value; } else { tbRequest.url = "https://trade.taobao.com/trade/detail/trade_order_detail.htm?biz_order_id=" + orderId_buy; tbRequest.cookies = Cookies; string html = tbRequest.MakeRequests(); html = GetHtml.UrlDecode(html).Replace("\\", ""); //url = "https://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&_input_charset=utf8"; //string refer = "https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm"; //string data = "pageNum=1&pageSize=15&auctionTitle=" + orderId_buy + "&itemTitle=" + orderId_buy + "&prePageNo=1"; //html = GetPostTaobaoHtml.UploadTaobaoPost(url, refer, true, data, true, Cookies); buyPrice = BaseRegex.GetMatchRegexString(ref html, "\"name\":\"实付款\",\"value\":\"¥([\\s\\S]+?)\""); if (string.IsNullOrEmpty(buyPrice)) { string url = "https://trade.taobao.com/trade/detail/trade_order_detail.htm?biz_order_id=" + orderId_buy; //string url = "https://trade.taobao.com/trade/detail/trade_order_detail.htm?biz_order_id=" + orderId_buy; html = GetPostTaobaoHtml.UploadTaobao(url, url, true, Cookies); if (html.Contains("滑动一下") || html.Contains("验证码") || html.Contains("_____tmd_____/punish")) { html = GetPostTaobaoHtml.UploadTaobao(url, url, true);//用滑动之后的cookies获取 if (html.Contains("滑动") || html.Contains("_____tmd_____/punish")) { url = "https://trade.tmall.com/detail/orderDetail.htm?biz_order_id=" + orderId_buy + "&forward_action="; html = GetPostTaobaoHtml.UploadTaobao(url, url, true, Cookies); } } html = GetHtml.UrlDecode(html).Replace("\\", ""); buyPrice = BaseRegex.GetMatchRegexString(ref html, "\"name\":\"实付款\",\"value\":\"¥([\\s\\S]+?)\""); if (string.IsNullOrEmpty(buyPrice)) { var arrPrice = BaseRegex.GetMatchRegex(ref html, "({\"content\":[{\"data\":{\"money\":[\\s\\S]+?\"text\":\"[\\s\\S]+?\",\"type\":\"label\"}})"); for (int i = 0; i < arrPrice.Count; i++) { var item = arrPrice[i].ToString(); if (item.Contains("实付款")) { buyPrice = BaseRegex.GetMatchRegexString(ref item, "\"text\":\"¥([\\s\\S]+?)\""); break; } } } } if (string.IsNullOrEmpty(buyPrice)) { buyPrice = BaseRegex.GetMatchRegexString(ref html, "\"name\":\"订单总金额\",\"value\":\"([\\s\\S]+?)\""); } if (string.IsNullOrEmpty(buyPrice)) { buyPrice = BaseRegex.GetStringJson(ref html, "priceInfo"); } if (string.IsNullOrEmpty(buyPrice)) { decimal tempPrice = 0M; try { var tempPriceInfo = System.Text.RegularExpressions.Regex.Matches(html, "priceInfo\":\\[\\{\"text\":\"¥[\\s\\S]+\",\"type\":\"label\"\\}\\],\"quantity\":\"[\\d]+?\"\\}\\]"); if (tempPriceInfo.Count > 0) { string tempPriceInfoStr = tempPriceInfo[0].ToString(); string tempPriceStr = BaseRegex.GetMatchRegexString(ref tempPriceInfoStr, "priceInfo\":\\[\\{\"text\":\"¥([\\s\\S]+)\",\"type\":\"label\"\\}\\],\"quantity\":\"([\\d]+?)\"\\}\\]"); if (!string.IsNullOrEmpty(tempPriceStr)) { tempPrice = Convert.ToDecimal(tempPriceStr);//单个商品价格 int quantity = BaseRegex.GetIntJson(ref tempPriceInfoStr, "quantity");//数量 tempPrice = tempPrice * quantity; buyPrice = tempPrice.ToString(); } } } catch (Exception) { try { var tempPriceInfo = System.Text.RegularExpressions.Regex.Matches(html, "priceInfo\":\\[\\{\"text\":\"¥[\\s\\S]+\",\"type\":\"label\"\\}\\],\"quantity\":\"[\\d]+?\"\\}\\]\\},"); if (tempPriceInfo.Count > 0) { string tempPriceInfoStr = tempPriceInfo[0].ToString(); string tempPriceStr = BaseRegex.GetMatchRegexString(ref tempPriceInfoStr, "priceInfo\":\\[\\{\"text\":\"¥([\\s\\S]+)\",\"type\":\"label\"\\}\\],\"quantity\":\"([\\d]+?)\"\\}\\]"); if (!string.IsNullOrEmpty(tempPriceStr)) { tempPrice = Convert.ToDecimal(tempPriceStr);//单个商品价格 int quantity = BaseRegex.GetIntJson(ref tempPriceInfoStr, "quantity");//数量 tempPrice = tempPrice * quantity; buyPrice = tempPrice.ToString(); } } } catch (Exception) { //throw; } //throw; } } if (string.IsNullOrEmpty(buyPrice)) { string url = "https://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&_input_charset=utf8"; string refer = "https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm"; string data = "pageNum=1&pageSize=15&auctionTitle=" + orderId_buy + "&itemTitle=" + orderId_buy + "&prePageNo=1"; string htmlx = GetPostTaobaoHtml.UploadTaobaoPost(url, refer, true, data, Cookies); string tempStr = BaseRegex.GetMatchRegexString(ref htmlx, "(\"payInfo\":\\{\"actualFee\":\"[\\s\\S]+?\",)"); if (!string.IsNullOrEmpty(tempStr)) { buyPrice = BaseRegex.GetStringJson(ref tempStr, "actualFee"); } if (string.IsNullOrEmpty(buyPrice)) { htmlx = GetPostTaobaoHtml.UploadTaobaoPost(url, refer, true, data); tempStr = BaseRegex.GetMatchRegexString(ref htmlx, "(\"payInfo\":\\{\"actualFee\":\"[\\s\\S]+?\",)"); if (!string.IsNullOrEmpty(tempStr)) { buyPrice = BaseRegex.GetStringJson(ref tempStr, "actualFee"); } } } if (!string.IsNullOrEmpty(buyPrice)) { buyPrice = buyPrice.Replace("¥", ""); } string address = BaseRegex.GetStringJson(ref html, "address"); if (!string.IsNullOrEmpty(address)) { var receiverInfoArr = address.Split(','); if (receiverInfoArr.Length == 4) { receiveName = receiverInfoArr[0]; mobile = receiverInfoArr[1]; addressDetail = receiverInfoArr[2]; } } } } if (string.IsNullOrEmpty(buyPrice)) { return "0"; } return buyPrice; } #endregion public bool isReturnProduct(string orderId_buy, string cookies) { if (orderId_buy.Contains("-")) { string url = BaseClass.PddMobileUrl + "order.html?order_sn=" + orderId_buy + "&refer_page_name=my_order&refer_page_id=my_order_1513009667550_P4xCgmERE6&refer_page_sn=10032"; string refere = BaseClass.PddMobileUrl; string html = GetPostTaobaoHtml.UploadTaobao(url, refere, true, cookies); html = GetHtml.UrlDecode(html); if (html.Contains("退款成功(详情)")) { return true; } } return false; } public void ResetErrorSend() { string data = UserLogin.GetSignaturex(); string url = BaseClass.BuildObjString("pay_group_order&act=reset_status"); string html = GetHtml.PostUtf8(url, data); if (html != "ok") { } } public string ExpressNames = "申通快递百世快递顺丰快递圆通快递赛澳递晟邦物流中通快递全峰快递优速快递邮政EMS天天快递京东配送韵达快递快捷快递国通快递宅急送快递如风达德邦快递邮政快递包裹龙邦快递联邦快递九曳供应链南京晟邦万象物流立即送门对门安达信海外快递飞远物流汇文快递黄马甲速尔快递亚马逊物流黑猫宅急便上海赛澳递城市100芝麻开门顺捷丰达汇通小红马其他跨越速递usps青岛安捷贝海国际速递环球速运全一快递远成快运联众国际快运天地华宇中邮速递EMS-国际件中铁物流新邦物流Flash Express新顺丰NSFDCS GLOBAL日日顺物流苏宁快递百世快运德邦物流微特派AAE全球专递AramexAsendia USACity-LinkCOEDHL德国DHL全球DHL中国EMS国际FedEx美国FedEx中国OCSOnTracTNTUPS阿尔巴尼亚邮政阿根廷邮政阿联酋邮政爱沙尼亚邮政奥地利邮政澳大利亚邮政巴基斯坦邮政巴西邮政白俄罗斯邮政百福东方包裹信件保加利亚邮政比利时邮政波兰邮政传喜物流大田物流递四方俄速通法国邮政凡宇快递飞豹快递飞远(爱彼西)配送芬兰邮政哥伦比亚邮政韩国邮政恒路物流华企快运加运美佳吉快运佳怡物流嘉里大通捷克邮政筋斗云物流京广速递克罗地亚邮政拉脱维亚邮政黎巴嫩邮政联昊通马耳他邮政马其顿邮政毛里求斯邮政秘鲁邮政民邦快递民航快递明亮物流摩尔多瓦邮政南非邮政挪威邮政葡萄牙邮政全日通日本邮政瑞典邮政瑞士邮政塞尔维亚邮政三态速递沙特邮政圣安物流盛丰物流盛辉物流斯洛伐克邮政斯洛文尼亚邮政穗佳物流泰国邮政土耳其邮政万家物流乌克兰邮政西班牙邮政信丰物流匈牙利邮政亚风速递亚美尼亚邮政燕文物流意大利邮政银捷速递印度邮政英国皇家邮政邮政标准快递邮政国际包裹越丰物流运通中港快递增益速递智利邮政中俄快递中铁快运快服务快递优邦国际速运天际快递飞洋快递斑马物联网易客满京东大件物流速必达"; } }