Example #1
0
        /// <summary>
        /// 添加卡券
        /// </summary>
        /// <param name="info"></param>
        public bool Add(WXCardLogInfo info)
        {
            bool   issuccess = false;
            string acctoken  = "";
            string curHost   = "http://" + Core.Helper.WebHelper.GetHost();
            string imagefile = "/images/defaultwxlogo.png";
            string wxlogo    = Core.HimallIO.GetImagePath(imagefile);

            if (wxlogo.IndexOf("http") < 0)
            {
                wxlogo = curHost + imagefile;
            }
            var siteSetting = ServiceProvider.Instance <ISiteSettingService> .Create.GetSiteSettings();

            var shopser  = ServiceProvider.Instance <IShopService> .Create;
            var vshopser = ServiceProvider.Instance <IVShopService> .Create;
            CardCreateResultJson wxResultJson = new CardCreateResultJson();

            #region 组织数据
            //基础数据
            Card_BaseInfoBase _BaseInfo = new Card_BaseInfoBase()
            {
                logo_url    = wxlogo,
                brand_name  = siteSetting.SiteName,
                code_type   = Card_CodeType.CODE_TYPE_BARCODE,
                title       = info.CardTitle,
                sub_title   = info.CardSubTitle,
                color       = info.CardColor,
                notice      = "专供" + siteSetting.SiteName + "使用",
                description = @"" + (info.LeastCost > 0 ? "满¥" + (info.LeastCost / 100).ToString("F2") + "使用" : "无门槛使用") + ",有效期至" + info.BeginTime.ToString("yyyy年MM月dd日") + "-" + info.EndTime.ToString("yyyy年MM月dd日"),
                date_info   = new Card_BaseInfo_DateInfo()
                {
                    type            = Card_DateInfo_Type.DATE_TYPE_FIX_TIME_RANGE.ToString(),
                    begin_timestamp = DateTimeHelper.GetWeixinDateTime(info.BeginTime),
                    end_timestamp   = DateTimeHelper.GetWeixinDateTime(info.EndTime),
                },
                sku = new Card_BaseInfo_Sku()
                {
                    quantity = info.Quantity == 0 ? MaxStock : info.Quantity
                },
                get_limit       = info.GetLimit == 0 ? MaxStock : info.GetLimit,
                use_custom_code = false,
                bind_openid     = false,
                can_share       = false,
                can_give_friend = false,
                custom_url_name = "立即使用",
            };
            //代金券数据
            var cardData = new Card_GeneralCouponData()
            {
                base_info      = _BaseInfo,
                default_detail = info.DefaultDetail
            };
            #endregion

            #region 诊所发布
            if (info.ShopId > 0)
            {
                _BaseInfo.custom_url = curHost + "/Shop/Home/" + info.ShopId.ToString();
            }
            var vshopSetting = vshopser.GetVShopSetting(info.ShopId);
            var vshopinfo    = vshopser.GetVShopByShopId(info.ShopId);
            var shopinfo     = shopser.GetShop(info.ShopId);
            if (vshopSetting != null && shopinfo != null && vshopinfo != null)
            {
                if (!string.IsNullOrWhiteSpace(vshopSetting.AppId) && !string.IsNullOrWhiteSpace(vshopSetting.AppSecret))
                {
                    acctoken = GetAccessToken(vshopSetting.AppId, vshopSetting.AppSecret);
                    if (!string.IsNullOrWhiteSpace(acctoken))
                    {
                        _BaseInfo.brand_name = shopinfo.ShopName;
                        if (!string.IsNullOrWhiteSpace(vshopinfo.WXLogo))
                        {
                            wxlogo             = curHost + vshopinfo.WXLogo;
                            _BaseInfo.logo_url = wxlogo;
                        }
                        wxResultJson = CardApi.CreateCard(acctoken, cardData);
                        if (wxResultJson.errcode == Senparc.Weixin.ReturnCode.请求成功)
                        {
                            info.AppId     = vshopSetting.AppId;
                            info.AppSecret = vshopSetting.AppSecret;
                            info.CardId    = wxResultJson.card_id;
                            issuccess      = true;
                        }
                    }
                }
            }
            #endregion

            if (!issuccess)
            {
                #region 平台发布
                if (!string.IsNullOrWhiteSpace(siteSetting.WeixinAppId) && !string.IsNullOrWhiteSpace(siteSetting.WeixinAppSecret))
                {
                    acctoken = GetAccessToken(siteSetting.WeixinAppId, siteSetting.WeixinAppSecret);
                    if (!string.IsNullOrWhiteSpace(acctoken))
                    {
                        _BaseInfo.brand_name = shopinfo.ShopName;
                        if (!string.IsNullOrWhiteSpace(siteSetting.WXLogo))
                        {
                            wxlogo             = curHost + siteSetting.WXLogo;
                            _BaseInfo.logo_url = wxlogo;
                        }
                        wxResultJson = CardApi.CreateCard(acctoken, cardData);
                        if (wxResultJson.errcode == Senparc.Weixin.ReturnCode.请求成功)
                        {
                            info.AppId     = siteSetting.WeixinAppId;
                            info.AppSecret = siteSetting.WeixinAppSecret;
                            info.CardId    = wxResultJson.card_id;
                            issuccess      = true;
                        }
                    }
                }

                if (info.ShopId < 1)
                {
                    _BaseInfo.custom_url = curHost + "/";
                }
                #endregion
            }

            if (issuccess)
            {
                info.AuditStatus = (int)WXCardLogInfo.AuditStatusEnum.Auditin;    //初始审核状态
                //数据入库
                Context.WXCardLogInfo.Add(info);
                Context.SaveChanges();
            }
            return(issuccess);
        }
Example #2
0
        public bool Add(WXCardLogInfo info)
        {
            string           str;
            bool             flag         = false;
            string           accessToken  = "";
            string           str1         = string.Concat("http://", WebHelper.GetHost());
            string           str2         = string.Concat(str1, "/images/defaultwxlogo.png");
            SiteSettingsInfo siteSettings = Instance <ISiteSettingService> .Create.GetSiteSettings();

            IShopService         create               = Instance <IShopService> .Create;
            IVShopService        vShopService         = Instance <IVShopService> .Create;
            CardCreateResultJson cardCreateResultJson = new CardCreateResultJson();
            Card_BaseInfoBase    cardBaseInfoBase     = new Card_BaseInfoBase()
            {
                logo_url   = str2,
                brand_name = siteSettings.SiteName,
                code_type  = Card_CodeType.CODE_TYPE_BARCODE,
                title      = info.CardTitle,
                sub_title  = info.CardSubTitle,
                color      = info.CardColor,
                notice     = string.Concat("专供", siteSettings.SiteName, "使用")
            };
            Card_BaseInfoBase cardBaseInfoBase1 = cardBaseInfoBase;

            string[] strArrays  = new string[5];
            string[] strArrays1 = strArrays;
            if (info.LeastCost > 0)
            {
                int leastCost = info.LeastCost / 100;
                str = string.Concat("满¥", leastCost.ToString("F2"), "使用");
            }
            else
            {
                str = "无门槛使用";
            }
            strArrays1[0] = str;
            strArrays[1]  = ",有效期至";
            strArrays[2]  = info.BeginTime.ToString("yyyy年MM月dd日");
            strArrays[3]  = "-";
            strArrays[4]  = info.EndTime.ToString("yyyy年MM月dd日");
            cardBaseInfoBase1.description = string.Concat(strArrays);
            Card_BaseInfo_DateInfo cardBaseInfoDateInfo = new Card_BaseInfo_DateInfo()
            {
                type            = Card_DateInfo_Type.DATE_TYPE_FIX_TIME_RANGE.ToString(),
                begin_timestamp = Senparc.Weixin.Helpers.DateTimeHelper.GetWeixinDateTime(info.BeginTime),
                end_timestamp   = Senparc.Weixin.Helpers.DateTimeHelper.GetWeixinDateTime(info.EndTime)
            };

            cardBaseInfoBase.date_info = cardBaseInfoDateInfo;
            Card_BaseInfoBase cardBaseInfoBase2 = cardBaseInfoBase;
            Card_BaseInfo_Sku cardBaseInfoSku   = new Card_BaseInfo_Sku()
            {
                quantity = (info.Quantity == 0 ? MaxStock : info.Quantity)
            };

            cardBaseInfoBase2.sku            = cardBaseInfoSku;
            cardBaseInfoBase.get_limit       = (info.GetLimit == 0 ? MaxStock : info.GetLimit);
            cardBaseInfoBase.use_custom_code = false;
            cardBaseInfoBase.bind_openid     = false;
            cardBaseInfoBase.can_share       = false;
            cardBaseInfoBase.can_give_friend = false;
            cardBaseInfoBase.custom_url_name = "立即使用";
            Card_BaseInfoBase      shopName = cardBaseInfoBase;
            Card_GeneralCouponData cardGeneralCouponDatum = new Card_GeneralCouponData()
            {
                base_info      = shopName,
                default_detail = info.DefaultDetail
            };
            Card_GeneralCouponData cardGeneralCouponDatum1 = cardGeneralCouponDatum;

            if (info.ShopId > 0)
            {
                long shopId = info.ShopId;
                shopName.custom_url = string.Concat(str1, "/Shop/Home/", shopId.ToString());
            }
            WXShopInfo vShopSetting  = vShopService.GetVShopSetting(info.ShopId);
            VShopInfo  vShopByShopId = vShopService.GetVShopByShopId(info.ShopId);
            ShopInfo   shop          = create.GetShop(info.ShopId, false);

            if (vShopSetting != null && shop != null && vShopByShopId != null && !string.IsNullOrWhiteSpace(vShopSetting.AppId) && !string.IsNullOrWhiteSpace(vShopSetting.AppSecret))
            {
                accessToken = GetAccessToken(vShopSetting.AppId, vShopSetting.AppSecret);
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    shopName.brand_name = shop.ShopName;
                    if (!string.IsNullOrWhiteSpace(vShopByShopId.WXLogo))
                    {
                        shopName.logo_url = string.Concat(str1, vShopByShopId.WXLogo);
                    }
                    cardCreateResultJson = CardApi.CreateCard(accessToken, cardGeneralCouponDatum1, 10000);
                    if (cardCreateResultJson.errcode == ReturnCode.请求成功)
                    {
                        info.AppId     = vShopSetting.AppId;
                        info.AppSecret = vShopSetting.AppSecret;
                        info.CardId    = cardCreateResultJson.card_id;
                        flag           = true;
                    }
                }
            }
            if (!flag)
            {
                if (!string.IsNullOrWhiteSpace(siteSettings.WeixinAppId) && !string.IsNullOrWhiteSpace(siteSettings.WeixinAppSecret))
                {
                    accessToken = GetAccessToken(siteSettings.WeixinAppId, siteSettings.WeixinAppSecret);
                    if (!string.IsNullOrWhiteSpace(accessToken))
                    {
                        shopName.brand_name = shop.ShopName;
                        if (!string.IsNullOrWhiteSpace(siteSettings.WXLogo))
                        {
                            shopName.logo_url = string.Concat(str1, siteSettings.WXLogo);
                        }
                        cardCreateResultJson = CardApi.CreateCard(accessToken, cardGeneralCouponDatum1, 10000);
                        if (cardCreateResultJson.errcode == ReturnCode.请求成功)
                        {
                            info.AppId     = siteSettings.WeixinAppId;
                            info.AppSecret = siteSettings.WeixinAppSecret;
                            info.CardId    = cardCreateResultJson.card_id;
                            flag           = true;
                        }
                    }
                }
                if (info.ShopId < 1)
                {
                    shopName.custom_url = string.Concat(str1, "/");
                }
            }
            if (flag)
            {
                info.AuditStatus = new int?(0);
                context.WXCardLogInfo.Add(info);
                context.SaveChanges();
            }
            return(flag);
        }