Exemplo n.º 1
0
        /// <summary>
        /// 根据卡券Id获取卡券信息。
        /// </summary>
        /// <param name="cardId">卡券Id。</param>
        /// <returns>卡券信息。</returns>
        public CardModel Get(string cardId)
        {
            var url = "https://api.weixin.qq.com/card/get?access_token=" + _accountModel.GetAccessToken();

            var content = WeiXinHttpHelper.PostString(url, new { card_id = cardId });

            var cardObj = JObject.Parse(content)["card"];

            var type        = cardObj.Value <string>("card_type");
            var contentObj  = cardObj[type.ToLower()];
            var baseInfoObj = contentObj["base_info"];
            var cardType    = GetCardByString(type);

            CardModel card;

            switch (cardType)
            {
            case CardType.Cash:
                card = new CashCardModel
                {
                    LeastCost  = contentObj.Value <int>("least_cost"),
                    ReduceCost = contentObj.Value <int>("reduce_cost")
                };
                break;

            case CardType.Coupon:
                card = new CouponCardModel
                {
                    Detail = contentObj.Value <string>("deal_detail")
                };
                break;

            case CardType.Discount:
                card = new DiscountCardModel
                {
                    Discount = contentObj.Value <short>("discount")
                };
                break;

            case CardType.Gift:
                card = new GiftCardModel
                {
                    Gift = contentObj.Value <string>("gift")
                };
                break;

            case CardType.Group:
                card = new GroupCardModel
                {
                    Detail = contentObj.Value <string>("deal_detail")
                };
                break;

            default:
                throw new NotSupportedException("不支持的卡券类型:" + cardType);
            }

            var baseInfo = new CardBaseInfo
            {
                LogoUrl        = baseInfoObj.Value <string>("logo_url"),
                ViewTypeString = baseInfoObj.Value <string>("code_type"),
                Id             = baseInfoObj.Value <string>("id"),
                BrandName      = baseInfoObj.Value <string>("brand_name"),
                Title          = baseInfoObj.Value <string>("title"),
                SubTitle       = baseInfoObj.Value <string>("sub_title"),
                Color          = baseInfoObj.Value <string>("color"),
                Notice         = baseInfoObj.Value <string>("notice"),
                Description    = baseInfoObj.Value <string>("description"),
                Product        = new CardBaseInfoBase.ProductInfo
                {
                    Quantity = baseInfoObj.SelectToken("sku.quantity").Value <int>()
                },
                UseTime              = GetUseTime(baseInfoObj["date_info"]),
                UseCustomCode        = baseInfoObj.Value <bool>("use_custom_code"),
                BindOpenid           = baseInfoObj.Value <bool>("bind_openid"),
                ServicePhone         = baseInfoObj.Value <string>("service_phone"),
                LocationIdList       = ((JArray)baseInfoObj["location_id_list"]).Select(i => i.Value <long>()).ToArray(),
                Source               = baseInfoObj.Value <string>("source"),
                CustomUrlName        = baseInfoObj.Value <string>("custom_url_name"),
                CustomUrl            = baseInfoObj.Value <string>("custom_url"),
                CustomUrlSubTitle    = baseInfoObj.Value <string>("custom_url_sub_title"),
                PromotionUrlName     = baseInfoObj.Value <string>("promotion_url_name"),
                PromotionUrl         = baseInfoObj.Value <string>("promotion_url"),
                PromotionUrlSubTitle = baseInfoObj.Value <string>("promotion_url_sub_title"),
                GetLimit             = baseInfoObj.Value <int>("get_limit"),
                AllowShare           = baseInfoObj.Value <bool>("can_share"),
                AllowGive            = baseInfoObj.Value <bool>("can_give_friend"),
                Status               = GetStatusByString(baseInfoObj.Value <string>("status"))
            };

            card.BaseInfo = baseInfo;

            return(card);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据卡券Id获取卡券信息。
        /// </summary>
        /// <param name="cardId">卡券Id。</param>
        /// <returns>卡券信息。</returns>
        public CardModel Get(string cardId)
        {
            var url = "https://api.weixin.qq.com/card/get?access_token=" + _accountModel.GetAccessToken();

            var content = WeiXinHttpHelper.PostString(url, new { card_id = cardId });

            var cardObj = JObject.Parse(content)["card"];

            var type = cardObj.Value<string>("card_type");
            var contentObj = cardObj[type.ToLower()];
            var baseInfoObj = contentObj["base_info"];
            var cardType = GetCardByString(type);

            CardModel card;

            switch (cardType)
            {
                case CardType.Cash:
                    card = new CashCardModel
                    {
                        LeastCost = contentObj.Value<uint>("least_cost"),
                        ReduceCost = contentObj.Value<uint>("reduce_cost")
                    };
                    break;

                case CardType.Coupon:
                    card = new CouponCardModel
                    {
                        Detail = contentObj.Value<string>("deal_detail")
                    };
                    break;

                case CardType.Discount:
                    card = new DiscountCardModel
                    {
                        Discount = contentObj.Value<ushort>("discount")
                    };
                    break;

                case CardType.Gift:
                    card = new GiftCardModel
                    {
                        Gift = contentObj.Value<string>("gift")
                    };
                    break;

                case CardType.Group:
                    card = new GroupCardModel
                    {
                        Detail = contentObj.Value<string>("deal_detail")
                    };
                    break;

                default:
                    throw new NotSupportedException("不支持的卡券类型:" + cardType);
            }

            var baseInfo = new CardBaseInfo
            {
                LogoUrl = baseInfoObj.Value<string>("logo_url"),
                ViewTypeString = baseInfoObj.Value<string>("code_type"),
                Id = baseInfoObj.Value<string>("id"),
                BrandName = baseInfoObj.Value<string>("brand_name"),
                Title = baseInfoObj.Value<string>("title"),
                SubTitle = baseInfoObj.Value<string>("sub_title"),
                Color = baseInfoObj.Value<string>("color"),
                Notice = baseInfoObj.Value<string>("notice"),
                Description = baseInfoObj.Value<string>("description"),
                Product = new CardBaseInfoBase.ProductInfo
                {
                    Quantity = baseInfoObj.SelectToken("sku.quantity").Value<uint>()
                },
                UseTime = GetUseTime(baseInfoObj["date_info"]),
                UseCustomCode = baseInfoObj.Value<bool>("use_custom_code"),
                BindOpenid = baseInfoObj.Value<bool>("bind_openid"),
                ServicePhone = baseInfoObj.Value<string>("service_phone"),
                LocationIdList = ((JArray)baseInfoObj["location_id_list"]).Select(i => i.Value<long>()).ToArray(),
                Source = baseInfoObj.Value<string>("source"),
                CustomUrlName = baseInfoObj.Value<string>("custom_url_name"),
                CustomUrl = baseInfoObj.Value<string>("custom_url"),
                CustomUrlSubTitle = baseInfoObj.Value<string>("custom_url_sub_title"),
                PromotionUrlName = baseInfoObj.Value<string>("promotion_url_name"),
                PromotionUrl = baseInfoObj.Value<string>("promotion_url"),
                PromotionUrlSubTitle = baseInfoObj.Value<string>("promotion_url_sub_title"),
                GetLimit = baseInfoObj.Value<uint>("get_limit"),
                AllowShare = baseInfoObj.Value<bool>("can_share"),
                AllowGive = baseInfoObj.Value<bool>("can_give_friend"),
                Status = GetStatusByString(baseInfoObj.Value<string>("status"))
            };

            card.BaseInfo = baseInfo;

            return card;
        }