/// <summary> /// 代金券与立减优惠 /// </summary> /// <param name="coupon"></param> /// <param name="key"></param> /// <param name="certpath"></param> /// <param name="certpwd"></param> /// <returns></returns> public static SendCouponRes SendCoupon(SendCoupon coupon, string key, string certpath, string certpwd) { var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon"; return(PayRequest <SendCouponRes>(coupon, key, url, certpath, certpwd)); }
public ActionResult SendCouponOnPost(SendCoupon model) { var result = new DataJsonResult(); try { //发放或赠送的不计算数量 var coupon = _currencyService.GetSingleById <Models.Coupon>(model.CouponId); if (coupon == null) { throw new Exception("优惠券Id不合法"); } //消息推送内容 var pushTargetType = (int)PushTargetType.Coupon; Dictionary <string, string> pushDic = new Dictionary <string, string>(); pushDic.Add("PushTargetType", pushTargetType.ToString()); var title = "系统赠送优惠券"; var content = ""; if (coupon.CouponType == CouponType.Discount) { content = $"恭喜您,获得系统赠送的[{coupon.Discount}折]的折扣券,请尽快使用!"; } else { content = $"恭喜您,获得系统赠送的[满{coupon.Minimum}减{coupon.Money}]的满减券,请尽快使用!"; } using (TransactionScope scope = new TransactionScope()) { if (model.SentType == 1) { //分类别发放 List <Member> members = null; if (model.MemberType == 0) { members = _currencyService.GetList <Member>(me => me.UserType == Security.Identity.UserType.Member); } //else // members = _currencyService.GetList<Member>(me => me.MemberType == model.MemberType && me.UserType == Security.Identity.UserType.Member); if (members == null || members.Count == 0) { throw new Exception("该分类没有会员"); } foreach (var item in members) { if (!(SendCouponToMember(item, coupon))) { throw new Exception("发放失败"); } } //消息通知发送 if (!_systemMessageService.CreatePushSystemMessage(title, content, content, null, null, pushDic, "BackSend", SystemMessageModule.Key, MessageCategory.System)) { throw new Exception("发送群消息失败"); } } else { if (string.IsNullOrWhiteSpace(model.UserName)) { throw new Exception("请输入用户名"); } var member = _currencyService.GetSingleByConditon <Member>(me => me.UserName == model.UserName); if (member == null) { throw new Exception("会员不存在"); } if (!(SendCouponToMember(member, coupon))) { throw new Exception("发放失败"); } //消息通知发送 if (!_systemMessageService.CreatePushSystemMessage(title, content, content, member.Id, null, pushDic, "BackSend", SystemMessageModule.Key, MessageCategory.System)) { throw new Exception("发送群消息失败"); } } scope.Complete(); } } catch (Exception e) { result.ErrorMessage = e.Message; } //发放 return(Json(result)); }