Ejemplo n.º 1
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);
                 * ……
                 */

                return(null);
            };
            JSSDK.UpdateJSApiTicketToStorage update = delegate(DateTime now, JSApiTicketCacheModel Ticket)
            {
                //根据Ticket.appid将JSApiTicket插入或更新到自定义存储库(接入者的DB、Cache、仓储源)

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

                return(true);
            };


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

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

            if (null != ticket)
            {
                return(ticket);
            }
            else
            {
                return(default(JSApiTicketCacheModel));
            }
        }
Ejemplo n.º 2
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));
            }
        }