472 lines
21 KiB
C#
472 lines
21 KiB
C#
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);
|
|
/// <summary>
|
|
/// 通知前端事件
|
|
/// </summary>
|
|
public event InfoReceivedHandler OnInfoReceived;
|
|
|
|
/// <summary>
|
|
/// 商品回采/采集
|
|
/// </summary>
|
|
/// <param name="sProdId"></param>
|
|
/// <param name="supplierId"></param>
|
|
/// <param name="isReGet"></param>
|
|
/// <returns></returns>
|
|
public string getProduct(string sProdId, string supplierId, bool isReGet = true, bool isCollectAuthorityUser = false)
|
|
{
|
|
if (BaseClass.SaLevel == 0 && !BaseClass.UserAuthority.Contains("微信回采") && !BaseClass.UserAuthority.Contains("商品采集"))
|
|
{
|
|
MainForm.instance.Invoke(new Action(() =>
|
|
{
|
|
BaseMsgForm.ShowError("用户没有采集权限!");
|
|
}));
|
|
return "failed";
|
|
}
|
|
if (BaseClass.SaLevel == 0 && !BaseClass.UserAuthority.Contains("微信回采") && isReGet == true)
|
|
{
|
|
MainForm.instance.Invoke(new Action(() =>
|
|
{
|
|
BaseMsgForm.ShowError("用户没有微信回采权限!");
|
|
}));
|
|
return "failed";
|
|
}
|
|
if (BaseClass.SaLevel == 0 && !BaseClass.UserAuthority.Contains("商品采集") && isCollectAuthorityUser == true)
|
|
{
|
|
MainForm.instance.Invoke(new Action(() =>
|
|
{
|
|
BaseMsgForm.ShowError("用户没有商品采集权限!");
|
|
}));
|
|
return "failed";
|
|
}
|
|
|
|
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(() =>
|
|
{
|
|
string msg = "";
|
|
if (isReGet) // 微信回采
|
|
msg = ReGetProductWithWXFormat(shop, sProdId);
|
|
else // 微信店铺商品采集
|
|
msg = GetWxProduct(shop, sProdId, isCollectAuthorityUser);
|
|
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";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商品详情回采
|
|
/// </summary>
|
|
/// <param name="shop"></param>
|
|
/// <param name="prodId"></param>
|
|
/// <returns></returns>
|
|
public string ReGetProductWithWXFormat(ShopLoginInfo shop, string prodId)
|
|
{
|
|
//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 "商品回采失败!" + errmsg;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//OnInfoReceived("商品回采失败!商品数据格式获取失败!" + ex.Message);
|
|
return "商品回采失败!商品数据格式获取失败!" + ex.Message;
|
|
}
|
|
|
|
WXGetEditProductInfoLite wxProdInfoLite = null;
|
|
try
|
|
{
|
|
wxProdInfoLite = JsonHelper.DeserializeJsonToObject<WXGetEditProductInfoLite>(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 "商品回采失败!" + errmsg;
|
|
}
|
|
var addressStr = JsonHelper.JObjectParse(addressRst)["addressList"].FirstOrDefault().ParseToString();
|
|
wxProdInfoLite.address = JsonHelper.DeserializeJsonToObject<WXAfterSaleAddressInfo>(addressStr);
|
|
if (wxProdInfoLite.address == null)
|
|
return "商品回采失败!商品详情获取失败";
|
|
// 查看下json结构
|
|
var wxProdInfoLiteStr = JsonHelper.SerializeObject(wxProdInfoLite);
|
|
if (string.IsNullOrWhiteSpace(wxProdInfoLite.product.info.spuCode))
|
|
{
|
|
return "商品回采失败:未找到外部编码,采集失败,跳过";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "商品回采失败:格式转换失败 " + 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 "商品回采查询商品错误:" + errMsg;
|
|
}
|
|
List<KJDDBProduct> complateDbProdExistLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.JObjectParse(result)["data"].ParseToString());
|
|
if (complateDbProdExistLst.Count == 0)
|
|
{
|
|
return "回采商品不存在";
|
|
}
|
|
|
|
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 "商品回采查询商品错误:" + errMsg;
|
|
}
|
|
else
|
|
{
|
|
return "商品回采保存成功!";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "商品回采失败!" + ex.Message;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 店铺商品采集
|
|
/// </summary>
|
|
/// <param name="shop"></param>
|
|
/// <param name="prodId"></param>
|
|
/// <param name="isCollectAuthorityUser"></param>
|
|
/// <returns></returns>
|
|
public string GetWxProduct(ShopLoginInfo shop, string prodId, bool isCollectAuthorityUser = false)
|
|
{
|
|
WXGetEditProductInfoLite wxProdInfoLite = null;
|
|
return GetWxProduct(shop, prodId, ref wxProdInfoLite, false, isCollectAuthorityUser);
|
|
}
|
|
|
|
public string GetWxProduct(ShopLoginInfo shop, string prodId, ref WXGetEditProductInfoLite wxProdInfoLite, bool isNotSaveDB = false, bool isCollectAuthorityUser = false)
|
|
{
|
|
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 "店铺商品采集失败!" + errmsg;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//OnInfoReceived("商品获取失败!商品数据格式获取失败!" + ex.Message);
|
|
return "店铺商品采集失败!商品数据格式获取失败!" + ex.Message;
|
|
}
|
|
|
|
try
|
|
{
|
|
wxProdInfoLite = JsonHelper.DeserializeJsonToObject<WXGetEditProductInfoLite>(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 "店铺商品采集失败!" + errmsg;
|
|
}
|
|
var addressStr = JsonHelper.JObjectParse(addressRst)["addressList"]?.FirstOrDefault().ParseToString();
|
|
if (!string.IsNullOrWhiteSpace(addressStr))
|
|
wxProdInfoLite.address = JsonHelper.DeserializeJsonToObject<WXAfterSaleAddressInfo>(addressStr);
|
|
//if (wxProdInfoLite.address == null)
|
|
// return "店铺商品采集失败!商品详情获取失败";
|
|
// 查看下json结构
|
|
var wxProdInfoLiteStr = JsonHelper.SerializeObject(wxProdInfoLite);
|
|
|
|
if (isNotSaveDB)
|
|
return "success";
|
|
else
|
|
return SaveWxProd(shop, prodId, wxProdInfoLite, isCollectAuthorityUser);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "店铺商品采集失败:格式转换失败 " + ex.Message;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "失败!" + ex.Message;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 店采商品保存
|
|
/// </summary>
|
|
/// <param name="shop"></param>
|
|
/// <param name="prodId"></param>
|
|
/// <param name="wxEditProdInfoLite"></param>
|
|
/// <param name="specialProdType"></param>
|
|
/// <param name="coid"></param>
|
|
/// <param name="realCoid"></param>
|
|
/// <returns></returns>
|
|
public string SaveWxProd(ShopLoginInfo shop, string prodId, WXGetEditProductInfoLite wxEditProdInfoLite, bool isCollectAuthorityUser = false,
|
|
string specialProdType = null, string coid = null, string realCoid = null, int? viewUv = null, string wx_safe_status = "安全商品")
|
|
{
|
|
// 商品采集权限用户采集的数据加上用户前缀(商品前缀显示给用户的时候隐藏)
|
|
string collectAuthorityTag = "";
|
|
if (isCollectAuthorityUser)
|
|
collectAuthorityTag = "-" + BaseClass.UserName;
|
|
|
|
bool isExist = false;
|
|
var existUrl = BaseClass.BuildPDDObjString("product/get_product_spu_id_list");
|
|
string existData = UserLogin.GetSignaturex() + "&search_spu_ids=" + prodId + collectAuthorityTag;
|
|
//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 + collectAuthorityTag;
|
|
if (specialProdType == "爆款数据")
|
|
data += "&add_hot_version=v3";
|
|
if (!string.IsNullOrWhiteSpace(coid))
|
|
data += "&skc_id=" + coid;
|
|
else
|
|
data += "&skc_id=" + prodId;
|
|
if (!string.IsNullOrWhiteSpace(realCoid))
|
|
{
|
|
data += "&real_coid=" + realCoid;
|
|
}
|
|
data += "&price_text=" + skus.Min(s => s.productSkuInfo.salePrice * 0.01m) + " - " + skus.Max(s => s.productSkuInfo.salePrice * 0.01m);
|
|
data += "&ext_code=" + wxEditProdInfoLite.product.info.spuCode;
|
|
data += "&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);
|
|
|
|
var getListProdRst = WXUpload.searchProductWithSpuId(shop, prodId).ParseToString();
|
|
var totalSoldNum = BaseRegex.GetStringJson(ref getListProdRst, "totalSoldNum");
|
|
|
|
data += "&sale_count=" + totalSoldNum +
|
|
"&sale_count_text=" + totalSoldNum +
|
|
"&main_img=" + HttpUtility.UrlEncode(wxEditProdInfoLite.product.info.headImg.FirstOrDefault(), Encoding.UTF8) +
|
|
"&wx_safe_status=" + HttpUtility.UrlEncode(wx_safe_status, Encoding.UTF8) +
|
|
"&product_get_type=" + HttpUtility.UrlEncode("微信店采", Encoding.UTF8);
|
|
if (isCollectAuthorityUser)
|
|
{
|
|
data += "&add_user_name=" + BaseClass.UserName;
|
|
data += "&group=用户" + BaseClass.UserName;
|
|
}
|
|
|
|
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.Count > 1)
|
|
{
|
|
data += "&wx_cat_2=" + wxEditProdInfoLite.product.info.category[1].catId;
|
|
}
|
|
if (wxEditProdInfoLite.product.info.category.Count > 2)
|
|
{
|
|
data += "&wx_cat_3=" + wxEditProdInfoLite.product.info.category[2].catId;
|
|
}
|
|
if (specialProdType != null)
|
|
{
|
|
data += "&special_prod_type=" + HttpUtility.UrlEncode("爆款数据", Encoding.UTF8);
|
|
}
|
|
if (viewUv != null)
|
|
{
|
|
data += "&view_uv=" + viewUv;
|
|
}
|
|
data += "&send_to_out_server=127";
|
|
data += "&distribution_set=127";
|
|
|
|
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 "微信商品采集入库成功!";
|
|
}
|
|
}
|
|
}
|
|
}
|