Exemplo n.º 1
0
        /// <summary>
        /// 获取js api ticket
        /// </summary>
        /// <returns>ticket</returns>
        private async Task <string> GetJsApiTicket()
        {
            string token = cache.GetString(JSAPI_TICKET__CACHE_KEY);

            if (string.IsNullOrEmpty(token))
            {
                var accessToken = await accessTokenSvc.GetToken();

                if (string.IsNullOrEmpty(accessToken))
                {
                    return(null);
                }

                var api    = string.Format(JSAPI_TICKET_API, accessToken);
                var ticket = await wxHelper.Send <JsApiTicket>(api, null, HttpMethod.Get);

                if (ticket != null && !ticket.HasError())
                {
                    string theTicket = ticket.ticket;

                    cache.SetString(JSAPI_TICKET__CACHE_KEY, theTicket, new DistributedCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(ticket.expires_in)
                    });

                    logger.LogInformation("获取JsApiTicket:{0}", theTicket);

                    return(ticket.ticket);
                }

                return(null);
            }

            return(token);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public async Task <WxError> SendTemplateMessage(WxTemplateMessage message)
        {
            string token = await accessTokenService.GetToken();

            string api    = string.Format(TEMPLATE_MESSAGE_API, token);
            var    result = await wxHelper.Send <WxError>(api, message);

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建二维码ticket, 临时二维码请求ticket
        /// </summary>
        private async Task <string> GetQrCodeTicket(int sceneId)
        {
            const int expire  = 10;
            var       api     = string.Format(TEMP_QR_CODE_TICKET_API, await wxAccessTokenService.GetToken());
            var       request = new QrCodeRequest()
            {
                expire_seconds = expire,
                action_name    = "QR_SCENE",
                action_info    = new QrCodeActionInfo()
                {
                    scene = new Scene()
                    {
                        scene_id = sceneId
                    }
                }
            };
            var response = await wxHelper.Send <QrCodeResponse>(api, request);

            if (response == null)
            {
                return(null);
            }

            #region 缓存场景id

            var key = string.Format(QR_CODE_SECENE, sceneId);

            cache.SetString(key, sceneId.ToString(), new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(expire)
            });

            #endregion

            return(response.ticket);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取token
        /// </summary>
        /// <returns></returns>
        public async Task <string> GetToken()
        {
            string token = cache.GetString(ACCESS_TOKEN_CACHE_KEY);

            if (string.IsNullOrEmpty(token))
            {
                var isDebug = config.IsDebug;

                // 调试模式,返回配置中的access token
                if (isDebug)
                {
                    var result = config.AccessToken;

                    if (!string.IsNullOrEmpty(result))
                    {
                        return(result);
                    }
                }

                var api = string.Format(ACCESS_TOKEN_API,
                                        accessTokenRequest.grant_type,
                                        accessTokenRequest.appid,
                                        accessTokenRequest.secret);
                var tokenResult = await wxHelper.Send <AccessTokenResponse>(api);

                if (tokenResult == null)
                {
                    return(null);
                }
                else
                {
                    cache.SetString(ACCESS_TOKEN_CACHE_KEY, tokenResult.access_token, new DistributedCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(tokenResult.expires_in)
                    });

                    logger.LogInformation("获取Token:{0}, 过期时间:{1}", tokenResult.access_token, tokenResult.expires_in);
                }

                return(tokenResult.access_token);
            }

            return(token);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">The message.</param>
        public async Task <WxError> Send(CustomerMessage message)
        {
            var api = string.Format(SEND_API, await tokenSvc.GetToken());

            return(await wxHelper.Send <WxError>(api, message));
        }