雙十一以前終於把淘寶客公衆號搭建好了,雖然耗時很多,但仍是蠻有成就感的,能不能賺錢就另一回事了。html
我這裏用到了淘寶聯盟api權限、拼多多平臺接口api、京東申請比較麻煩就沒有申請,因此京東使用了第三方免費接口,各聯盟帳號api權限申請我就不贅述了。api
效果: 微信
原理:async
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找券。 京東代碼:使用了第三方接口(好京客)ui
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 ""; } }
拼多多代碼以下:url
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 ""; } } }
詳細請查看 http://blog.yshizi.cn/5.html 請關注「網購有券」,支持下我吧。有任何問題也能夠在公衆號中聯繫我。spa