从店铺商品列表获取商品曝光量

This commit is contained in:
wule committed 2026-06-05 18:45:29 +08:00
1 parent 58c26725fb
commit 4ee91e2625
10 files changed
+588 -184

No files matched your search

+415 -26
View File
@@ -1,8 +1,10 @@
using AllBase;
using AllBase.Helper;
using AllBase.Model.Param.WXParam;
using AllBase.Model.Result;
using AllBase.Model.Result.WXResult;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
@@ -22,6 +24,288 @@ namespace TemuKJD.BLL.Wx
public static bool isGetOnce { get; set; } = true;
public void GetPlatformRecommendProduct()
{
//Task.Run(() =>
//{
// if (!Directory.Exists("wxPlatformRecommendUpdate"))
// Directory.CreateDirectory("wxPlatformRecommendUpdate");
// while (true)
// {
// int cnt = 0;
// foreach (var shop in GlobalConfig.ShopLoginList)
// {
// if (shop.LoginStatus != "登录成功")
// continue;
// if (shop.ShopId == "wx8fd533b89195d717")
// {
// }
// Thread.Sleep(Randomx.GetId(500, 1500));
// WXPlatformRecommendUpdateInfo info = new WXPlatformRecommendUpdateInfo() { shopId = shop.ShopId, updateDay = DateTime.Now.ToString("yyMMdd") };
// try
// {
// info = NaiBa.Util.Helper.ConfigHelper.LoadConfig<WXPlatformRecommendUpdateInfo>("wxPlatformRecommendUpdate\\" + shop.ShopId);
// if (info == null)
// info = new WXPlatformRecommendUpdateInfo() { shopId = shop.ShopId, updateDay = DateTime.MinValue.ToString("yyMMdd") };
// if (info.updateDay == DateTime.Now.ToString("yyMMdd") && isGetOnce)
// {
// // 已查询过,跳过,暂时关闭改为每次打开都查询
// continue;
// }
// info.failReason = "";
// long endMs = BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now.Date) - 1;
// long startMs = BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now.Date.AddDays(-30));
// string result = "";
// for (int i = 0; i < 100; i++)
// {
// result = getPlatformRecommend(shop, startMs, endMs, i + 1);
// if (!result.Contains("\"code\":0"))
// {
// WXForms.ShopForms.WXShopListForm.CheckShopLogin(shop);
// info.failReason = "读取失败,推荐商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// break;
// }
// Thread.Sleep(Randomx.GetId(500, 1500));
// try
// {
// var jArr = JsonHelper.JObjectParse(result)["data"]["data"]["list"];
// List<WXPlatformRecommendProdInfo> recommendProds = JsonHelper.DeserializeJsonToList<WXPlatformRecommendProdInfo>(JsonHelper.SerializeObject(jArr));
// foreach (var recommendProd in recommendProds)
// {
// if (recommendProd.product_view_uv.ParseToInt() < minViewUv)
// {
// i = 999999;
// break;
// }
// cnt++;
// if (cnt % 20 == 0)
// {
// var url0 = BaseClass.BuildObjString("product/is_get_wx_recommend_hot_prod");
// var data0 = "";
// data0 = UserLogin.GetSignaturex();
// var html0 = GetHtml.PostUtf8(url0, data0);
// if (!html0.Contains("\"code\":200"))
// html0 = GetHtml.PostUtf8(url0, data0);
// if (html0.Contains("\"is_get\":\"true\""))
// {
// minViewUv = Math.Max(100, BaseRegex.GetIntJson(ref html0, "minViewUv"));
// getInterval = Math.Max(3, BaseRegex.GetIntJson(ref html0, "getViewUvInterval"));
// }
// else
// {
// break;
// }
// }
// Thread.Sleep(getInterval * 1000);
// #region 查询爆款数据是否已存在
// var url = BaseClass.BuildPDDObjString("product/get_product_list");
// var data = "";
// data = UserLogin.GetSignaturex() +
// "&search_prod_name_in=" + GetHtml.UrlEncodeUtf8(recommendProd.title) + //"&wx_safe_status=" + "安全商品,未校验" +
// "&special_prod_type=爆款数据" +
// "&is_not_count=true";
// var html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// var prodObjs = JsonHelper.JObjectParse(html)["data"];
// List<KJDDBProduct> dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// var dbProdHotLst = dbProdLst.Where(p => p.special_prod_type == "爆款数据").ToList();
// var existHotProd = dbProdHotLst.FirstOrDefault();
// if (existHotProd != null) // 爆款数据商品已存在,更新下曝光量view_uv后继续
// {
// if(existHotProd.view_uv.ParseToInt() < recommendProd.product_view_uv.ParseToInt())
// {
// url = BaseClass.BuildObjString("product/mdy_best_sale_prod");
// data = UserLogin.GetSignaturex() +
// "&spu_id=" + existHotProd.spu_id +
// "&view_uv=" + recommendProd.product_view_uv;
// html = GetHtml.PostUtf8(url, data);
// }
// continue;
// }
// #endregion
// WXGetEditProductInfoLite wxProdInfoLite = null;
// var rst1 = new WXShopBrowserObject().GetWxProduct(shop, recommendProd.product_id, ref wxProdInfoLite, true);
// if (rst1 != "success")
// {
// info.failReason = "读取失败,微信商品详情查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// string spuCode = wxProdInfoLite.product.info.spuCode.ParseToString(); // 2042905570314301442
// #region 查询原始数据
// dbProdLst.Clear();
// if (spuCode.Length == 19)
// {
// url = BaseClass.BuildPDDObjString("product/get_product_list");
// data = UserLogin.GetSignaturex() +
// "&search_secret_spu_ids=" + spuCode +
// "&special_prod_type=普通商品,书籍" +
// "&is_not_count=true";
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// prodObjs = JsonHelper.JObjectParse(html)["data"];
// dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// }
// if (dbProdLst.Count == 0 && spuCode.StartsWith("EC1") && spuCode.EndsWith("A"))
// {
// spuCode = spuCode.Substring(3, spuCode.Length - 4);
// url = BaseClass.BuildPDDObjString("product/get_product_list");
// data = UserLogin.GetSignaturex() +
// "&search_spu=" + spuCode +
// "&special_prod_type=普通商品,书籍" +
// "&is_not_count=true";
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// prodObjs = JsonHelper.JObjectParse(html)["data"];
// dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// }
// if (dbProdLst.Count == 0 && spuCode.Length > 10)
// {
// url = BaseClass.BuildPDDObjString("product/get_product_list");
// data = UserLogin.GetSignaturex() +
// "&ext_code_in=" + spuCode +
// "&special_prod_type=普通商品,书籍" +
// "&is_not_count=true";
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// prodObjs = JsonHelper.JObjectParse(html)["data"];
// dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// }
// if (dbProdLst.Count == 0)
// {
// url = BaseClass.BuildPDDObjString("product/get_product_list");
// data = UserLogin.GetSignaturex() +
// "&search_prod_name_in=" + GetHtml.UrlEncodeUtf8(recommendProd.title) +
// "&special_prod_type=普通商品,书籍" +
// "&is_not_count=true";
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// prodObjs = JsonHelper.JObjectParse(html)["data"];
// dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// }
// if (dbProdLst.Count == 0 && spuCode.Length > 10)
// {
// url = BaseClass.BuildPDDObjString("product/get_product_list");
// data = UserLogin.GetSignaturex() +
// "&ext_code_in=" + GetHtml.UrlEncodeUtf8(recommendProd.title) +
// "&special_prod_type=普通商品,书籍" +
// "&is_not_count=true";
// html = GetHtml.PostUtf8(url, data);
// if (!html.Contains("\"code\":200"))
// {
// info.failReason = "读取失败,库存商品查询失败";
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// prodObjs = JsonHelper.JObjectParse(html)["data"];
// dbProdLst = JsonHelper.DeserializeJsonToList<KJDDBProduct>(JsonHelper.SerializeObject(prodObjs));
// }
// #endregion
// var dbProdNormalLst = dbProdLst.Where(p => p.special_prod_type != "爆款数据").ToList();
// var normalJstProd = dbProdNormalLst
// .FirstOrDefault(p => p.product_get_type_text == "厂商直采" && p.secret_spu_id == wxProdInfoLite.product.info.spuCode);
// string prodId = recommendProd.product_id;
// string coid = normalJstProd?.skc_id;
// string real_coid = string.IsNullOrEmpty(normalJstProd?.real_coid) ? coid : normalJstProd?.real_coid;
// string wx_safe_status = !dbProdNormalLst.Any(p => p.wx_safe_status == "违规商品") ? "安全商品" : "违规商品";
// int? view_uv = recommendProd.product_view_uv.IsNullOrEmpty() ? (int?)null : recommendProd.product_view_uv.ParseToInt();
// var rst2 = new WXShopBrowserObject()
// .SaveWxProd(shop, prodId, wxProdInfoLite, "爆款数据", coid, real_coid, view_uv, wx_safe_status);
// if (!rst2.Contains("微信商品采集入库成功"))
// {
// info.failReason = "查询失败," + rst2;
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// }
// }
// catch (Exception ex)
// {
// info.failReason = "读取失败" + ex.Message;
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// continue;
// }
// var total = BaseRegex.GetIntJson(ref result, "list_total");
// var totalPage = Math.Ceiling(1.0m * total / 10).ParseToInt();
// if (totalPage <= i + 1)
// {
// break;
// }
// }
// if (!result.Contains("\"code\":0"))
// continue;
// if (string.IsNullOrEmpty(info.failReason))
// info.updateDay = DateTime.Now.ToString("yyMMdd");
// NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
// }
// catch (Exception ex)
// {
// }
// }
// // 不循环获取
// break;
// }
//});
}
/// <summary>
/// 从商品列表读取曝光量
/// </summary>
public void GetPlatformRecommendProduct260605()
{
Task.Run(() =>
@@ -37,11 +321,6 @@ namespace TemuKJD.BLL.Wx
if (shop.LoginStatus != "登录成功")
continue;
if (shop.ShopId == "wx8fd533b89195d717")
{
}
Thread.Sleep(Randomx.GetId(500, 1500));
WXPlatformRecommendUpdateInfo info = new WXPlatformRecommendUpdateInfo() { shopId = shop.ShopId, updateDay = DateTime.Now.ToString("yyMMdd") };
@@ -59,17 +338,19 @@ namespace TemuKJD.BLL.Wx
info.failReason = "";
long endMs = BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now.Date) - 1;
long startMs = BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now.Date.AddDays(-30));
string result = "";
for (int i = 0; i < 100; i++)
string getProdRst = "";
for (int i = 0; i < 10000; i++)
{
result = getPlatformRecommend(shop, startMs, endMs, i + 1);
if (!result.Contains("\"code\":0"))
// 先查询商品
getProdRst = searchProductWithSaleStatus(shop, i + 1);
if (!getProdRst.Contains("\"code\":0") && !getProdRst.Contains("\"success\":true"))
{
WXForms.ShopForms.WXShopListForm.CheckShopLogin(shop);
WXShopListForm.CheckShopLogin(shop);
info.failReason = "读取失败,商品查询失败";
if (shop.LoginStatus != "登录成功")
info.failReason = "读取失败,店铺掉线";
info.failReason = "读取失败,推荐商品查询失败";
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
break;
}
@@ -77,17 +358,57 @@ namespace TemuKJD.BLL.Wx
try
{
var jArr = JsonHelper.JObjectParse(result)["data"]["data"]["list"];
List<WXPlatformRecommendProdInfo> recommendProds = JsonHelper.DeserializeJsonToList<WXPlatformRecommendProdInfo>(JsonHelper.SerializeObject(jArr));
var wxProdObjList = JsonHelper.JObjectParse(getProdRst)["productList"];
//var prodLst = JsonHelper.DeserializeJsonToList<WXUploadProductParam>(JsonHelper.SerializeObject(wxProdObjList));
var prodIdLst = wxProdObjList.Select(p => p["productId"].ParseToString()).ToList();
if (prodIdLst.Count == 0)
{
// 取到商品页,直接跳出
i = 999999;
break;
}
var prodMetricRst = getProductMetricList(shop, prodIdLst, i + 1);
if (!prodMetricRst.Contains("\"code\":0"))
{
WXShopListForm.CheckShopLogin(shop);
info.failReason = "读取失败,商品查询失败";
if (shop.LoginStatus != "登录成功")
info.failReason = "读取失败,店铺掉线";
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wxPlatformRecommendUpdate\\" + shop.ShopId, info);
break;
}
Thread.Sleep(Randomx.GetId(500, 1500));
var productMetric = JsonHelper.DeserializeJsonToObject<ProductMetric>(JsonHelper.JObjectParse(prodMetricRst)["data"].ToString());
if (productMetric.rowsMap == null || productMetric.rowsMap.Count == 0)
{
// 取到无流量页,直接跳出
i = 999999;
break;
}
// 将商品信息和曝光信息融合到一起
List<WXPlatformRecommendProdInfo> recommendProds = new List<WXPlatformRecommendProdInfo>();
foreach (var wxProdObj in wxProdObjList)
{
WXPlatformRecommendProdInfo recommendProd = new WXPlatformRecommendProdInfo();
var prodStr = JsonHelper.SerializeObject(wxProdObj);
var productMetricStr = JsonHelper.SerializeObject(productMetric);
recommendProd.product_id = wxProdObj["productId"].ParseToString();
recommendProd.title = wxProdObj["title"].ParseToString();
var ViewUvCnt = productMetric.rowsMap
.Where(r => r.value?.spu_id_ == recommendProd.product_id)
.Sum(r => r.value?.shopxspu_growth_product_view_cnt_30d_2.ParseToInt());
recommendProd.product_view_uv = ViewUvCnt.ParseToString();
if (ViewUvCnt > 0)
recommendProds.Add(recommendProd);
}
// 计算曝光,大于指定数量的入库
var recommendProdStr = JsonHelper.SerializeObject(recommendProds);
foreach (var recommendProd in recommendProds)
{
if (recommendProd.product_view_uv.ParseToInt() < minViewUv)
{
i = 999999;
break;
}
cnt++;
if (cnt % 20 == 0)
{
var url0 = BaseClass.BuildObjString("product/is_get_wx_recommend_hot_prod");
@@ -107,6 +428,11 @@ namespace TemuKJD.BLL.Wx
}
}
if (recommendProd.product_view_uv.ParseToInt() < minViewUv)
{
continue;
}
cnt++;
Thread.Sleep(getInterval * 1000);
#region 查询爆款数据是否已存在
@@ -131,7 +457,7 @@ namespace TemuKJD.BLL.Wx
var existHotProd = dbProdHotLst.FirstOrDefault();
if (existHotProd != null) // 爆款数据商品已存在,更新下曝光量view_uv后继续
{
if(existHotProd.view_uv.ParseToInt() < recommendProd.product_view_uv.ParseToInt())
if (existHotProd.view_uv.ParseToInt() < recommendProd.product_view_uv.ParseToInt())
{
url = BaseClass.BuildObjString("product/mdy_best_sale_prod");
data = UserLogin.GetSignaturex() +
@@ -274,14 +600,16 @@ namespace TemuKJD.BLL.Wx
continue;
}
var total = BaseRegex.GetIntJson(ref result, "list_total");
var total = BaseRegex.GetIntJson(ref getProdRst, "list_total");
if (total.IsNullOrZero())
total = BaseRegex.GetIntJson(ref getProdRst, "totalNum");
var totalPage = Math.Ceiling(1.0m * total / 10).ParseToInt();
if (totalPage <= i + 1)
{
break;
}
}
if (!result.Contains("\"code\":0"))
if (!getProdRst.Contains("\"code\":0") && !getProdRst.Contains("\"success\":true"))
continue;
if (string.IsNullOrEmpty(info.failReason))
@@ -296,7 +624,7 @@ namespace TemuKJD.BLL.Wx
// 不循环获取
break;
}
}
});
}
@@ -312,6 +640,37 @@ namespace TemuKJD.BLL.Wx
var result = WXHttpResponseBLL.WeiXinPostResponse(shop, url, data, referer);
return result;
}
public static string searchProductWithSaleStatus(ShopLoginInfo shop, int pageNum = 1, int pageSize = 50)
{
// 在售
string url = "https://store.weixin.qq.com/shop-faas/mmchannelstradeproductcore/cgi/goods/searchProduct?token=&lang=zh_CN&pageSize=" + pageSize +
"&productStatus[]=-1&productSource=[1,16,32]&searchSource=1&useNew=true&fromProductManager=1&pageNum=" + pageNum + "&status[]=5&sortType=6";
// 全部商品
//string url = "https://store.weixin.qq.com/shop-faas/mmchannelstradeproductcore/cgi/goods/searchProduct?token=&lang=zh_CN&pageSize=" + pageSize +
// "&productStatus[]=-1&productSource=[1,16,32]&searchSource=1&useNew=true&fromProductManager=1&pageNum=" + pageNum + "&status[]=-1&sortType=6";
string refere = "https://store.weixin.qq.com/shop/goods/list?currentTab=PRODUCT_STATUS_ON&currentPage=" + pageNum;
var result = WXHttpResponseBLL.WeiXinGetResponse(shop, url, refere);
return result;
}
public static string getProductMetricList(ShopLoginInfo shop, List<string> spuIdLst, int pageNum = 1, int pageSize = 50)
{
string url = "https://store.weixin.qq.com/shop-faas/mmecnodeproductlist/cgi/goods/getProductMetricList?token=&lang=zh_CN";
string refere = "https://store.weixin.qq.com/shop/goods/list?currentTab=PRODUCT_STATUS_ON&currentPage=" + pageNum;
string body = @"{""spuIds"":[""10000941685997"",""10000941606843"",""10000941606678"",""10000941814176"",""10000941730758"",""10000941794530"",""10000941656198"",""10000941838792"",""10000941813664"",""10000941507483"",""10000941644842"",""10000941511634"",""10000941566070"",""10000941773596"",""10000941605208"",""10000941768333"",""10000941442087"",""10000941595429"",""10000941600162"",""10000941382412"",""10000941506207"",""10000941599882"",""10000941743120"",""10000941708791"",""10000941480748"",""10000941505847"",""10000941377556"",""10000941327242"",""10000941519895"",""10000941574393"",""10000941708024"",""10000941543916"",""10000941445436"",""10000941287155"",""10000941297349"",""10000941543530"",""10000941603084"",""10000941360832"",""10000941484113"",""10000941137242"",""10000941468968"",""10000941271777"",""10000941563120"",""10000941528202"",""10000941226651"",""10000941136287"",""10000940942388"",""10000941389756"",""10000941261318"",""10000940962314""],""limit"":300,""offset"":0,""sortMetricCode"":""shopxspu_growth_pay_product_cnt_30d_2"",""sortRule"":""desc"",""sceneType"":""0""}";
var data = new Dictionary<string, object>();
data["spuIds"] = spuIdLst;
data["limit"] = pageSize * 6;
data["offset"] = 0;
data["sortMetricCode"] = "shopxspu_growth_pay_product_cnt_30d_2";
data["sortRule"] = "desc";
data["sceneType"] = "0";
body = JsonHelper.SerializeObject(data);
var result = WXHttpResponseBLL.WeiXinPostResponse(shop, url, body, refere);
return result;
}
}
public class WXPlatformRecommendUpdateInfo
@@ -355,4 +714,34 @@ namespace TemuKJD.BLL.Wx
public string product_click_cnt_hb { get; set; } // ""
}
public class ProductMetric
{
public List<MetricSchema> schema { get; set; }
public List<MetricRowsMap> rowsMap { get; set; }
}
public class MetricRowsMap
{
public MetricRowsMapValue value { get; set; }
}
public class MetricRowsMapValue
{
public string shopxspu_growth_product_view_cnt_30d_2 { get; set; } // "167", "(店铺x商品)商品成长-曝光次数(近30天)-新"
public string shopxspu_growth_pay_product_cnt_30d_2 { get; set; } // "0",
public string shopxspu_growth_rec_product_view_cnt_30d_2 { get; set; } // "167",
public string scene_type_ { get; set; } // "10100",
public string spu_id_ { get; set; } // "10000697373414"
}
public class MetricSchema
{
public string key { get; set; } // "shopxspu_growth_product_view_cnt_30d_2", "shopxspu_growth_rec_product_view_cnt_30d_2", "shopxspu_growth_pay_product_cnt_30d_2"
public string fieldDbType { get; set; } // "int64",
public string name { get; set; } // "(店铺x商品)商品成长-曝光次数(近30天)-新", "(店铺x商品)商品成长-推荐曝光次数(近30天)-新", "(店铺x商品)商品成长-成交件数(近30天)-新"
public string logicType { get; set; } // "METRIC",
public string dataType { get; set; } // "BIGINT"
}
}