主要實現功能:關注公眾號的用戶發送拼多多商品鏈接,后台程序通過鏈接查找商品優惠券或返佣情況。
說明:使用了niltor 封裝的拼多多接口 github地址 ,但是需要注意可能會存在返回模型無法正確解析,導致沒有正確返回數據的問題。使用的過程中修改下返回模型屬性類型就好了。
最終實現效果如下:

代碼實現:
public async System.Threading.Tasks.Task<string> GetPddCouponAsync(ExmlMsg xmlMsg)
{
//通過正則表達式解析拼多多商品鏈接,獲取拼多多商品Id
string msg = xmlMsg.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 "";
}
//調用之前需要先注冊PddOpenSdk服務
/*
PddCommonApi.ClientId = "d8172a66ddf14220beac58e8eddca0d9";
PddCommonApi.ClientSecret = "d5993ce16cc0d1bd35403176f35544962e786645";
PddCommonApi.RedirectUri = "RedirectUri";
*/
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 = xmlMsg.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下單確認收貨后就能收到返利佣金啦~";
}
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 = xmlMsg.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/44.html
請關注“網購有券”,支持下我吧。有任何問題也可以在公眾號中聯系我。
