예제 #1
0
        public async Task <StrResp> GetAccessToken(IAppSecret appConfig)
        {
            var key            = string.Concat(_AccessCacheKey, appConfig);
            var accessTokenRes = await CacheHelper.GetAsync <WechatAccessTokenResp>(key);

            if (accessTokenRes != null)
            {
                return(new StrResp(accessTokenRes.access_token));
            }

            accessTokenRes = await new WechatAccessTokenReq(appConfig).ExecuteAsync();
            if (!accessTokenRes.IsSuccess())
            {
                return(new StrResp().WithResp(accessTokenRes));
            }

            await CacheHelper.SetAbsoluteAsync(key, accessTokenRes,
                                               TimeSpan.FromSeconds(accessTokenRes.expires_in - 60 * 5), "OSS.Clients.Platform.Wechat");// 按照返回的过期时间提前5分钟过期

            return(new StrResp(accessTokenRes.access_token));
        }
예제 #2
0
        public async Task <StrResp> GetJsTicket(WechatJsTicketType type, IAppSecret appConfig)
        {
            var key         = string.Concat(jsTicketCacheKey, appConfig.app_id);
            var jsTicketRes = await CacheHelper.GetAsync <WechatJsTicketResp>(key);

            if (jsTicketRes != null)
            {
                return(new StrResp(jsTicketRes.ticket));
            }

            jsTicketRes = await new WechatJsTicketReq(type)
                          .SetContextConfig(appConfig)
                          .ExecuteAsync();

            if (!jsTicketRes.IsSuccess())
            {
                return(new StrResp().WithResp(jsTicketRes));
            }

            // 按照返回的过期时间提前5分钟过期
            await CacheHelper.SetAbsoluteAsync(key, jsTicketRes, TimeSpan.FromSeconds(jsTicketRes.expires_in - 60 * 5), "OSS.Clients.Platform.Wechat");

            return(new StrResp(jsTicketRes.ticket));
        }
예제 #3
0
 /// <summary>
 ///  设置当前请求对应的秘钥配置信息
 /// </summary>
 /// <param name="req"></param>
 /// <param name="appConfig"></param>
 /// <returns></returns>
 public static TReq SetContextConfig <TReq>(this TReq req, IAppSecret appConfig)
     where TReq : WechatBaseReq
 {
     req.app_config = appConfig;
     return(req);
 }
예제 #4
0
 //private IAppSecret m_appConfig;
 public WechatAccessTokenReq(IAppSecret appconfig) : base(HttpMethod.Get)
 {
     this.app_config = appconfig;
 }