做微信公众号查券首先需要一个1个产品库,我们可以使用淘宝的开放平台提供的产品库通用物料搜索API。
通过用户手淘分享的商品信息,信息中包含商品标题和链接还有淘口令,我们可以用正则表达式提取出商品标题
或者通过分享信息中的淘宝链接http抓包方式获取淘宝商品id; 然后通过id在查询的一批商品中找到指定的商品优惠券、佣金等信息。
关键代码如下
public static string GetTaobaoCoupon(RequestMessageText responseMessageText)
{
string itemInfo = responseMessageText.Content.Trim();
string responeMessage = "";
try
{
Match m_title = Regex.Match(itemInfo, @"【.*】");
string temp = m_title.Value;
if (!string.IsNullOrEmpty(temp))
{
temp = temp.Substring(1, temp.Length - 2);
}
else
{
return "";
}
if (temp.Contains("#手聚App"))
{
int IndexofA = temp.IndexOf("宝贝不错:");
int IndexofB = temp.IndexOf("(分享自");
temp = temp.Substring(IndexofA + 5, IndexofB - IndexofA - 5);
}
string title = temp;
//通过商品关键字查询商品
ITopClient client = new DefaultTopClient(taobaoApiUrl,taobaoAppkey, taobaoSecret);
TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
req.AdzoneId = addzoneId;
req.Platform = 2L;
req.PageSize = 100L;
req.Q = title;
req.PageNo = 1L;
TbkDgMaterialOptionalResponse rsp = client.Execute(req);
if (rsp.ResultList.Count > 0)
{
//获取淘宝短链接
Match m_url = Regex.Match(itemInfo, @"htt(p|ps):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?");
if (m_url.Value == "")
{
return responeMessage;
}
var s = HttpUtility.HttpGet(m_url.Value, "", "utf-8");
//Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')");
//获取宝贝item id
Match m_item = Regex.Match(s, @"(?<=m.taobao.com\/i)([0-9]*)");
string item_id = m_item.Value;
if (string.IsNullOrEmpty(item_id))
{
Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')");
var htmlContent = HttpUtility.HttpGet(am_url.Value, "", "gbk");
Match re_m_item = Regex.Match(htmlContent, @"(?<=taobao.com/item.htm\?id=)([0-9]*)");
item_id = re_m_item.Value;
}
if (string.IsNullOrEmpty(item_id))
{
//LogHelper.WriteLog(typeof(WechatController), "通过抓包方式未获取到宝贝item id");
var g = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(w => w.Volume).FirstOrDefault();
if (g == null)
{
responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");
}
else
{
var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;
responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";
}
return responeMessage;
}
else
{
float numid = 0;
try
{
numid = float.Parse(item_id);
}
catch (Exception ex)
{
//通过淘宝链接没有获取到item id,显示销量最高商品
var g = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault();
var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;
responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";
return responeMessage;
}
//在接口返回的商品中找查询的商品
foreach (var g in rsp.ResultList)
{
if (g.NumIid == numid)
{
if (string.IsNullOrEmpty(g.CouponInfo))
{
var hongbao = decimal.Parse(g.ZkFinalPrice) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;
responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【约返利】{Math.Round(hongbao, 2)}元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.Url, g.PictUrl + "_400x400.jpg")}\n==========================\n下单确认收货后就能收到返利佣金啦~\n 点击查看 <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何领取返利</a>";
return responeMessage;
}
else
{
var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;
responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";
return responeMessage;
}
}
}
//没有找到,有相似宝贝推荐
var w = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault();
if (w == null)
{
responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");
}
else
{
var hongbao = (decimal.Parse(w.ZkFinalPrice) - decimal.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(w.CommissionRate) / 10000 * commission_rate;
responeMessage = $"/:rose 亲,这款商品的优惠返利活动结束了~\n已为你推荐以下宝贝。\n==========================\n{w.Title}\n【在售价】{w.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(w.ZkFinalPrice) - double.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(w.CouponShareUrl, w.PictUrl + "_400x400.jpg")}\n";
}
return responeMessage;
}
}
else
{
responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");
}
}
catch (Exception ex)
{
//LogHelper.WriteLog(typeof(WechatController), "返回消息异常" + ex.Message);
}
return responeMessage;
}