/// <summary> /// 获取js api ticket /// </summary> /// <returns>ticket</returns> public string GetJsApiTicket() { var token = cacheMgr.Get(JSAPI_TICKET__CACHE_KEY, 0, () => { var api = string.Format(JSAPI_TICKET_API, accessTokenSvc.GetToken()); var ticket = WxHelper.Send <JsApiTicket>(api, null, HttpMethod.Get); if (ticket != null) { if (cacheMgr.IsSet(JSAPI_TICKET__CACHE_KEY)) { cacheMgr.Remove(JSAPI_TICKET__CACHE_KEY); } string theTicket = ticket.ticket; cacheMgr.Set(JSAPI_TICKET__CACHE_KEY, theTicket, ticket.expires_in / 60); Log.Info(string.Format("获取JsApiTicket:{0}", theTicket)); return(ticket.ticket); } return(null); }); return(token); }
/// <summary> /// 发送模板消息 /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public WxError SendTemplateMessage(WxTemplateMessage message) { string api = string.Format(TEMPLATE_MESSAGE_API, accessTokenSvc.GetToken()); var result = WxHelper.Send <WxError>(api, message); return(result); }
/// <summary> /// 获取Access Token /// </summary> /// <param name="code">The code.</param> /// <returns></returns> public static WebAuthAccessTokenResponse GetAccessToken(string code) { var request = new WebAuthAccessTokenRequest(code); var requestUrl = string.Format(ACCESS_TOKEN_API , WebAuthAccessTokenRequest.appid , WebAuthAccessTokenRequest.secret , request.code , WebAuthAccessTokenRequest.grant_type); var response = WxHelper.Send <WebAuthAccessTokenResponse>(requestUrl); return(response); }
/// <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 bool IsSubscribe(string openId) { if (string.IsNullOrEmpty(openId)) { return(false); } var api = string.Format(USER_INFO_API, accessTokenSvc.GetToken(), openId); var userInfo = WxHelper.Send <UserInfo>(api, null, HttpMethod.Get); Log.Debug($"获取用户信息:{userInfo?.ToJson()}"); return(userInfo != null && userInfo.subscribe == 1); }
/// <summary> /// 获取token /// </summary> /// <returns></returns> public string GetToken() { var token = cacheMgr.Get(ACCESS_TOKEN_CACHE_KEY, 0, () => { var isDebug = WxConfig.IsDebug; if (isDebug) { var result = WxConfig.WxAccessToken; if (!string.IsNullOrEmpty(result)) { return(result); } } var request = new AccessTokenRequest(); var api = string.Format(ACCESS_TOKEN_API, request.grant_type, request.appid, request.secret); var tokenResult = WxHelper.Send <AccessTokenResponse>(api); if (tokenResult == null) { return(null); } else { if (cacheMgr.IsSet(ACCESS_TOKEN_CACHE_KEY)) { cacheMgr.Remove(ACCESS_TOKEN_CACHE_KEY); } cacheMgr.Set(ACCESS_TOKEN_CACHE_KEY, tokenResult.access_token, tokenResult.expires_in / 60); Log.Info(string.Format("获取Token:{0}, 过期时间:{1}", tokenResult.access_token, tokenResult.expires_in)); } return(tokenResult.access_token); }); return(token); }
/// <summary> /// 创建二维码ticket, 临时二维码请求ticket /// </summary> public string GetQrCodeTicket(int sceneId) { const int expire = 10; var api = string.Format(TEMP_QR_CODE_TICKET_API, accessTokenSvc.GetToken()); var request = new QrCodeRequest() { expire_seconds = expire, action_name = "QR_SCENE", action_info = new QrCodeActionInfo() { scene = new Scene() { scene_id = sceneId } } }; var response = WxHelper.Send <QrCodeResponse>(api, request); if (response == null) { return(null); } #region 缓存场景id var key = string.Format(QR_CODE_SECENE, sceneId); if (cachMgr.IsSet(key)) { cachMgr.Remove(key); } cachMgr.Set(key, sceneId, expire); #endregion return(response.ticket); }
/// <summary> /// 获取用户信息 /// </summary> /// <param name="code">The code.</param> /// <returns></returns> public static WxMessage <UserInfoResponse> GetUserInfo(string code) { var result = new WxMessage <UserInfoResponse>(); var accessTokenResp = GetAccessToken(code); //if (accessTokenResp == null) //{ // accessTokenResp = GetAccessToken(code); // if (string.IsNullOrEmpty(accessTokenResp.errcode) || accessTokenResp.errcode == "0") // { // cacheMgr.Set(CACHE_KEY_AUTH_TOKEN, accessTokenResp, accessTokenResp.expires_in / 60); // } //} if (accessTokenResp == null || !string.IsNullOrEmpty(accessTokenResp.errcode)) { result.Data = new UserInfoResponse { errcode = accessTokenResp?.errcode, errmsg = accessTokenResp?.errmsg }; result.AddError("获取AccessToken失败"); return(result); } var requestUrl = string.Format(USER_INFO_API, accessTokenResp.access_token, accessTokenResp.openid); var response = WxHelper.Send <UserInfoResponse>(requestUrl); result.Data = response; return(result); }
/// <summary> /// 发送消息 /// </summary> /// <param name="message">The message.</param> public WxError Send(CustomerMessage message) { var api = string.Format(SEND_API, accessTokenSvc.GetToken()); return(WxHelper.Send <WxError>(api, message)); }