예제 #1
0
        /// <summary>
        /// 获取全局JS唯一票据
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public WXAccessTokenCache ApiTicket(string access_token)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                WXAccessTokenCache ticket = s.Get <WXAccessTokenCache>(" where type = @0 and id != @1 ", "jsapi_ticket", 0);
                if (ticket == null || ticket.expires_time < DateTime.Now)
                {
                    if (ticket == null)
                    {
                        ticket = new WXAccessTokenCache();
                    }

                    //从API获取jsapi_ticket
                    JsApiTicket jsapi_ticket = WeChatJsApiHelper.GetJsApiTicket(access_token);

                    ticket.type  = "jsapi_ticket";
                    ticket.token = jsapi_ticket.ticket;
                    //提前600秒更新token
                    ticket.expires_time = (DateTime)DateTime.Now.AddSeconds(jsapi_ticket.expires_in -= 600);

                    if (ticket == null)
                    {
                        s.Insert <WXAccessTokenCache>(ticket);
                    }
                    else
                    {
                        s.Update <WXAccessTokenCache>(ticket);
                    }
                }
                return(ticket);
            }
        }
 /// <summary>
 /// 获取js
 /// </summary>
 /// <param name="access_token"></param>
 /// <returns></returns>
 public WXJsApiTicketCache Get(string access_token)
 {
     using (ISession s = SessionFactory.Instance.CreateSession())
     {
         WXJsApiTicketCache jsapiTicketCache = s.Get <WXJsApiTicketCache>(" where id != 0 ");
         if (jsapiTicketCache == null || jsapiTicketCache.expires_time < DateTime.Now)
         {
             WeiFos.WeChat.TickeModule.JsApiTicket jsapi_ticket = WeChatJsApiHelper.GetJsApiTicket(access_token);
             jsapi_ticket.expires_in -= 600; //提前600秒更新token
             if (jsapiTicketCache == null)
             {
                 jsapiTicketCache              = new WXJsApiTicketCache();
                 jsapiTicketCache.ticket       = jsapi_ticket.ticket;
                 jsapiTicketCache.expires_time = (DateTime)DateTime.Now.AddSeconds(jsapi_ticket.expires_in);
                 s.Insert <WXJsApiTicketCache>(jsapiTicketCache);
             }
             else
             {
                 jsapiTicketCache.ticket       = jsapi_ticket.ticket;
                 jsapiTicketCache.expires_time = (DateTime)DateTime.Now.AddSeconds(jsapi_ticket.expires_in);
                 s.Update <WXJsApiTicketCache>(jsapiTicketCache);
             }
         }
         return(jsapiTicketCache);
     }
 }
예제 #3
0
        /// <summary>
        /// 通过access_token获取 jsApiTicket
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public JsApiTicket GetWXOpenJsApiTicket(string token)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                //当前预授权码对象
                JsApiTicket jsApiTicket = null;

                //当前凭据参数是否存在
                WXOpenAuth entity = s.Get <WXOpenAuth>("where auth_key = @0 ", WXOpenAuthKey.wx_open_jsApi_ticket.ToString());
                if (entity == null)
                {
                    entity = new WXOpenAuth();
                }
                else
                {
                    jsApiTicket = JsonConvert.DeserializeObject <JsApiTicket>(entity.val);
                }

                //获取授权票据
                if (entity.id == 0 || (jsApiTicket != null && entity.update_time.AddSeconds(jsApiTicket.expires_in) < DateTime.Now))
                {
                    jsApiTicket = WeChatJsApiHelper.GetJsApiTicket(token);
                    if (jsApiTicket.errcode != 0)
                    {
                        string er = "开放平台接口,通过access_token获取jsApiTicket[GetWXOpenJsApiTicket]==>" + JsonConvert.SerializeObject(jsApiTicket);
                        s.Insert(new APILogs()
                        {
                            type = 1, created_date = DateTime.Now, content = er
                        });
                    }
                    else
                    {
                        //提前2分钟刷新
                        jsApiTicket.expires_in -= 600;
                        entity.update_time      = DateTime.Now;
                        entity.auth_key         = WXOpenAuthKey.wx_open_jsApi_ticket.ToString();
                        entity.val              = JsonConvert.SerializeObject(jsApiTicket);

                        if (entity.id == 0)
                        {
                            s.Insert(entity);
                        }
                        else
                        {
                            s.Update(entity);
                        }
                    }
                }

                return(jsApiTicket);
            }
        }