Beispiel #1
0
        /// <summary>
        /// 获取JSApiTicket
        /// </summary>
        /// <param name="Now">当前日期时间</param>
        /// <param name="AppId">AppId为空时默认取配置文件appSettings节点key=WeChatAppId</param>
        /// <param name="access_token">访问令牌</param>
        /// <param name="GetJSApiTicketFromStorage">委托客户端从自定义存储库(接入者的DB、Cache、仓储源)获取JSApiTicket</param>
        /// <param name="UpdateJSApiTicketToStorage">委托客户端将JSApiTicket插入或更新到自定义存储库(接入者的DB、Cache、仓储源)</param>
        /// <returns></returns>
        public static JSApiTicketCacheModel GetJSApiTicket(DateTime Now, string AppId, string access_token, GetJSApiTicketFromStorage GetJSApiTicketFromStorage = null, UpdateJSApiTicketToStorage UpdateJSApiTicketToStorage = null)
        {
            if (string.IsNullOrWhiteSpace(access_token))
            {
                throw new ArgumentNullException(nameof(access_token));
            }
            AppId = string.IsNullOrWhiteSpace(AppId) ? Privacy.AppId : AppId;

            var ticketModel = GetJSApiTicketFromStorage?.Invoke(Now, AppId);

            if (CheckTicket(Now, ticketModel))
            {
                return(ticketModel);
            }

            ticketModel = GetTicketFromCache(AppId);
            if (CheckTicket(Now, ticketModel))
            {
                return(ticketModel);
            }

            var ticket = GetJSApiTicketFromWeChat(AppId, access_token);

            ticketModel            = new JSApiTicketCacheModel(ticket.expires_in);
            ticketModel.ticket     = ticket.ticket;
            ticketModel.expires_in = ticket.expires_in;
            ticketModel.appid      = AppId;

            UpdateTicketToCache(ticketModel);
            UpdateJSApiTicketToStorage?.Invoke(Now, ticketModel);

            return(ticketModel);
        }
Beispiel #2
0
 private static bool UpdateTicketToCache(JSApiTicketCacheModel Ticket)
 {
     if (null != Ticket)
     {
         CacheHelper.SetCache(key(Ticket.appid), Ticket, Ticket.expires_in / 60);
         return(true);
     }
     return(false);
 }
Beispiel #3
0
 /// <summary>
 /// 检查Ticket是否有效
 /// </summary>
 /// <param name="Now">当前日期时间</param>
 /// <param name="Ticket">JSApiTicketCacheModel</param>
 /// <returns></returns>
 public static bool CheckTicket(DateTime Now, JSApiTicketCacheModel Ticket)
 {
     if (null == Ticket)
     {
         return(false);
     }
     if (string.IsNullOrWhiteSpace(Ticket.ticket) || Ticket.expires_in < 1 || string.IsNullOrWhiteSpace(Ticket.expires_time))
     {
         return(false);
     }
     return(DateTime.Compare(Now.AddMinutes(5), DateTime.SpecifyKind(DateTime.Parse(Ticket.expires_time), Now.Kind)) < 0);
 }
Beispiel #4
0
        /// <summary>
        /// 用户自定义获取JSApiTicket
        /// </summary>
        /// <returns></returns>
        private static JSApiTicketCacheModel GetJSApiTicket()
        {
            JSSDK.GetJSApiTicketFromStorage get = delegate(DateTime Now, string AppId)
            {
                //根据AppId从自定义存储库(接入者的DB、Cache、仓储源)获取JSApiTicket

                /*
                 * return GetJSApiTicketByCache(AppId);
                 * return GetJSApiTicketByRedis(AppId);
                 * return GetJSApiTicketByMemcache(AppId);
                 * return GetJSApiTicketByMongodb(AppId);
                 * return GetJSApiTicketByDB(AppId);
                 * ……
                 */

                /***** 示例·开始 *****/
                JSApiTicketCacheModel Ticket = null;
                /* 先从自己系统的快速获取已存在的票据(Cache、Redis,Memcache等)*/
                object TokenFromCache = CacheHelper.GetCache("WeChatJSApiTicket_" + AppId);
                Ticket = null != TokenFromCache ? (JSApiTicketCacheModel)TokenFromCache : null;
                if (WeChatHelper4Net.JSSDK.CheckTicket(Now, Ticket))
                {
                    return(Ticket);
                }
                /* 如果快速数据没有,再从自己系统的稳定数据库获取已存在的票据(MySQL,SQLServer,Oracle等)*/
                //var TokenFromDB = BLLRepository.wechatManageBLL.GetTokenOrTicket(AppId, "JSApiTicket");
                //if(null != TokenFromDB)
                //{
                //    Ticket = new JSApiTicketCacheModel()
                //    {
                //        appid = TokenFromDB.appid,
                //        ticket = TokenFromDB.access_token,
                //        expires_in = TokenFromDB.expires_in,
                //        expires_time = TokenFromDB.expires_time,
                //        errcode = TokenFromDB.errcode,
                //        errmsg = TokenFromDB.errmsg
                //    };
                //    if(WeChatHelper4Net.JSSDK.CheckTicket(Now, Ticket))
                //    {
                //        CacheHelper.SetCache("WeChatJSApiTicket_" + Ticket.appid, Ticket, Ticket.expires_in / 60);
                //        return Ticket;
                //    }
                //}
                /***** 示例·结束 *****/

                return(null);
            };
            JSSDK.UpdateJSApiTicketToStorage update = delegate(DateTime now, JSApiTicketCacheModel Ticket)
            {
                bool result = false;

                //根据Ticket.appid将JSApiTicket插入或更新到自定义存储库(接入者的DB、Cache、仓储源)

                /*
                 * result = UpdateJSApiTicketToCache(Ticket.appid, Ticket);
                 * result = UpdateJSApiTicketToRedis(Ticket.appid, Ticket);
                 * result = UpdateJSApiTicketToMemcache(Ticket.appid, Ticket);
                 * result = UpdateJSApiTicketToMongodb(Ticket.appid, Ticket);
                 * result = UpdateJSApiTicketToDB(Ticket.appid, Ticket);
                 * ……
                 */

                /***** 示例·开始 *****/
                if (null != Ticket && !string.IsNullOrWhiteSpace(Ticket.appid) && Ticket.expires_in > 60)
                {
                    /* 先将票据存到自己系统的快速获取已存在的票据(Cache、Redis,Memcache等)*/
                    CacheHelper.SetCache("WeChatJSApiTicket_" + Ticket.appid, Ticket, Ticket.expires_in / 60);

                    //var entity = new TbWeChatTokenOrTicketModel()
                    //{
                    //    appid = Ticket.appid,
                    //    access_token = Ticket.ticket,
                    //    expires_in = Ticket.expires_in,
                    //    expires_time = Ticket.expires_time,
                    //    errcode = Ticket.errcode,
                    //    errmsg = Ticket.errmsg,
                    //    type = "JSApiTicket",
                    //    UpdateTime = DateTime.Now
                    //};
                    /* 为了避免快速数据丢失,再将票据存到自己系统的稳定数据库获取已存在的票据(MySQL,SQLServer,Oracle等)*/
                    //result = BLLRepository.wechatManageBLL.UpdateTokenOrTicket(entity);
                }
                /***** 示例·结束 *****/

                return(result);
            };


            string appId        = ConfigurationManager.AppSettings["WeChatAppId"].ToString(); //AppId为空时默认取配置文件appSettings节点key=WeChatAppId
            string access_token = WeChatTokenOrTicket.GetAccessToken().access_token;

            var ticket = JSSDK.GetJSApiTicket(DateTime.Now, appId, access_token, get, update);

            if (null != ticket)
            {
                return(ticket);
            }
            else
            {
                return(default(JSApiTicketCacheModel));
            }
        }