コード例 #1
0
        public static string GetCardApi(string token)
        {
            XHYCouponService _xyhService    = new XHYCouponService();
            string           Cardapi_ticket = "";
            CardApiTicket    at             = new CardApiTicket();

            at = _xyhService.GetModelCardApi();
            if (at == null)
            {
                at = new CardApiTicket();
            }
            if (at.Cardapi_Ticket != "" && at.Cardapi_Ticket != null && (at.GetTicketTime == null ? DateTime.Now.AddHours(-3) : at.GetTicketTime.Value).AddHours(2) > DateTime.Now) //不为空,并且获取时间没有超过2小时
            {
                return(at.Cardapi_Ticket);
            }
            else
            {
                Cardapi_ticket = at.Cardapi_Ticket;
                string  url    = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=wx_card";
                string  b      = PostRequest(url);
                CardApi ticket = JsonConvert.DeserializeObject <CardApi>(b);
                if (ticket.errcode == 0)  //正确
                {
                    at.Cardapi_Ticket = ticket.ticket;
                    at.GetTicketTime  = DateTime.Now;
                    _xyhService.AddCardApi(at);
                    return(at.Cardapi_Ticket);
                }
                return("");
            }
        }
コード例 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["para"] == null)
            {
                if ((Request.QueryString["openid"] != null) || Session["FromUserName"] != null)
                {
                    //WriteTxt("结束授权");
                    string user         = Request.QueryString["openid"] == null ? Session["FromUserName"].ToString() : Request.QueryString["openid"].ToString();
                    string access_token = Request.QueryString["Access_token"] == null ? Session["Access_token"].ToString() : Request.QueryString["Access_token"].ToString();
                    Session["FromUserName"] = user;
                    Session["Access_token"] = access_token;

                    HttpCookie cookie = Request.Cookies["XYH_COUPON"];
                    if (cookie == null)
                    {
                        cookie = new HttpCookie("XYH_COUPON");
                    }
                    cookie.Values.Set("FromUserName", user);

                    string url   = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + user + "&lang=zh_CN";
                    string token = PostRequest(url);
                    WriteTxt(url);
                    WriteTxt(token);
                    OpenInfo autho = JsonConvert.DeserializeObject <OpenInfo>(token);
                    if (!string.IsNullOrWhiteSpace(autho.headimgurl))
                    {
                        MySmallShopService mss = new MySmallShopService();
                        OAauth_Log         oa  = mss.GetOA(user, "商业集团送券活动");
                        if (oa == null)
                        {
                            oa = new OAauth_Log();
                            oa.FromUserName = user;
                            oa.ToUserName   = "******";
                            oa.headimgurl   = autho.headimgurl;
                            oa.Nickname     = autho.nickname;
                            oa.CreateDate   = DateTime.Now;
                            mss.SaveOA(oa);
                        }
                    }

                    XHYCouponService _xyhService = new XHYCouponService();
                    //判断是否参加并且可以领取卡券
                    WXCouponGiveInfo model = _xyhService.GetWXCouponGiveInfoByOpenid(user, "SYJT");
                    if (model != null)
                    {
                        if (model.GetCoupon == 1)
                        {
                            var phone = model.Mobile;
                            Response.Redirect("list.html?mobile=" + phone);
                        }
                    }
                }
                else
                {
                    //WriteTxt("开始授权");
                    OAuth(AbsoluteUri);
                }
            }
        }
コード例 #3
0
        //导入卡券
        protected void btnImportCoupon_Click(object sender, EventArgs e)
        {
            XHYCouponService _xyhservice = new XHYCouponService();
            string           message     = "";
            string           url         = "";
            string           json        = "";
            string           cardid      = this.txtCardId.Text;

            string resmsg = NetHelper.HttpRequest(AppConfig.TokenUrl, "", "GET", 2000,
                                                  Encoding.UTF8, "application/json");

            TokeRes toke  = JsonHelper.DeserializeObject <TokeRes>(resmsg);
            string  token = toke.Access_Token;
            List <WXCouponNoInfo> list = _xyhservice.QueryWXCouponNoInfo(cardid);

            int    succ_cnt = 0;
            string codes    = "";

            foreach (WXCouponNoInfo model in list)
            {
                url  = "http://api.weixin.qq.com/card/code/deposit?access_token=" + token;
                json = @"{
                                   ""card_id"": """ + cardid + @""",
                                   ""code"": [
                                       """ + model.CouponNo + @"""
                                   ]
                                }";
                var resMessage = NetHelper.HttpRequest(url, json, "POST", 2000, Encoding.UTF8, "application/json");
                if (resMessage.Contains("succ_code")) //成功
                {
                    succ_cnt++;
                    _xyhservice.UpdateWXCouponNoInfoIsImport(model.id);
                }
                else
                {
                    message = resMessage + "<br>ID:" + model.id;
                }
                codes += "\"" + model.CouponNo + "\",";
            }
            codes = codes.Substring(0, codes.Length - 1);

            if (succ_cnt > 0)
            {
                //修改库存
                url  = "https://api.weixin.qq.com/card/modifystock?access_token=" + token;
                json = @"{""card_id"": """ + cardid + @""",""increase_stock_value"":" + succ_cnt + @",""reduce_stock_value"": 0}";
                var resMessage = NetHelper.HttpRequest(url, json, "POST", 2000, Encoding.UTF8, "application/json");
            }

            //核查code接口
            url     = "http://api.weixin.qq.com/card/code/checkcode?access_token=" + token;
            json    = @"{""card_id"": """ + cardid + @""",""code"":[" + codes + @"]}";
            message = NetHelper.HttpRequest(url, json, "POST", 2000, Encoding.UTF8, "application/json");

            this.txtImportCouponLog.Text = message;
        }
コード例 #4
0
        public bool HXCoupon(string code, string card_id)
        {
            XHYCouponService _xyhservice = new XHYCouponService();

            try
            {
                string resmsg = NetHelper.HttpRequest(AppConfig.TokenUrl, "", "GET", 2000,
                                                      Encoding.UTF8, "application/json");

                TokeRes toke  = JsonHelper.DeserializeObject <TokeRes>(resmsg);
                string  token = toke.Access_Token;
                string  url   = "https://api.weixin.qq.com/card/code/consume?access_token=" + token;
                string  json  = @"{
  ""code"": ""{0}"",
  ""card_id"": """ + card_id + @"""}";
                json = json.Replace("{0}", code);
                var resMessage = NetHelper.HttpRequest(url, json, "POST", 2000, Encoding.UTF8, "application/json");
                //{ "errcode":0,"errmsg":"ok","card":{                "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},"openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"}
                if (resMessage.Split('"')[5] == "ok")
                {
                    //_logwarn.Warn("核销成功!code=" + code + " ,card_id=" + card_id);

                    _xyhservice.AddLodInfo(new Tb_log()
                    {
                        title = "核销卡券", msgType = "message", msgContent = "核销成功!code=" + code + " ,card_id=" + card_id, createDate = DateTime.Now
                    });
                    return(true);
                }
                _xyhservice.AddLodInfo(new Tb_log()
                {
                    title = "核销卡券", msgType = "error", msgContent = "核销失败", createDate = DateTime.Now
                });
                return(false);
            }
            catch (Exception ex)
            {
                _xyhservice.AddLodInfo(new Tb_log()
                {
                    title = "核销异常", msgType = "exception", msgContent = "核销异常:" + ex.Message, createDate = DateTime.Now
                });
                return(false);
            }
        }