/// <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); }
/// <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); }
/// <summary> /// 是否订阅 /// </summary> /// <param name="openId">The open identifier.</param> /// <returns> /// <c>true</c> if the specified open identifier is subscribe; otherwise, <c>false</c>. /// </returns> public async Task <bool> IsSubscribe(string openId) { if (string.IsNullOrEmpty(openId)) { return(false); } var token = await accessTokenSvc.GetToken(); var api = string.Format(USER_INFO_API, token, openId); var userInfo = await wxHelper.Send <UserInfo>(api, null, HttpMethod.Get); logger.LogInformation($"获取用户信息:{userInfo?.ToJson()}"); return(userInfo != null && userInfo.subscribe == 1); }
/// <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); }
/// <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)); }