public CreateCardResult AddWxCoupons(Coupons coupons, XcxAppAccountRelation xcx, string accountId) { //默认专业版的 string center_app_brand_pass = "******"; //专业版 个人中心 string custom_app_brand_pass = "******"; //首页 专业版 string logo_url = string.Empty; string brand_name = string.Empty; string appOriginalId = string.Empty; CreateCardResult _createCardResult = new CreateCardResult(); XcxTemplate xcxTemplate = XcxTemplateBLL.SingleModel.GetModel($"id={xcx.TId}"); if (xcxTemplate == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "小程序模板不存在"; return(_createCardResult); } List <OpenAuthorizerConfig> listOpenAuthorizerConfig = OpenAuthorizerConfigBLL.SingleModel.GetListByaccoundidAndRid(accountId, xcx.Id, 4); if (listOpenAuthorizerConfig == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "请先绑定认证服务号才有生成卡券权限"; return(_createCardResult); } OpenAuthorizerConfig umodel = listOpenAuthorizerConfig[0]; switch (xcxTemplate.Type) { case (int)TmpType.小程序专业模板: EntSetting ent = EntSettingBLL.SingleModel.GetModel(coupons.appId); if (ent == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "该专业版信息找不到"; return(_createCardResult); } OpenAuthorizerConfig XUserList = OpenAuthorizerConfigBLL.SingleModel.GetModelByAppids(xcx.AppId); if (XUserList == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "请先授权给平台"; return(_createCardResult); } ConfParam imginfo = ConfParamBLL.SingleModel.GetModelByParamappid("logoimg", xcx.AppId); if (imginfo == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "请先到小程序管理配置底部Logo"; return(_createCardResult); } logo_url = imginfo.Value; brand_name = XUserList.nick_name; break; case (int)TmpType.小程序电商模板: center_app_brand_pass = "******"; //个人中心页面 Store store = StoreBLL.SingleModel.GetModelByAId(xcx.Id); if (store == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "电商版店铺不存在"; return(_createCardResult); } logo_url = store.logo; brand_name = store.name; break; case (int)TmpType.小程序餐饮模板: center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 Food miAppFood = FoodBLL.SingleModel.GetModel($"appId={xcx.Id}"); if (miAppFood == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "餐饮版店铺不存在"; return(_createCardResult); } logo_url = miAppFood.Logo; brand_name = miAppFood.FoodsName; break; case (int)TmpType.小程序足浴模板: case (int)TmpType.小程序多门店模板: int t = 0; if ((int)TmpType.小程序足浴模板 == xcxTemplate.Type) { center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 } else { center_app_brand_pass = "******"; //个人中心页面 t = 1; } FootBath storeModel = FootBathBLL.SingleModel.GetModel($"appId={xcx.Id}"); if (storeModel == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "找不到该足浴版"; return(_createCardResult); } brand_name = storeModel.StoreName; List <C_Attachment> LogoList = C_AttachmentBLL.SingleModel.GetListByCache(storeModel.Id, t == 0 ? (int)AttachmentItemType.小程序足浴版店铺logo : (int)AttachmentItemType.小程序多门店版门店logo); if (LogoList != null && LogoList.Count > 0) { logo_url = LogoList[0].filepath; } break; case (int)TmpType.小未平台子模版: center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 PlatStore platStore = PlatStoreBLL.SingleModel.GetPlatStore(xcx.Id, 2); if (platStore == null) { _createCardResult.errcode = 1; _createCardResult.errmsg = "平台版店铺不存在"; return(_createCardResult); } brand_name = platStore.Name; logo_url = platStore.StoreHeaderImg; break; } if (string.IsNullOrEmpty(logo_url) || string.IsNullOrEmpty(brand_name)) { _createCardResult.errcode = 1; _createCardResult.errmsg = "请先配置Logo以及名称"; return(_createCardResult); } //这里可能会出现token失效 个人发布的未授权给我们第三方平台的卡券会生成不了 string xcxapiurl = XcxApiBLL.SingleModel.GetOpenAuthodModel(umodel.user_name); string authorizer_access_token = CommondHelper.GetAuthorizer_Access_Token(xcxapiurl); string uploadImgResult = CommondHelper.WxUploadImg(authorizer_access_token, logo_url); if (!uploadImgResult.Contains("url")) { _createCardResult.errcode = 1; _createCardResult.errmsg = $"上传Logo到微信失败uploadImgResult={uploadImgResult}"; return(_createCardResult); } if (brand_name.Length >= 12) { brand_name = brand_name.Substring(0, 12); } WxUploadImgResult wxUploadImgResult = JsonConvert.DeserializeObject <WxUploadImgResult>(uploadImgResult); logo_url = wxUploadImgResult.url; base_info _base_info = new base_info(); _base_info.logo_url = logo_url; _base_info.code_type = "CODE_TYPE_TEXT"; _base_info.brand_name = brand_name; _base_info.title = coupons.CouponName.Length > 9 ? Utility.StringHelper.strSubstring(coupons.CouponName, 0, 8) : coupons.CouponName; _base_info.color = "Color010"; _base_info.center_title = "立即使用"; _base_info.center_app_brand_user_name = $"{appOriginalId}@app"; _base_info.center_app_brand_pass = center_app_brand_pass; _base_info.custom_url_name = "小程序"; _base_info.custom_app_brand_user_name = $"{appOriginalId}@app"; _base_info.custom_app_brand_pass = custom_app_brand_pass; _base_info.custom_url_sub_title = "点击进入"; _base_info.description = coupons.Desc.Length > 1024 ? Utility.StringHelper.strSubstring(coupons.Desc, 0, 1023) : coupons.Desc; _base_info.notice = "使用时向服务员出示此券"; _base_info.sku.quantity = coupons.CreateNum; if (coupons.LimitReceive > 0) { //大于0表示限制,0表示无限制 _base_info.get_limit = coupons.LimitReceive; } if (coupons.ValType == 0) { //表示固定日期 _base_info.date_info = new Firstdate_infoItem() { begin_timestamp = WxUtils.GetWeixinDateTime(coupons.StartUseTime), end_timestamp = WxUtils.GetWeixinDateTime(coupons.EndUseTime) }; } if (coupons.ValType == 2 || coupons.ValType == 1) { //fixed_begin_term=0表示领取后当天开始生效 1表示次日后开始生效 领取当日N天内有效 _base_info.date_info = new Seconddate_infoItem() { fixed_begin_term = coupons.ValType == 2?0:1, fixed_term = coupons.ValDay }; } string json = string.Empty; if (coupons.CouponWay == 0) { //表示是需要生成微信代金券 WxCashCoupons wxCashCoupons = new WxCashCoupons(); Cash cash = new Cash(); cash.base_info = _base_info;//基础字段信息 Use_condition use_condition = new Use_condition(); use_condition.accept_category = coupons.GoodsType == 0 ? "全部产品" : "部分产品"; if (coupons.LimitMoney > 0) { cash.least_cost = coupons.LimitMoney; //满减门槛 use_condition.least_cost = coupons.LimitMoney; } use_condition.can_use_with_other_discount = coupons.discountType == 0; cash.reduce_cost = coupons.Money; cash.advanced_info = new Advanced_info() { use_condition = use_condition }; wxCashCoupons.cash = cash; json = JsonConvert.SerializeObject(new { card = wxCashCoupons }); } else { //表示是需要生成微信折扣券 WxDiscountCoupons wxDiscountCoupons = new WxDiscountCoupons(); Discount discount = new Discount(); discount.base_info = _base_info; discount.discount = (int)(100 - coupons.Money * 0.1); wxDiscountCoupons.discount = discount; json = JsonConvert.SerializeObject(new { card = wxDiscountCoupons }); } string result = Utility.IO.Context.PostData($"https://api.weixin.qq.com/card/create?access_token={authorizer_access_token}", json); if (string.IsNullOrEmpty(result)) { return(_createCardResult); } _createCardResult = JsonConvert.DeserializeObject <CreateCardResult>(result); return(_createCardResult); }
/// <summary> /// 创建微信会员卡卡套 /// </summary> /// <param name="logo_url">会员卡Logo</param> /// <param name="brand_name">商户名字,字数上限为12个汉字。 </param> /// <param name="title">卡券名,字数上限为9个汉字</param> /// <param name="appOriginalId">小程序原始Id</param> /// <param name="access_token">小程序access_token</param> public static CreateCardResult AddVipWxCard(string logo_url, string brand_name, string title, string appOriginalId, string access_token, int PageType = 22) { //默认专业版的 string center_app_brand_pass = "******"; //专业版 个人中心 string custom_app_brand_pass = "******"; //首页 专业版 switch (PageType) { case (int)TmpType.小程序电商模板: center_app_brand_pass = "******"; //个人中心页面 break; case (int)TmpType.小程序餐饮模板: center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 break; case (int)TmpType.小程序足浴模板: center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 break; case (int)TmpType.小程序多门店模板: center_app_brand_pass = "******"; //个人中心页面 break; case (int)TmpType.小未平台子模版: center_app_brand_pass = "******"; //个人中心页面 custom_app_brand_pass = "******"; //首页 break; } base_info _base_info = new base_info(); _base_info.logo_url = logo_url; _base_info.code_type = "CODE_TYPE_TEXT"; _base_info.brand_name = brand_name; _base_info.title = title; _base_info.color = "Color010"; _base_info.center_title = "储值余额"; _base_info.center_app_brand_user_name = $"{appOriginalId}@app"; _base_info.center_app_brand_pass = center_app_brand_pass; _base_info.custom_url_name = "小程序"; _base_info.custom_app_brand_user_name = $"{appOriginalId}@app"; _base_info.custom_app_brand_pass = custom_app_brand_pass; _base_info.custom_url_sub_title = "点击进入"; //_base_info.promotion_url_name = "门店地址"; //_base_info.promotion_app_brand_user_name = $"{appOriginalId}@app"; //_base_info.promotion_app_brand_pass = "******"; _base_info.description = $"{brand_name}会员卡"; _base_info.notice = "使用时向服务员出示此券"; Card _card = new Card(); WxCard wxCard = new WxCard(); member_card _member_Card = new member_card(); _member_Card.auto_activate = true; _member_Card.base_info = _base_info; _member_Card.custom_field1 = new custom_fieldItem { name = "储值余额", }; _member_Card.custom_field2 = new custom_fieldItem { name = "累计消费", }; _member_Card.custom_field3 = new custom_fieldItem { name = "会员权益", }; _member_Card.prerogative = "更快更便捷的了解积分余额等信息"; _card.member_card = _member_Card; wxCard.card = _card; string json = JsonConvert.SerializeObject(wxCard); string result = Utility.IO.Context.PostData($"https://api.weixin.qq.com/card/create?access_token={access_token}", json); CreateCardResult _createCardResult = new CreateCardResult(); // _createCardResult.errmsg = json; if (string.IsNullOrEmpty(result)) { return(_createCardResult); } _createCardResult = JsonConvert.DeserializeObject <CreateCardResult>(result); return(_createCardResult); }
public ActionResult addorupdatecoupon(Coupons coupon) { if (coupon == null || coupon.appId <= 0) { return(Json(new { isok = false, msg = "参数错误" })); } int appId = coupon.appId; XcxAppAccountRelation app = XcxAppAccountRelationBLL.SingleModel.GetModel(appId); if (app == null) { return(Json(new { isok = false, msg = "没开通模板" })); } XcxTemplate xcxTemplate = XcxTemplateBLL.SingleModel.GetModel($"id={app.TId}"); if (xcxTemplate == null) { return(Json(new { isok = false, msg = "小程序模板不存在" })); } int showTipCount = CouponsBLL.SingleModel.GetShowTipCount(appId, coupon.Id); if (showTipCount >= 3 && coupon.IsShowTip == 1) { return(Json(new { isok = false, msg = "只允许设置3张优惠券弹窗显示" })); } #region 专业版 版本控制 if (xcxTemplate.Type == (int)TmpType.小程序专业模板) { FunctionList functionList = new FunctionList(); int versionId = app.VersionId; functionList = FunctionListBLL.SingleModel.GetModel($"TemplateType={xcxTemplate.Type} and VersionId={versionId}"); if (functionList == null) { return(Json(new { isok = false, msg = "此功能未开启" })); } MarketingPlugin marketingPlugin = new MarketingPlugin(); if (!string.IsNullOrEmpty(functionList.MarketingPlugin)) { marketingPlugin = JsonConvert.DeserializeObject <MarketingPlugin>(functionList.MarketingPlugin); } if (marketingPlugin.ReductionCard == 1)//表示关闭了立减金功能 { return(Json(new { isok = false, msg = "请升级更高版本才能使用此功能!" })); } } #endregion //表单验证 if (coupon.CreateNum > 999 || coupon.CreateNum < 1) { return(Json(new { isok = false, msg = "优惠券的生成数量为1-999!" }, JsonRequestBehavior.AllowGet)); } //固定日期 if (coupon.Id <= 0) { if (coupon.ValType == 0) { if (coupon.EndUseTime < coupon.StartUseTime) { return(Json(new { isok = false, msg = "优惠券过期日期必须大于生效日期!" }, JsonRequestBehavior.AllowGet)); } } else if (coupon.ValDay <= 0) { return(Json(new { isok = false, msg = "优惠券生效日期不能为空!" }, JsonRequestBehavior.AllowGet)); } } if (coupon.Money <= 0) { return(Json(new { isok = false, msg = coupon.CouponWay == 0 ? "优惠金额必须0!" : "优惠折扣必须大于0" }, JsonRequestBehavior.AllowGet)); } else if (coupon.CouponWay == 1 && coupon.Money > 99 * 100) { return(Json(new { isok = false, msg = "优惠折扣不能超过9.9折" }, JsonRequestBehavior.AllowGet)); } bool result = false; #region Base64解密 try { if (!string.IsNullOrEmpty(coupon.Desc)) { string strDescription = coupon.Desc.Replace(" ", "+"); byte[] bytes = Convert.FromBase64String(strDescription); coupon.Desc = System.Text.Encoding.UTF8.GetString(bytes); } coupon.UpdateTime = DateTime.Now; if (coupon.Id > 0) { Coupons oldModel = CouponsBLL.SingleModel.GetModel(coupon.Id); if (null == oldModel) { return(Json(new { isok = false, msg = "优惠券出错,请刷新重试!" }, JsonRequestBehavior.AllowGet)); } //已领取份数 int couponUserNum = 0; List <CouponLog> loglist = CouponLogBLL.SingleModel.GetList("couponid=" + coupon.Id + " and state!=4 "); if (loglist != null && loglist.Any()) { couponUserNum = loglist.GroupBy(g => g.FromOrderId).Count(); } if (couponUserNum > coupon.CreateNum) { return(Json(new { isok = false, msg = $"当前优惠券已被领取{couponUserNum}份 , 生成数量不能小于已领取数量!" }, JsonRequestBehavior.AllowGet)); } // log4net.LogHelper.WriteInfo(this.GetType(),JsonConvert.SerializeObject(coupon)); string columnField = "couponname,CreateNum,UpdateTime,desc,IsShowTip,discountType,WxCouponsCardOpen"; //生成微信卡包对应的优惠券 if (string.IsNullOrEmpty(coupon.WxCouponsCardId) && coupon.WxCouponsCardOpen == 1) { CreateCardResult createCardResult = CouponsBLL.SingleModel.AddWxCoupons(coupon, app, dzaccount.Id.ToString()); if (createCardResult != null && createCardResult.errcode == 0) { columnField += ",WxCouponsCardId,WxCouponsCardOpenResult"; coupon.WxCouponsCardId = createCardResult.card_id; coupon.WxCouponsCardOpenResult = $"同步成功微信优惠券ID:{createCardResult.card_id}"; } else { columnField += ",WxCouponsCardOpenResult"; coupon.WxCouponsCardOpenResult = $"同步失败原因{createCardResult.errcode}:{createCardResult.errmsg}"; } } result = CouponsBLL.SingleModel.Update(coupon, columnField); } else { coupon.AddTime = DateTime.Now; //添加优惠券 if (coupon.WxCouponsCardOpen == 1) { CreateCardResult createCardResult = CouponsBLL.SingleModel.AddWxCoupons(coupon, app, dzaccount.Id.ToString()); if (createCardResult != null && createCardResult.errcode == 0) { coupon.WxCouponsCardId = createCardResult.card_id; coupon.WxCouponsCardOpenResult = $"同步成功微信优惠券ID:{createCardResult.card_id}"; } else { coupon.WxCouponsCardOpenResult = $"同步失败原因:{createCardResult.errmsg}"; } } int id = coupon.Id = Convert.ToInt32(CouponsBLL.SingleModel.Add(coupon)); result = id > 0; } } catch (Exception ex) { return(Json(new { isok = false, msg = "系统繁忙!" + ex.Message }, JsonRequestBehavior.AllowGet)); } #endregion return(Json(new { isok = result, msg = result ? "保存成功!" : "系统繁忙" }, JsonRequestBehavior.AllowGet)); }