using AllBase; using AllBase.Helper; using AllBase.Model.Param.PDDProdParam; using AllBase.Model.Result; using AllBase.Model.Result.PDDResult; using AllBase.Model.Result.WXResult; using NaiBa.Util.Extension; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using TemuKJD.BaseForms; using TemuKJD.BLL; using TemuKJD.PDDForms.ProductThirdSelectForms; using TemuKJD.WXForms.AdminForms; using TemuKJD.WXForms.ProductForms; using Work; namespace TemuKJD.WXForms.ShopForms { public class WXShopBrowserObject { public delegate void InfoReceivedHandler(string information); public event InfoReceivedHandler OnInfoReceived; public string getProduct(string sProdId, string supplierId, bool isReGet = true, bool notSaveDb = false) { var prodId = sProdId.ParseToLong(); if (prodId <= 0) { MainForm.instance.Invoke(new Action(() => { BaseMsgForm.ShowError("商品Id不存在!"); })); return "failed"; } //var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == supplierId.ParseToString()); var shop = WXAdminShopListForm.lstAdminShopInfo.FirstOrDefault(s => s.ShopId == supplierId.ParseToString()); if (shop == null) shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == supplierId.ParseToString()); if (string.IsNullOrEmpty(supplierId) || shop == null) { MainForm.instance.Invoke(new Action(() => { BaseMsgForm.ShowError("店铺不存在!"); })); return "failed"; } Task.Run(() => { var msg = reGetProductWidthWXFormat(shop, sProdId, isReGet); if (!string.IsNullOrEmpty(msg)) { if (!msg.StartsWith("success") && !msg.StartsWith("商品回采保存成功") && !msg.Contains("微信商品采集入库成功")) { MainForm.instance.Invoke(new Action(() => { BaseMsgForm.ShowError(msg); })); } else { MainForm.instance.Invoke(new Action(() => { BaseMsgForm.ShowInfo(msg); })); } } }); return "success"; } public string reGetProductWidthWXFormat(ShopLoginInfo shop, string prodId, bool isReGet = true) { string getString = isReGet ? "商品回采" : "商品采集"; try { Thread.Sleep(Randomx.GetId(10, 50)); string getEditProdRst = ""; try { getEditProdRst = WXUpload.getEditProductV2(shop, prodId); Thread.Sleep(Randomx.GetId(50, 100)); if (getEditProdRst.Contains("biz magic invalid")) { WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false); } if (!getEditProdRst.Contains("\"success\":true")) { var errmsg = BaseRegex.GetStringJson(ref getEditProdRst, "msg"); if (string.IsNullOrWhiteSpace(errmsg)) errmsg = getEditProdRst; if (shop.LoginStatus != "登录成功") errmsg = "店铺掉线"; return getString + "失败!" + errmsg; } } catch (Exception ex) { //OnInfoReceived("商品获取失败!商品数据格式获取失败!" + ex.Message); return getString + "失败!商品数据格式获取失败!" + ex.Message; } WXGetEditProductInfoLite wxProdInfoLite = null; try { wxProdInfoLite = JsonHelper.DeserializeJsonToObject(getEditProdRst); var addressRst = WXUpload.getAddressById(shop, wxProdInfoLite.product.info.afterSaleInfo.afterSaleAddressId); if (addressRst.Contains("biz magic invalid")) { WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false); } if (!addressRst.Contains("\"success\":true")) { var errmsg = BaseRegex.GetStringJson(ref addressRst, "msg"); if (string.IsNullOrWhiteSpace(errmsg)) errmsg = addressRst; if (shop.LoginStatus != "登录成功") errmsg = "店铺掉线"; return getString + "失败!" + errmsg; } var addressStr = JsonHelper.JObjectParse(addressRst)["addressList"].FirstOrDefault().ParseToString(); wxProdInfoLite.address = JsonHelper.DeserializeJsonToObject(addressStr); if (isReGet && wxProdInfoLite.address == null) return getString + "失败!商品详情获取失败"; // 查看下json结构 var wxProdInfoLiteStr = JsonHelper.SerializeObject(wxProdInfoLite); if (string.IsNullOrWhiteSpace(wxProdInfoLite.product.info.spuCode)) { if (isReGet) { return getString + "失败:外部编码采集失败,跳过"; } else { return SaveWxProd(shop, prodId, wxProdInfoLite); } } } catch (Exception ex) { return getString + "失败:格式转换失败 " + ex.Message; } // 查询商品库有没有 var url = BaseClass.BuildPDDObjString("product/get_product_spu_id_list"); Thread.Sleep(200); var data = UserLogin.GetSignaturex(); data += "&search_secret_spu_ids=" + wxProdInfoLite.product.info.spuCode; data += "&wx_safe_status=" + "安全商品,未校验"; var result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) { var errMsg = BaseRegex.GetStringJson(ref result, "msg"); if (string.IsNullOrEmpty(errMsg)) errMsg = result; return getString + "查询商品错误:" + errMsg; } List complateDbProdExistLst = JsonHelper.DeserializeJsonToList(JsonHelper.JObjectParse(result)["data"].ParseToString()); if (complateDbProdExistLst.Count == 0) { return getString + "商品不存在"; } url = BaseClass.BuildPDDObjString("product/add_prod_wx_complate"); Thread.Sleep(200); data = UserLogin.GetSignaturex(); data += "&secret_spu_id=" + wxProdInfoLite.product.info.spuCode; data += "&spu_id=" + complateDbProdExistLst[0].spu_id; data += "&coid=" + complateDbProdExistLst[0].skc_id; JsonSerializerSettings jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; data += "&wx_edit_prod_json=" + GetHtml.UrlEncodeUtf8(JsonConvert.SerializeObject(wxProdInfoLite, jsonSetting)); result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) { var errMsg = BaseRegex.GetStringJson(ref result, "msg"); if (string.IsNullOrEmpty(errMsg)) errMsg = result; return getString + "查询商品错误:" + errMsg; } else { return getString + "保存成功!"; } } catch (Exception ex) { return getString + "失败!" + ex.Message; } } string SaveWxProd(ShopLoginInfo shop, string prodId, WXGetEditProductInfoLite wxEditProdInfoLite) { bool isExist = false; var existUrl = BaseClass.BuildPDDObjString("product/get_product_spu_id_list"); string existData = UserLogin.GetSignaturex() + "&search_spu_ids=" + prodId; //if (!string.IsNullOrEmpty(prod.dbProduct.real_spu_id)) // existData = UserLogin.GetSignaturex() + "&search_spu_ids=" + prod.spuId + "," + prod.dbProduct.real_spu_id; string html = GetHtml.PostUtf8(existUrl, existData); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(existUrl, existData); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) { return "采集入库失败,产品数据库连接失败"; } else { if (html.Contains("\"data\":[") && !html.Contains("\"data\":[]")) isExist = true; } var url = BaseClass.BuildPDDObjString("product/add_product"); if (isExist) url = BaseClass.BuildPDDObjString("product/mdy_product"); var skus = wxEditProdInfoLite.product.productSkus; var data = Work.UserLogin.GetSignaturex() + "&from_shop_id=" + shop.ShopId + "&from_shop_name=" + shop.ShopAccount + "&spu_id=" + prodId + "&skc_id=" + prodId + "&price_text=" + skus.Min(s => s.productSkuInfo.salePrice * 0.01m) + " - " + skus.Max(s => s.productSkuInfo.salePrice * 0.01m) + "&ext_code=" + "" + "&product_name=" + HttpUtility.UrlEncode(wxEditProdInfoLite.product.info.title, Encoding.UTF8) + "&product_json=" + HttpUtility.UrlEncode(JsonHelper.SerializeObject(wxEditProdInfoLite), Encoding.UTF8) + "&size_json=" + HttpUtility.UrlEncode("{}", Encoding.UTF8) + "&sale_count=" + "&sale_count_text=" + "&main_img=" + HttpUtility.UrlEncode(wxEditProdInfoLite.product.info.headImg.FirstOrDefault(), Encoding.UTF8) + "&wx_safe_status=" + HttpUtility.UrlEncode("安全商品", Encoding.UTF8) + "&product_get_type=" + HttpUtility.UrlEncode("微信店采", Encoding.UTF8); if (wxEditProdInfoLite.product.info.category.Count > 0) { data += "&wx_cat_1=" + wxEditProdInfoLite.product.info.category[0].catId; data += "&main_cat_id=" + wxEditProdInfoLite.product.info.category[0].catId; data += "&main_cat_name=" + wxEditProdInfoLite.product.info.category[0].name; data += "&wx_cat_leaf=" + wxEditProdInfoLite.product.info.category.LastOrDefault()?.catId; data += "&cat_id=" + wxEditProdInfoLite.product.info.category.LastOrDefault()?.catId; data += "&cat_name=" + HttpUtility.UrlEncode((wxEditProdInfoLite.product.info.category.LastOrDefault()?.name).ParseToString(), Encoding.UTF8); } if (wxEditProdInfoLite.product.info.category.FirstOrDefault().name == "图书") { data += "&special_prod_type=" + HttpUtility.UrlEncode("书籍", Encoding.UTF8); } html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) { string msg = "商品入库失败!请检查网络连接:" + BaseRegex.GetStringJson(ref html, "msg"); //Invoke(new Action(() => //{ // row.Cells["colMsg"].Value = msg; //})); return msg; } else { //prod.getProductMsg = "商品入库成功!"; //Invoke(new Action(() => //{ // row.Cells["colMsg"].Value = prod.getProductMsg; //})); if (!isExist) { url = BaseClass.BuildPDDObjString("product/mdy_product"); html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) { string msg = "商品入库失败!请检查网络连接:" + BaseRegex.GetStringJson(ref html, "msg"); return msg; } } return "微信商品采集入库成功!"; } } } }