主要实现功能:关注公众号的用户发送拼多多商品链接,后台程序通过链接查找商品优惠券或返佣情况。
说明:使用了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 亲,这款商品的优惠返利活动结束了~
请换个商品试试吧。
========================
ue231 <a href='https://mobile.yangkeduo.com/duo_cms_mall.html?pid=2495191_31302208cpsSign=CM2495191_31302208_3a1c1a0431608b9c1eb417183d57c1bdduoduo_type=2'>拼多多优惠券商城</a>
下单确认收货后就能收到返利佣金啦~";
}
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 亲,商品信息如下~
========================
{goods.GoodsName}
【在售价】{((decimal)goods.MinGroupPrice) / 100}元
【券后价】{Math.Round(((decimal)(goods.MinGroupPrice - goods.CouponDiscount.Value)) / 100, 2)}元
ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>点击这里下单</a>
下单确认收货后就能收到返利佣金啦~";
}
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 亲,商品信息如下~
========================
{goods.GoodsName}
【在售价】{((decimal)goods.MinGroupPrice) / 100}元
【约返利】{Math.Round((decimal)(goods.MinGroupPrice * goods.PromotionRate) / 100000,2)}元
ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>点击这里下单</a>
下单确认收货后就能收到返利佣金啦~
点击查看 <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
请关注“网购有券”,支持下我吧。有任何问题也可以在公众号中联系我。