private RefreshAccessToken InternalRefreshToken(string authorizerAppId, string authorizerRefreshToken) { Func <string, RefreshAccessToken> get = accessToken => WeiXinHttpHelper.PostResultByJson <RefreshAccessToken>( "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + accessToken, new { component_appid = _accountModel.AppId, authorizer_appid = authorizerAppId, authorizer_refresh_token = authorizerRefreshToken }); try { return(get(GetAccessToken().AccessToken)); } catch (WeiXinException exception) { if (exception.ErrorCode == 40001 || exception.Message == "invalid credential, access_token is invalid or not latest") { return(get(GetAccessToken(true).AccessToken)); } throw; } }
/// <summary> /// 获取授权码。 /// </summary> /// <param name="ignoreCached">是否忽略缓存。</param> /// <returns>授权码结果。</returns> public AuthorizeCodeResult GetAuthorizeCode(bool ignoreCached = false) { Func <string, AuthorizeCodeResult> get = accessToken => WeiXinHttpHelper.PostResultByJson <AuthorizeCodeResult>( "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + accessToken, new { component_appid = _accountModel.AppId }); if (_authorizeCodeResult == null || _authorizeCodeResult.IsExpired() || ignoreCached) { AuthorizeCodeResult newModel; try { newModel = get(GetAccessToken().AccessToken); } catch (WeiXinException exception) { if (exception.ErrorCode == 40001 || exception.Message == "invalid credential, access_token is invalid or not latest") { newModel = get(GetAccessToken(true).AccessToken); } else { throw; } } if (_authorizeCodeResult != null && _authorizeCodeResult.AuthCode == newModel.AuthCode) { return(_authorizeCodeResult); } return(_authorizeCodeResult = newModel); } return(_authorizeCodeResult); }
/// <summary> /// 获取第三方平台的全局唯一票据。 /// </summary> /// <param name="ignoreCached">是否忽略缓存。</param> /// <returns>第三方用户唯一凭证密钥,即appsecret。</returns> public AccessTokenModel GetAccessToken(bool ignoreCached = false) { Func <AccessTokenModel> get = () => WeiXinHttpHelper.PostResultByJson <AccessTokenModel>( "https://api.weixin.qq.com/cgi-bin/component/api_component_token", new { component_appid = _accountModel.AppId, component_appsecret = _accountModel.AppSecret, component_verify_ticket = _accountModel.GetVerifyTicket() }); //是否需要重新获取(无效、忽略缓存、过期) Func <bool> needGet = () => _accessTokenModel == null || ignoreCached || _accessTokenModel.IsExpired(); if (needGet()) { lock (this) { if (needGet()) { /* var newModel = get(); * if (_accessTokenModel != null && _accessTokenModel.AccessToken == newModel.AccessToken) * return _accessTokenModel; * return _accessTokenModel = newModel;*/ return(_accessTokenModel = get()); } } } return(_accessTokenModel); }
/// <summary> /// 获取用户身上的标签列表 /// </summary> /// <param name="openId">粉丝OpenId</param> /// <returns>标签列表集合</returns> public List <int> GetIdList(string openId) { string url = $" https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token={_accountModel.GetAccessToken()}"; GetIdListModel result = WeiXinHttpHelper.PostResultByJson <GetIdListModel>(url, new { openid = openId }); return(result?.TagIds ?? new List <int>(0)); }
/// <summary> /// 创建标签 /// </summary> /// <param name="name">标签名(30个字符以内)</param> /// <returns>返回标签编号</returns> public TagInfo Create(string name) { string url = $"https://api.weixin.qq.com/cgi-bin/tags/create?access_token={_accountModel.GetAccessToken()}"; var result = WeiXinHttpHelper.PostResultByJson <TagWarp>(url, new { tag = new { name } }); return(result.Tag); }
/// <summary> /// 刷新授权公众号令牌。 /// </summary> /// <param name="authorizerAppId">授权方appid。</param> /// <param name="authorizerRefreshToken">授权方的刷新令牌,刷新令牌主要用于公众号第三方平台获取和刷新已授权用户的access_token,只会在授权时刻提供,请妥善保存。 一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。</param> /// <param name="ignoreCached">是否忽略缓存。</param> /// <returns>刷新令牌模型。</returns> public RefreshAccessToken RefreshToken(string authorizerAppId, string authorizerRefreshToken, bool ignoreCached = false) { Func <string, RefreshAccessToken> getFunc = accessToken => WeiXinHttpHelper.PostResultByJson <RefreshAccessToken>( "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + accessToken, new { component_appid = _accountModel.AppId, authorizer_appid = authorizerAppId, authorizer_refresh_token = authorizerRefreshToken }); var get = new Lazy <RefreshAccessToken>(() => { try { return(getFunc(GetAccessToken().AccessToken)); } catch (WeiXinException exception) { if (exception.ErrorCode == 40001 || exception.Message == "invalid credential, access_token is invalid or not latest") { return(getFunc(GetAccessToken(true).AccessToken)); } throw; } }); return(_objectCacheDictionary.AddOrUpdate("RefreshToken_" + authorizerAppId, k => get.Value, (k, v) => { var token = v as RefreshAccessToken; if (token == null || token.IsExpired() || ignoreCached) { var newModel = get.Value; if (token != null && token.AuthorizerAccessToken == newModel.AuthorizerAccessToken) { return v; } return get.Value; } return v; }) as RefreshAccessToken); }
/// <summary> /// 上传。 /// </summary> /// <typeparam name="TResult">结果类型。</typeparam> /// <param name="url">请求地址。</param> /// <param name="bytes">内容。</param> /// <param name="fieldName">文件名称。</param> /// <param name="func">其他字段数据。</param> /// <returns>结果。</returns> protected static TResult Upload <TResult>(string url, byte[] bytes, string fieldName, Func <CreateBytes, byte[][]> func = null) where TResult : class { var createBytes = new CreateBytes(); var list = new ArrayList { createBytes.CreateFieldData(fieldName, FileHelper.GetRandomFileName(bytes), FileHelper.GetContentType(bytes), bytes), }; if (func != null) { foreach (var b in func(createBytes)) { list.Add(b); } } var data = createBytes.JoinBytes(list); return(WeiXinHttpHelper.PostResultByJson <TResult>(url, data, createBytes.ContentType)); }
/// <summary> /// 批量查询卡列表。 /// </summary> /// <param name="skip">查询卡列表的起始偏移量,从0开始,即offset: 5是指从从列表里的第六个开始读取。</param> /// <param name="take">需要查询的卡片的数量(数量最大50)。</param> /// <param name="status">支持开发者拉出指定状态的卡券列表,例:仅拉出通过审核的卡券。</param> /// <returns>卡券信息。</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="take"/> 等于0或者大于50。</exception> public CardListResult GetCardList(int skip, short take, CardStatusEnum[] status = null) { if (take > 50 || take == 0) { throw new ArgumentOutOfRangeException("take", "查询的数量必须大于0小等于50。"); } var url = "https://api.weixin.qq.com/card/batchget?access_token=" + _accountModel.GetAccessToken(); object postData; if (status == null) { postData = new { offset = skip, count = take }; } else { postData = new { offset = skip, count = take, status_list = status.Select(GetStatusString).ToArray() }; } return(WeiXinHttpHelper.PostResultByJson <CardListResult>(url, postData)); }
/// <summary> /// 获取第三方平台的全局唯一票据。 /// </summary> /// <param name="ignoreCached">是否忽略缓存。</param> /// <returns>第三方用户唯一凭证密钥,即appsecret。</returns> public AccessTokenModel GetAccessToken(bool ignoreCached = false) { Func <AccessTokenModel> get = () => WeiXinHttpHelper.PostResultByJson <AccessTokenModel>( "https://api.weixin.qq.com/cgi-bin/component/api_component_token", new { component_appid = _accountModel.AppId, component_appsecret = _accountModel.AppSecret, component_verify_ticket = _accountModel.GetVerifyTicket() }); if (_accessTokenModel == null || _accessTokenModel.IsExpired() || ignoreCached) { var newModel = get(); if (_accessTokenModel != null && _accessTokenModel.AccessToken == newModel.AccessToken) { return(_accessTokenModel); } return(_accessTokenModel = newModel); } return(_accessTokenModel); }
/// <summary> /// 创建一个二维码。 /// </summary> /// <param name="model">创建二维码模型。</param> /// <returns>创建二维码的结果模型。</returns> public CreateQrCodeResultModel Create(CreateQrCodeModel model) { var url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + _accountModel.GetAccessToken(); return(WeiXinHttpHelper.PostResultByJson <CreateQrCodeResultModel>(url, model)); }
/// <summary> /// 获取永久素材列表。 /// </summary> /// <param name="filter">获取素材列表的筛选信息。</param> /// <returns>获取素材列表的结果模型。</returns> public GetMaterialListResultModel GetList(GetMaterialListFilter filter) { var url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + _accountModel.GetAccessToken(); return(WeiXinHttpHelper.PostResultByJson <GetMaterialListResultModel>(url, filter)); }
/// <summary> /// 获取标签下所有用户openid /// </summary> /// <param name="tagId">标签编号</param> /// <param name="nextOpenId">第一个拉取的openid, 默认重头开始</param> /// <returns>标签下粉丝列表</returns> public GetTagUserResultModel Get(int tagId, string nextOpenId = "") { string url = $"https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={_accountModel.GetAccessToken()}"; return(WeiXinHttpHelper.PostResultByJson <GetTagUserResultModel>(url, new { tagid = tagId, next_openid = nextOpenId })); }