Example #1
0
        /// <summary>
        ///   获取公众号的AccessToken
        /// </summary>
        /// <returns></returns>
        public WxOffAccessTokenResp GetOffcialAccessToken()
        {
            var tokenResp = CacheUtil.Get <WxOffAccessTokenResp>(m_OffcialAccessTokenKey, ModuleNames.SnsCenter);

            if (tokenResp == null || tokenResp.expires_date < DateTime.Now)
            {
                OsHttpRequest req = new OsHttpRequest();

                req.AddressUrl = $"{m_ApiUrl}/cgi-bin/token?grant_type=client_credential&appid={ApiConfig.AppId}&secret={ApiConfig.AppSecret}";
                req.HttpMothed = HttpMothed.GET;

                tokenResp = RestCommon <WxOffAccessTokenResp>(req);

                if (!tokenResp.IsSuccess)
                {
                    return(tokenResp);
                }

                tokenResp.expires_date = DateTime.Now.AddSeconds(tokenResp.expires_in - 600);

                CacheUtil.AddOrUpdate(m_OffcialAccessTokenKey, tokenResp, TimeSpan.FromSeconds(tokenResp.expires_in), null, ModuleNames.SnsCenter);
            }

            return(tokenResp);
        }
Example #2
0
        /// <summary>
        ///   获取公众号的AccessToken
        /// </summary>
        /// <returns></returns>
        public async Task <WxOffAccessTokenResp> GetAccessTokenAsync()
        {
            var tokenResp = CacheUtil.Get <WxOffAccessTokenResp>(m_OffcialAccessTokenKey, ModuleNames.SocialCenter);

            if (tokenResp != null && tokenResp.expires_date >= DateTime.Now)
            {
                return(tokenResp);
            }

            var req = new OsHttpRequest
            {
                AddressUrl =
                    $"{m_ApiUrl}/cgi-bin/token?grant_type=client_credential&appid={ApiConfig.AppId}&secret={ApiConfig.AppSecret}",
                HttpMothed = HttpMothed.GET
            };

            tokenResp = await RestCommonJson <WxOffAccessTokenResp>(req);

            if (!tokenResp.IsSuccess())
            {
                return(tokenResp);
            }

            tokenResp.expires_date = DateTime.Now.AddSeconds(tokenResp.expires_in - 600);

            CacheUtil.AddOrUpdate(m_OffcialAccessTokenKey, tokenResp, TimeSpan.FromSeconds(tokenResp.expires_in),
                                  null, ModuleNames.SocialCenter);
            return(tokenResp);
        }
Example #3
0
        /// <summary>
        ///  获取js 接口 Ticket
        /// 内部已经处理缓存
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task <WxGetJsTicketResp> GetJsTicketAsync(WxJsTicketType type)
        {
            string key    = string.Format(WxCacheKeysUtil.OffcialJsTicketKey, ApiConfig.AppId, type);
            var    ticket = CacheUtil.Get <WxGetJsTicketResp>(key, ModuleNames.SocialCenter);

            if (ticket != null && ticket.expires_time > DateTime.Now)
            {
                return(ticket);
            }

            var req = new OsHttpRequest();

            req.HttpMothed = HttpMothed.GET;
            req.AddressUrl = string.Concat(m_ApiUrl, string.Concat("cgi-bin/ticket/getticket?type=", type.ToString()));

            var ticketRes = await RestCommonOffcialAsync <WxGetJsTicketResp>(req);

            if (ticketRes.IsSuccess())
            {
                ticketRes.expires_time = DateTime.Now.AddSeconds(ticketRes.expires_in);
                CacheUtil.AddOrUpdate(key, ticketRes, TimeSpan.FromSeconds(ticketRes.expires_in - 10), null,
                                      ModuleNames.SocialCenter);
            }
            return(ticketRes);
        }
Example #4
0
        public static async Task <ResultMo <UserInfoMo> > GetCurrentUser()
        {
            var user = CacheUtil.Get <UserInfoMo>(CacheKeysUtil.CurrentUserInfo);

            if (user != null)
            {
                return(new ResultMo <UserInfoMo>(user));
            }

            var userRes = await ApiUtil.PostApi <ResultMo <UserInfoMo> >("/member/GetCurrentUser");

            if (!userRes.IsSuccess())
            {
                return(userRes.ConvertToResultOnly <UserInfoMo>());
            }

            CacheUtil.AddOrUpdate(CacheKeysUtil.CurrentUserInfo, userRes.data, TimeSpan.Zero,
                                  DateTime.Now.AddHours(CacheKeysUtil.CurrentUserInfoHours));

            return(userRes);
        }
Example #5
0
        /// <summary>
        ///   获取公众号的AccessToken
        ///     【首先从缓存中获取,如果没有再从远程获取】
        /// </summary>
        /// <returns></returns>
        public virtual async Task <WxOffAccessTokenResp> GetAccessTokenFromCacheAsync()
        {
            if (ApiConfig.OperateMode == AppOperateMode.ByAgent)
            {
                var atoken = WxOfficialConfigProvider.AccessTokenFromAgentFunc?.Invoke(ApiConfig);
                if (string.IsNullOrEmpty(atoken))
                {
                    throw new ArgumentNullException("access_token",
                                                    "access_token值未发现,请检查 WxOfficialConfigProvider 下 AccessTokenFromAgentFunc 委托是否为空或者返回值不正确!");
                }
                return(new WxOffAccessTokenResp()
                {
                    access_token = atoken
                });
            }

            var m_OffcialAccessTokenKey = string.Format(WxCacheKeysUtil.OffcialAccessTokenKey, ApiConfig.AppId);
            var tokenResp = CacheUtil.Get <WxOffAccessTokenResp>(m_OffcialAccessTokenKey, ModuleName);

            if (tokenResp != null && tokenResp.expires_date >= DateTime.Now.ToUtcSeconds())
            {
                return(tokenResp);
            }

            tokenResp = await GetAccessTokenFromWxAsync();

            if (!tokenResp.IsSuccess())
            {
                return(tokenResp);
            }

            tokenResp.expires_date = DateTime.Now.ToUtcSeconds() + tokenResp.expires_in - 600;

            CacheUtil.AddOrUpdate(m_OffcialAccessTokenKey, tokenResp, TimeSpan.FromSeconds(tokenResp.expires_in - 600),
                                  null, ModuleName);

            return(tokenResp);
        }
Example #6
0
        /// <summary>
        ///   获取公众号的AccessToken
        ///     【首先从缓存中获取,如果没有再从远程获取】
        /// </summary>
        /// <returns></returns>
        public async Task <WxGetAgentAccessTokenResp> GetAgentAccessTokenFromCacheAsync()
        {
            var m_OffcialAccessTokenKey = string.Format(WxCacheKeysUtil.OffcialAgentAccessTokenKey, ApiConfig.AppId);
            var tokenResp = CacheUtil.Get <WxGetAgentAccessTokenResp>(m_OffcialAccessTokenKey, ModuleName);

            if (tokenResp != null && tokenResp.expires_date >= DateTime.Now.ToUtcSeconds())
            {
                return(tokenResp);
            }

            tokenResp = await GetAgentAccessTokenFromWxAsync();

            if (!tokenResp.IsSuccess())
            {
                return(tokenResp);
            }

            tokenResp.expires_date = DateTime.Now.ToUtcSeconds() + tokenResp.expires_in - 600;

            CacheUtil.AddOrUpdate(m_OffcialAccessTokenKey, tokenResp, TimeSpan.FromSeconds(tokenResp.expires_in - 600),
                                  null, ModuleName);
            return(tokenResp);
        }
Example #7
0
        /// <summary>
        ///  获取js 接口 Ticket
        /// 内部已经处理缓存
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task <WxGetJsTicketResp> GetJsTicketFromCacheAsync(WxJsTicketType type)
        {
            var key = string.Format(WxCacheKeysUtil.OffcialJsTicketKey, ApiConfig.AppId, type);

            var ticket = CacheUtil.Get <WxGetJsTicketResp>(key, ModuleName);

            if (ticket != null && ticket.expires_time > DateTime.Now)
            {
                return(ticket);
            }

            var ticketRes = await GetJsTicketFromWxAsync(type);

            if (!ticketRes.IsSuccess())
            {
                return(ticketRes);
            }

            ticketRes.expires_time = DateTime.Now.AddSeconds(ticketRes.expires_in);

            CacheUtil.AddOrUpdate(key, ticketRes, TimeSpan.FromSeconds(ticketRes.expires_in - 10), null,
                                  ModuleName);
            return(ticketRes);
        }