using AllBase; using AllBase.Helper; using AllBase.Model.Result; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using TemuKJD.BLL; using TemuKJD.WXForms.ProductForms; using Work; namespace TemuKJD.PDDForms.ProductForms { // 微信店铺违规商品抓取 public class WXProdViolationManage { public void GetShopProdViolation() { Task.Run(() => { if (!Directory.Exists("wxProdViolation")) Directory.CreateDirectory("wxProdViolation"); while (true) { //break; foreach (var shop in GlobalConfig.ShopLoginList) { if (shop.LoginStatus != "登录成功") continue; Thread.Sleep(Randomx.GetId(500, 1500)); WXProdViolationGetInfo info = new WXProdViolationGetInfo() { shopId = shop.ShopId, updateDay = DateTime.Now.ToString("yyMMdd") }; try { info = NaiBa.Util.Helper.ConfigHelper.LoadConfig("wxProdViolation\\" + shop.ShopId); if (info == null) info = new WXProdViolationGetInfo() { shopId = shop.ShopId, updateDay = DateTime.MinValue.ToString("yyMMdd") }; if (info.updateDay == DateTime.Now.ToString("yyMMdd")) { // 今日同步过,跳过,暂时关闭改为每次打开立即读取 //continue; } info.failReason = ""; var result = getShopiolationProd(shop); Thread.Sleep(Randomx.GetId(1000, 2000)); if (!result.Contains("\"code\":0")) { WXForms.ShopForms.WXShopListForm.CheckShopLogin(shop); info.failReason = "读取失败"; NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxProdViolation\\" + shop.ShopId, info); continue; } JObject recordObj = JsonHelper.JObjectParse(result)["record"] as JObject; JArray jArr = JsonHelper.JObjectParse(result)["record"]["items"] as JArray; if (recordObj != null && jArr == null) { jArr = new JArray(); } foreach(var jObj in jArr) { string sVioInfo = JsonHelper.SerializeObject(jObj); var punishUuid = BaseRegex.GetStringJson(ref sVioInfo, "punishUuid"); var punishReason = BaseRegex.GetStringJson(ref sVioInfo, "punishReason"); var punishTime = BaseClass.StampToDateTime(BaseRegex.GetIntJson(ref sVioInfo, "punishTime")); var productId = BaseRegex.GetStringJson(ref sVioInfo, "productId"); var productDesc = BaseRegex.GetStringJson(ref sVioInfo, "productDesc"); //相似同款低质商品 不当使用他人权利 if (!punishReason.Contains("发布违禁信息") && !punishReason.Contains("材质夸大宣传") && !punishReason.Contains("相似同款低质商品") && !punishReason.Contains("价格违规") && !punishReason.Contains("售假") && !punishReason.Contains("品牌资质问题") && !punishReason.Contains("不当使用他人权利") && !punishReason.Contains("用户反馈虚假宣传")) continue; if (!string.IsNullOrWhiteSpace(productDesc) && !string.IsNullOrWhiteSpace(productId)) { var url = BaseClass.BuildPDDObjString("product/mdy_violation_prod"); var data = UserLogin.GetSignaturex(); data += "&wx_safe_status=违规商品"; data += "¬es=" + GetHtml.UrlEncodeUtf8(("违规轮询-" + shop.ShopId + "-" + punishUuid + "-" + punishReason + "-" + punishTime.ToString("yy/MM/dd HH:mm")).Trim()); data += "&product_name=" + productDesc; var html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (!html.Contains("\"code\":200")) { info.failReason = "更新商品状态失败"; break; } if(html.Contains("商品不存在")) { var getProdRst = WXUpload.searchProductWithTitle(shop, GetHtml.UrlEncodeUtf8(productDesc)); var spuCode = BaseRegex.GetStringJson(ref getProdRst, "spuCode"); var secrec_spu_id = ""; var spu_id = ""; if (string.IsNullOrEmpty(spuCode) || spuCode.Length < 10) { continue; } if (spuCode.StartsWith("EC1") && spuCode.EndsWith("A")) { spu_id = spuCode.Substring(3, spuCode.Length - 4); } else { secrec_spu_id = spuCode; } url = BaseClass.BuildPDDObjString("product/mdy_violation_prod"); data = UserLogin.GetSignaturex(); data += "&wx_safe_status=违规商品"; data += "¬es=" + ("违规轮询-" + shop.ShopId + "-" + punishUuid + "-" + punishReason).Trim(); data += "&secret_spu_id=" + secrec_spu_id; data += "&spu_id=" + spu_id; html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (!html.Contains("\"code\":200")) { info.failReason = "更新商品状态失败"; break; } } } } if (string.IsNullOrEmpty(info.failReason)) info.updateDay = DateTime.Now.ToString("yyMMdd"); NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxProdViolation\\" + shop.ShopId, info); Thread.Sleep(5000); } catch (Exception ex) { try { if (info == null) info = new WXProdViolationGetInfo() { shopId = shop.ShopId, updateDay = DateTime.Now.ToString("yyMMdd") }; info.failReason = ex.Message; NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxProdViolation\\" + shop.ShopId, info); } catch { } } } // 休眠两个小时 Thread.Sleep(1000 * 60 * 60 * 2); } }); } public static string getShopiolationProd(ShopLoginInfo shop) { string url = "https://store.weixin.qq.com/shop-faas/mmecnodeviolationsec/violation/cgi/get?token=&lang=zh_CN&violationType=finder&shopId="; string referer = "https://store.weixin.qq.com/shop/violation/home"; var result = WXHttpResponseBLL.WeiXinGetResponse(shop, url, referer); return result; } } public class WXProdViolationGetInfo { public string shopId { get; set; } public string updateDay { get; set; } // yyMMdd public string failReason { get; set; } // "", "掉线"... } }