CommonApi.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:CommonApi.cs
  17. 文件功能描述:通用接口(用于和微信服务器通讯,一般不涉及自有网站服务器的通讯)
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150330
  22. 修改描述:获取调用微信JS接口的临时票据中的AccessToken添加缓存
  23. 修改标识:Senparc - 20150401
  24. 修改描述:添加公众号第三方平台获取授权码接口
  25. 修改标识:Senparc - 20150430
  26. 修改描述:公众号第三方平台分离
  27. 修改标识:Senparc - 20160721
  28. 修改描述:增加其接口的异步方法
  29. 修改标识:Senparc - 20161110
  30. 修改描述:完善GetTicket系列方法备注
  31. 修改标识:Senparc - 20170707
  32. 修改描述:v14.5.1 完善异步方法async/await
  33. 修改标识:Senparc - 20180928
  34. 修改描述:添加Clear_quota
  35. ----------------------------------------------------------------*/
  36. /*
  37. API:http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3&oldid=103
  38. */
  39. using System.Threading.Tasks;
  40. using Senparc.CO2NET.Extensions;
  41. using Senparc.CO2NET.HttpUtility;
  42. using Senparc.NeuChar;
  43. using Senparc.Weixin.CommonAPIs;
  44. using Senparc.Weixin.Entities;
  45. using Senparc.Weixin.MP.Containers;
  46. using Senparc.Weixin.MP.Entities;
  47. namespace Senparc.Weixin.MP.CommonAPIs
  48. {
  49. /// <summary>
  50. /// 通用接口
  51. /// 通用接口用于和微信服务器通讯,一般不涉及自有网站服务器的通讯
  52. /// </summary>
  53. public partial class CommonApi
  54. {
  55. #region 同步方法
  56. /// <summary>
  57. /// 获取凭证接口
  58. /// </summary>
  59. /// <param name="grant_type">获取access_token填写client_credential</param>
  60. /// <param name="appid">第三方用户唯一凭证</param>
  61. /// <param name="secret">第三方用户唯一凭证密钥,既appsecret</param>
  62. /// <returns></returns>
  63. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetToken", true)]
  64. public static AccessTokenResult GetToken(string appid, string secret, string grant_type = "client_credential")
  65. {
  66. //注意:此方法不能再使用ApiHandlerWapper.TryCommonApi(),否则会循环
  67. var url = string.Format(Config.ApiMpHost + "/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
  68. grant_type.AsUrlData(), appid.AsUrlData(), secret.AsUrlData());
  69. AccessTokenResult result = Get.GetJson<AccessTokenResult>(url);//此处为最原始接口,不再使用重试获取的封装
  70. return result;
  71. }
  72. /// <summary>
  73. /// 用户信息接口
  74. /// </summary>
  75. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  76. /// <param name="openId"></param>
  77. /// <returns></returns>
  78. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetUserInfo", true)]
  79. public static WeixinUserInfoResult GetUserInfo(string accessTokenOrAppId, string openId)
  80. {
  81. return ApiHandlerWapper.TryCommonApi(accessToken =>
  82. {
  83. var url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info?access_token={0}&openid={1}",
  84. accessToken.AsUrlData(), openId.AsUrlData());
  85. WeixinUserInfoResult result = CommonJsonSend.Send<WeixinUserInfoResult>(null, url, null, CommonJsonSendType.GET);
  86. return result;
  87. }, accessTokenOrAppId);
  88. }
  89. /// <summary>
  90. /// 获取调用微信JS接口的临时票据
  91. /// </summary>
  92. /// <param name="appId"></param>
  93. /// <param name="secret"></param>
  94. /// <param name="type">默认为jsapi,当作为卡券接口使用时,应当为wx_card</param>
  95. /// <returns></returns>
  96. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetTicket", true)]
  97. public static JsApiTicketResult GetTicket(string appId, string secret, string type = "jsapi")
  98. {
  99. var accessToken = AccessTokenContainer.TryGetAccessToken(appId, secret);
  100. return GetTicketByAccessToken(accessToken, type);
  101. }
  102. /// <summary>
  103. /// 获取调用微信JS接口的临时票据
  104. /// </summary>
  105. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  106. /// <param name="type">默认为jsapi,当作为卡券接口使用时,应当为wx_card</param>
  107. /// <returns></returns>
  108. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetTicketByAccessToken", true)]
  109. public static JsApiTicketResult GetTicketByAccessToken(string accessTokenOrAppId, string type = "jsapi")
  110. {
  111. return ApiHandlerWapper.TryCommonApi(accessToken =>
  112. {
  113. var url = string.Format(Config.ApiMpHost + "/cgi-bin/ticket/getticket?access_token={0}&type={1}",
  114. accessToken.AsUrlData(), type.AsUrlData());
  115. JsApiTicketResult result = CommonJsonSend.Send<JsApiTicketResult>(null, url, null, CommonJsonSendType.GET);
  116. return result;
  117. }, accessTokenOrAppId);
  118. }
  119. /// <summary>
  120. /// 获取微信服务器的ip段
  121. /// </summary>
  122. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  123. /// <returns></returns>
  124. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetCallBackIp", true)]
  125. public static GetCallBackIpResult GetCallBackIp(string accessTokenOrAppId)
  126. {
  127. return ApiHandlerWapper.TryCommonApi(accessToken =>
  128. {
  129. var url = string.Format(Config.ApiMpHost + "/cgi-bin/getcallbackip?access_token={0}", accessToken.AsUrlData());
  130. return CommonJsonSend.Send<GetCallBackIpResult>(null, url, null, CommonJsonSendType.GET);
  131. }, accessTokenOrAppId);
  132. }
  133. /// <summary>
  134. ///公众号调用或第三方平台帮公众号调用对公众号的所有api调用(包括第三方帮其调用)次数进行清零
  135. /// </summary>
  136. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  137. /// <param name="appId"></param>
  138. /// <param name="timeOut"></param>
  139. /// <returns></returns>
  140. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CardApi.Clear_quota", true)]
  141. public static WxJsonResult Clear_quota(string accessTokenOrAppId, string appId, int timeOut = Config.TIME_OUT)
  142. {
  143. return ApiHandlerWapper.TryCommonApi(accessToken =>
  144. {
  145. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/clear_quota?access_token={0}", accessToken.AsUrlData());
  146. var data = new
  147. {
  148. appid = appId
  149. };
  150. return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, timeOut: timeOut);
  151. }, accessTokenOrAppId);
  152. }
  153. #endregion
  154. #if !NET35 && !NET40
  155. #region 异步方法
  156. /// <summary>
  157. /// 【异步方法】获取凭证接口
  158. /// </summary>
  159. /// <param name="grant_type">获取access_token填写client_credential</param>
  160. /// <param name="appid">第三方用户唯一凭证</param>
  161. /// <param name="secret">第三方用户唯一凭证密钥,既appsecret</param>
  162. /// <returns></returns>
  163. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetTokenAsync", true)]
  164. public static async Task<AccessTokenResult> GetTokenAsync(string appid, string secret, string grant_type = "client_credential")
  165. {
  166. //注意:此方法不能再使用ApiHandlerWapper.TryCommonApi(),否则会循环
  167. var url = string.Format(Config.ApiMpHost + "/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
  168. grant_type.AsUrlData(), appid.AsUrlData(), secret.AsUrlData());
  169. AccessTokenResult result = await Get.GetJsonAsync<AccessTokenResult>(url);//此处为最原始接口,不再使用重试获取的封装
  170. return result;
  171. }
  172. /// <summary>
  173. /// 【异步方法】用户信息接口
  174. /// </summary>
  175. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  176. /// <param name="openId"></param>
  177. /// <returns></returns>
  178. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetUserInfoAsync", true)]
  179. public static async Task<WeixinUserInfoResult> GetUserInfoAsync(string accessTokenOrAppId, string openId)
  180. {
  181. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  182. {
  183. var url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info?access_token={0}&openid={1}",
  184. accessToken.AsUrlData(), openId.AsUrlData());
  185. var result = CommonJsonSend.SendAsync<WeixinUserInfoResult>(null, url, null, CommonJsonSendType.GET);
  186. return await result;
  187. }, accessTokenOrAppId);
  188. }
  189. /// <summary>
  190. /// 【异步方法】获取调用微信JS接口的临时票据
  191. /// </summary>
  192. /// <param name="appId"></param>
  193. /// <param name="secret"></param>
  194. /// <param name="type">默认为jsapi,当作为卡券接口使用时,应当为wx_card</param>
  195. /// <returns></returns>
  196. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetTicketAsync", true)]
  197. public static async Task<JsApiTicketResult> GetTicketAsync(string appId, string secret, string type = "jsapi")
  198. {
  199. var accessToken = await AccessTokenContainer.TryGetAccessTokenAsync(appId, secret);
  200. return GetTicketByAccessToken(accessToken, type);
  201. }
  202. /// <summary>
  203. /// 【异步方法】获取调用微信JS接口的临时票据
  204. /// </summary>
  205. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  206. /// <param name="type">默认为jsapi,当作为卡券接口使用时,应当为wx_card</param>
  207. /// <returns></returns>
  208. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetTicketByAccessTokenAsync", true)]
  209. public static async Task<JsApiTicketResult> GetTicketByAccessTokenAsync(string accessTokenOrAppId, string type = "jsapi")
  210. {
  211. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  212. {
  213. var url = string.Format(Config.ApiMpHost + "/cgi-bin/ticket/getticket?access_token={0}&type={1}",
  214. accessToken.AsUrlData(), type.AsUrlData());
  215. var result = CommonJsonSend.SendAsync<JsApiTicketResult>(null, url, null, CommonJsonSendType.GET);
  216. return await result;
  217. }, accessTokenOrAppId);
  218. }
  219. /// <summary>
  220. /// 【异步方法】获取微信服务器的ip段
  221. /// </summary>
  222. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  223. /// <returns></returns>
  224. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetCallBackIpAsync", true)]
  225. public static async Task<GetCallBackIpResult> GetCallBackIpAsync(string accessTokenOrAppId)
  226. {
  227. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  228. {
  229. var url = string.Format(Config.ApiMpHost + "/cgi-bin/getcallbackip?access_token={0}", accessToken.AsUrlData());
  230. return await CommonJsonSend.SendAsync<GetCallBackIpResult>(null, url, null, CommonJsonSendType.GET);
  231. }, accessTokenOrAppId);
  232. }
  233. /// <summary>
  234. /// 【异步方法】公众号调用或第三方平台帮公众号调用对公众号的所有api调用(包括第三方帮其调用)次数进行清零
  235. /// </summary>
  236. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  237. /// <param name="appId"></param>
  238. /// <param name="timeOut"></param>
  239. /// <returns></returns>
  240. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CardApi.Clear_quotaAsync", true)]
  241. public static async Task<WxJsonResult> Clear_quotaAsync(string accessTokenOrAppId, string appId, int timeOut = Config.TIME_OUT)
  242. {
  243. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  244. {
  245. var urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/clear_quota?access_token={0}", accessToken.AsUrlData());
  246. var data = new
  247. {
  248. appid = appId
  249. };
  250. return await CommonJsonSend.SendAsync<WxJsonResult>(null, urlFormat, data, timeOut: timeOut);
  251. }, accessTokenOrAppId);
  252. }
  253. #endregion
  254. #endif
  255. }
  256. }