using KJD_Web.Model; using KJD_Web.Utils; using KJD_WebService.Model; using KJD_WebService.Model.WxModel; using NaiBa.Util.Extension; using NaiBa.Util.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace KJD_Web.Upload.Prod { public class BaseClass { public static string BuildPDDObjString(string file, bool IsLoginDef = false) { string LoginApiUrl = "http://1.13.195.131:9001"; string MainPath = "kuajingda"; return LoginApiUrl + "/bxx_" + file + "?obj=" + MainPath + "/bxx_" + file; } public static int TimeDifferent = 0; public static int VersionCount = 99999999; public static int CompanyId = 1; public static string CompanyName = "1"; public static string UserKey = "1"; public static string Mobile = "18888888888"; public static string UserName = "18888888888"; public static string ShopId = "18888888888"; public static string ShopName = "18888888888"; } public class UserLogin { /// /// 获取签名字符串,新版 /// /// public static string GetSignaturex() { return GetSignaturex(BaseClass.ShopId, BaseClass.ShopName); } public static string GetSignaturex(string shopId, string shopName) { if (string.IsNullOrEmpty(shopId)) shopId = "11111111"; if (string.IsNullOrEmpty(shopName)) shopName = "wx11111111"; int time = TimeUtils.DateTimeToTimestampSeconds(DateTime.Now) + BaseClass.TimeDifferent; string time1 = EncrypUtil.Encryption(time); return "time=" + time + "&code=" + time1 + "&NbCodeVersion=1" + "&version=" + BaseClass.VersionCount + "&company_id=" + BaseClass.CompanyId + "&company=" + HtmlUtils.UrlEncodeUtf8(BaseClass.CompanyName) + "&user_name=" + BaseClass.UserName + "&user_key=" + BaseClass.UserKey + "&mobile=" + BaseClass.Mobile + "&sid=" + shopId + "&shop=" + HtmlUtils.UrlEncodeUtf8(shopName); } } public partial class WXProd : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { FileUpload1.Visible = false; } protected void btnSubmit_Click(object sender, EventArgs e) { Label1.Text = ""; Label2.Text = ""; string rst = getProd(); Label1.Text = rst; Label2.Text = rst; TextBox2.Text = ""; FileUpload1.Visible = false; } string getProd() { string prodStr = Encoding.UTF8.GetString(this.FileUpload1.FileBytes); prodStr = TextBox2.Text.Trim(); WXClientProductInfoLite wxClientProd = new WXClientProductInfoLite(); var str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "product_key"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.product_key = JsonUtils.DeserializeJsonToObject(regex_str); if (wxClientProd.product_key == null) return "解析失败"; } catch { return "解析失败"; } //商品详情格式 wxClientProd.info = new wx_client_prod_info(); //主标题 wxClientProd.info.title = RegexUtils.GetStringJson(ref prodStr, "title"); if (wxClientProd.info.title.IsNullOrEmpty()) return "解析失败"; //副标题 wxClientProd.info.sub_title = RegexUtils.GetStringJson(ref prodStr, "sub_title"); str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "head_img"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.head_img = JsonUtils.DeserializeJsonToListNullable(regex_str); if (wxClientProd.info.head_img == null) return "解析失败"; } catch { return "解析失败"; } var nodeCatId = RegexUtils.GetStringJson(ref prodStr, "nodeCatId"); str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "category"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.category = JsonUtils.DeserializeJsonToListNullable(regex_str); if (wxClientProd.info.category != null && !str_lst.IsNullOrEmpty() && !wxClientProd.info.category.Any(c => c.catIdStr == nodeCatId)) { wxClientProd.info.category.Add(new wx_client_category() { catId = nodeCatId, cat_id = nodeCatId }); } //if (wxClientProd.info.category == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "detail"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.detail = JsonUtils.DeserializeJsonToObjectNullable(regex_str); if (wxClientProd.info.detail == null) { wxClientProd.info.detail = new wx_client_prod_detail(); wxClientProd.info.detail.detail_img.Add(wxClientProd.info.head_img.FirstOrDefault()); } //if (wxClientProd.info.detail == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "param"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.param = JsonUtils.DeserializeJsonToListNullable(regex_str); if (wxClientProd.info.param == null) wxClientProd.info.param = new List(); //if (wxClientProd.info.param == null) // return "解析失败"; } catch { return "解析失败"; } //品牌id wxClientProd.info.brand_id = RegexUtils.GetStringJson(ref prodStr, "brand_id"); //if (wxClientProd.info.brand_id.IsNullOrEmpty()) // return "解析失败"; str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "sku_id_list"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.sku_id_list = JsonUtils.DeserializeJsonToObjectNullable(regex_str); //if (wxClientProd.info.sku_id_list == null) // return "解析失败"; } catch { return ""; } str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "sku_list"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.sku_list = JsonUtils.DeserializeJsonToObjectNullable(regex_str); //if (wxClientProd.info.sku_list == null) // return "解析失败"; } catch { return "解析失败"; } if (wxClientProd.info.sku_list == null) { str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "sku_info"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); var sku_infos = JsonUtils.DeserializeJsonToListNullable(regex_str); if (sku_infos != null) { wxClientProd.info.sku_list = new wx_sku(); wxClientProd.info.sku_list.sku_info = sku_infos; } } catch { return "解析失败"; } } str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "product_sale_attrs"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.product_sale_attrs = JsonUtils.DeserializeJsonToListNullable(regex_str); //if (wxClientProd.info.product_sale_attrs == null) // return "解析失败"; } catch { return "解析失败"; } //总销量 wxClientProd.info.total_sold_num = RegexUtils.GetStringJson(ref prodStr, "total_sold_num"); //if (wxClientProd.info.total_sold_num.IsNullOrEmpty()) // return "解析失败"; str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "service_desc"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.service_desc = JsonUtils.DeserializeJsonToObjectNullable(regex_str); //if (wxClientProd.info.service_desc == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "express_info"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.express_info = JsonUtils.DeserializeJsonToObjectNullable(regex_str); //if (wxClientProd.info.express_info == null) // return; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "headVideoInfo"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.headVideoInfo = JsonUtils.DeserializeJsonToListNullable(regex_str); //if (wxClientProd.info.headVideoInfo == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonObjStrJson(ref prodStr, "afterSaleInfo"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.afterSaleInfo = JsonUtils.DeserializeJsonToObjectNullable(regex_str); //if (wxClientProd.info.afterSaleInfo == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "deliveryExpressFee"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.deliveryExpressFee = JsonUtils.DeserializeJsonToListNullable(regex_str); //if (wxClientProd.info.deliveryExpressFee == null) // return "解析失败"; } catch { return "解析失败"; } str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "templateInfo"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.info.templateInfo = JsonUtils.DeserializeJsonToListNullable(regex_str); //if (wxClientProd.info.templateInfo == null) // return "解析失败"; } catch { return "解析失败"; } wxClientProd.info.spuCode = RegexUtils.GetStringJson(ref prodStr, "spuCode"); str_lst = RegexUtils.GetJsonArrStrJson(ref prodStr, "productSkus"); try { var regex_str = str_lst.OrderByDescending(s => s.Length).FirstOrDefault(); wxClientProd.productSkus = JsonUtils.DeserializeJsonToList(regex_str); if (wxClientProd.productSkus == null) return "解析失败"; } catch { return "解析失败"; } return SaveWxProd(wxClientProd.product_key.product_id, wxClientProd, prodStr); } string SaveWxProd(string prodId, WXClientProductInfoLite wxClientProdInfoLite, string baseStr) { 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 = HtmlUtils.PostUtf8(existUrl, existData); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = HtmlUtils.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 = wxClientProdInfoLite.productSkus; var product_json = "{\"wxxdData\":" + JsonUtils.SerializeObject(wxClientProdInfoLite) + ",\"baseData\":" + JsonUtils.SerializeObject(baseStr) + "}"; var data = UserLogin.GetSignaturex() + "&from_shop_id=" + wxClientProdInfoLite.product_key.appid + "&from_shop_name=" + wxClientProdInfoLite.product_key.appid + "&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=" + wxClientProdInfoLite.info.spuCode + "&product_name=" + HttpUtility.UrlEncode(wxClientProdInfoLite.info.title, Encoding.UTF8) + "&product_json=" + HttpUtility.UrlEncode(product_json, Encoding.UTF8) + // product_json /*HttpUtility.UrlEncode(JsonUtils.SerializeObject(wxClientProdInfoLite), Encoding.UTF8)*/ "&sale_count=" + wxClientProdInfoLite.info.total_sold_num + "&sale_count_text=" + wxClientProdInfoLite.info.total_sold_num + "&main_img=" + HttpUtility.UrlEncode(wxClientProdInfoLite.info.head_img.FirstOrDefault(), Encoding.UTF8) + "&wx_safe_status=" + HttpUtility.UrlEncode("安全商品", Encoding.UTF8) + "&product_get_type=" + HttpUtility.UrlEncode("微信小店", Encoding.UTF8); if (wxClientProdInfoLite.info.category.Count > 0) { data += "&wx_cat_1=" + wxClientProdInfoLite.info.category[0].catIdStr; data += "&main_cat_id=" + wxClientProdInfoLite.info.category[0].catIdStr; data += "&main_cat_name=" + wxClientProdInfoLite.info.category[0].name; data += "&wx_cat_leaf=" + wxClientProdInfoLite.info.category.LastOrDefault()?.catIdStr; data += "&cat_id=" + wxClientProdInfoLite.info.category.LastOrDefault()?.catIdStr; data += "&cat_name=" + HttpUtility.UrlEncode((wxClientProdInfoLite.info.category.LastOrDefault()?.name).ParseToString(), Encoding.UTF8); } if (wxClientProdInfoLite.info.category.Count > 1) { data += "&wx_cat_2=" + wxClientProdInfoLite.info.category[1].catIdStr; } if (wxClientProdInfoLite.info.category.Count > 2) { data += "&wx_cat_3=" + wxClientProdInfoLite.info.category[2].catIdStr; } if (wxClientProdInfoLite.info.category.FirstOrDefault().name == "图书") { data += "&special_prod_type=" + HttpUtility.UrlEncode("书籍", Encoding.UTF8); } html = HtmlUtils.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = HtmlUtils.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) { string msg = "商品入库失败!请检查网络连接:" + RegexUtils.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 = HtmlUtils.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = HtmlUtils.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) { string msg = "商品入库失败!请检查网络连接:" + RegexUtils.GetStringJson(ref html, "msg"); return msg; } } return "微信商品采集入库成功!"; } } } }