公眾號搭建淘寶、京東、拼多多查券平臺
阿新 • • 發佈:2018-11-14
雙十一之前終於把淘寶客公眾號搭建好了,雖然耗時不少,但還是蠻有成就感的,能不能賺錢就另外一回事了。
我這裡用到了淘寶聯盟api許可權、拼多多平臺介面api、京東申請比較麻煩就沒有申請,所以京東使用了第三方免費介面,各聯盟賬號api許可權申請我就不贅述了,有疑問的話可以在下面留言,或加我微信公眾號“網購有券”聯絡我。
淘寶找券實現
原理:
- 通過使用者手淘分享的商品資訊,資訊中包含商品關鍵字和連結,使用商品關鍵字通過淘寶 通用物料搜尋API 介面查詢一批商品;
- 然後通過分享資訊中的淘寶連結http抓包方式獲取淘寶商品item id;
- 然後通過item 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; }
京東、拼多多找券
京東、拼多多找券都比較簡單,微信支援京東、拼多多連結,可以通過連結獲取到商品id,然後直接通過介面使用商品id找券。
京東程式碼:使用了第三方介面(好京客)
public static string GetJDCoupon(RequestMessageText requestMessageText) { string msg = requestMessageText.Content; Match m_goods = Regex.Match(msg, @"(?<=product\/)([0-9]*)|(?<=sku=)([0-9]*)"); string skuId = m_goods.Value; if (string.IsNullOrEmpty(skuId)) { // LogHelper.WriteLog(typeof(WechatController), "獲取京東skuid失敗" + msg); return ""; } try { var hjkGoodsDetail = hjkApi.GetJDGoodsDetail(skuId); if (hjkGoodsDetail.StatusCode == 200 && hjkGoodsDetail.Data != null && !string.IsNullOrEmpty(hjkGoodsDetail.Data.CouponList)) { var model = hjkApi.GetUnionUrl(skuId, hjkGoodsDetail.Data.CouponList); if (model != null && model.StatusCode == 200 && !string.IsNullOrEmpty(model.Data)) { return $"{hjkGoodsDetail.Data.SkuName}\n【在售價】{hjkGoodsDetail.Data.WlPrice}元\n【巻後價】{hjkGoodsDetail.Data.WlPriceAfter} 元\n\n\ue231 <a href='{model.Data}'>點選這裡領券下單</a>\n\n"; } else { return ConfigurationManager.AppSettings["jd_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } } else { return ConfigurationManager.AppSettings["jd_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "獲取京東skuid失敗" + ex.Message); return ""; } }
拼多多程式碼如下:
public static async Task<string> GetPDDCouponAsync(RequestMessageText responseMessageText)
{
PddCommonApi.ClientId = "d8172a66ddf14220beac58e8eddca0d9";
PddCommonApi.ClientSecret = "d5993ce16cc0d1bd35403176f35544962e786645";
PddCommonApi.RedirectUri = "RedirectUri";
PddCommonApi.AccessToken = "";
string msg = responseMessageText.Content;
Match m_goods = Regex.Match(msg, @"(?<=goods_id=)([0-9]*)");
string goods_id = m_goods.Value;
if (string.IsNullOrEmpty(goods_id))
{
//LogHelper.WriteLog(typeof(WechatController), "獲取拼多多goods id失敗" + msg);
return "";
}
DdkApi api = new DdkApi();
var model = new DetailDdkGoodsRequestModel()
{
Type = "pdd.ddk.goods.detail",
GoodsIdList = $"[{goods_id}]"
};
DetailDdkGoodsResponseModel result = null;
try
{
result = await api.DetailDdkGoodsAsync(model);
}
catch (Exception ex)
{
//LogHelper.WriteLog(typeof(WechatController), "呼叫拼多多獲取商品詳細資訊失敗" + ex.Message);
return "";
}
var goods = result.GoodsDetailResponse.GoodsDetails.FirstOrDefault();
if (goods == null) //無優惠券 無佣金
{
return "/:rose 親,這款商品的優惠返利活動結束了~\n請換個商品試試吧。\n========================\n\ue231 <a href='https://mobile.yangkeduo.com/duo_cms_mall.html?pid=2495191_31302208cpsSign=CM2495191_31302208_3a1c1a0431608b9c1eb417183d57c1bdduoduo_type=2'>拼多多優惠券商城</a>\n下單確認收貨後就能收到返利佣金啦~";
}
else if (goods.HasCoupon) //有優惠券 有佣金
{
try
{
var promotionUrlModel = await api.GenerateDdkGoodsPromotionUrlAsync(new GenerateDdkGoodsPromotionUrlRequestModel
{
Type = "pdd.ddk.goods.promotion.url.generate",
PId = pdd_pid,
GoodsIdList = $"[{goods_id}]",
GenerateShortUrl = true,
CustomParameters = responseMessageText.FromUserName
});
return $"/:rose 親,商品資訊如下~\n========================\n{goods.GoodsName}\n【在售價】{((decimal)goods.MinGroupPrice) / 100}元\n【券後價】{Math.Round(((decimal)(goods.MinGroupPrice - goods.CouponDiscount.Value)) / 100, 2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>點選這裡下單</a>\n下單確認收貨後就能收到返利佣金啦~";
//return $"/:rose 親,商品資訊如下~\n========================\n{goods.GoodsName}\n【在售價】{((decimal)goods.MinGroupPrice) / 100}元\n【券後價】{Math.Round(((decimal)(goods.MinGroupPrice - goods.CouponDiscount.Value)) / 100,2)}元\n【約返利】{Math.Round((decimal)((goods.MinNormalPrice - goods.CouponDiscount.Value) * goods.PromotionRate) / 100000,2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>點選這裡下單</a>\n下單確認收貨後就能收到返利佣金啦~";
}
catch (Exception ex)
{
//LogHelper.WriteLog(typeof(WechatController), "呼叫拼多多獲取推廣連結失敗" + ex.Message);
return "";
}
}
else //無優惠券 但有佣金
{
try
{
var promotionUrlModel = await api.GenerateDdkGoodsPromotionUrlAsync(new GenerateDdkGoodsPromotionUrlRequestModel
{
Type = "pdd.ddk.goods.promotion.url.generate",
GoodsIdList = $"[{goods_id}]",
PId = pdd_pid,
GenerateShortUrl = true,
CustomParameters = responseMessageText.FromUserName
});
return $"/:rose 親,商品資訊如下~\n========================\n{goods.GoodsName}\n【在售價】{((decimal)goods.MinGroupPrice) / 100}元\n【約返利】{Math.Round((decimal)(goods.MinGroupPrice * goods.PromotionRate) / 100000, 2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>點選這裡下單</a>\n下單確認收貨後就能收到返利佣金啦~\n\n 點選檢視 <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何領取返利</a>";
}
catch (Exception ex)
{
//LogHelper.WriteLog(typeof(WechatController), "呼叫拼多多獲取推廣連結失敗" + ex.Message);
return "";
}
}
}
完整程式碼 https://github.com/xuhang2015/TBK
請關注“網購有券”,支援下我吧。有任何問題也可以在公眾號中聯絡我。