UserApi.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. 文件名:UserAPI.cs
  17. 文件功能描述:用户接口
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:jsionr - 20150322
  22. 修改描述:添加修改关注者备注信息接口
  23. 修改标识:Senparc - 20150325
  24. 修改描述:修改关注者备注信息开放代理请求超时时间
  25. 修改标识:Senparc - 20160719
  26. 修改描述:增加其接口的异步方法
  27. 修改标识:Senparc - 20170707
  28. 修改描述:v14.5.1 完善异步方法async/await
  29. 修改标识:Senparc - 20190129
  30. 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
  31. ----------------------------------------------------------------*/
  32. /*
  33. 接口详见:http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF
  34. */
  35. using System.Collections.Generic;
  36. using System.Threading.Tasks;
  37. using Senparc.Weixin.Entities;
  38. using Senparc.Weixin.MP.AdvancedAPIs.User;
  39. using Senparc.Weixin.MP.CommonAPIs;
  40. using Senparc.Weixin.HttpUtility;
  41. using Senparc.CO2NET.Extensions;
  42. using Senparc.NeuChar;
  43. using Senparc.Weixin.CommonAPIs;
  44. namespace Senparc.Weixin.MP.AdvancedAPIs
  45. {
  46. /// <summary>
  47. /// 用户接口
  48. /// </summary>
  49. public static class UserApi
  50. {
  51. #region 同步方法
  52. /// <summary>
  53. /// 获取用户信息
  54. /// </summary>
  55. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  56. /// <param name="openId">普通用户的标识,对当前公众号唯一</param>
  57. /// <param name="lang">返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语</param>
  58. /// <returns></returns>
  59. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.Info", true)]
  60. public static UserInfoJson Info(string accessTokenOrAppId, string openId, Language lang = Language.zh_CN)
  61. {
  62. return ApiHandlerWapper.TryCommonApi(accessToken =>
  63. {
  64. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}",
  65. accessToken.AsUrlData(), openId.AsUrlData(), lang.ToString("g").AsUrlData());
  66. return CommonJsonSend.Send<UserInfoJson>(null, url, null, CommonJsonSendType.GET);
  67. //错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):
  68. //{"errcode":40013,"errmsg":"invalid appid"}
  69. }, accessTokenOrAppId);
  70. }
  71. /// <summary>
  72. /// 获取关注者OpenId信息
  73. /// </summary>
  74. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  75. /// <param name="nextOpenId"></param>
  76. /// <returns></returns>
  77. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.Get", true)]
  78. public static OpenIdResultJson Get(string accessTokenOrAppId, string nextOpenId)
  79. {
  80. return ApiHandlerWapper.TryCommonApi(accessToken =>
  81. {
  82. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/get?access_token={0}", accessToken.AsUrlData());
  83. if (!string.IsNullOrEmpty(nextOpenId))
  84. {
  85. url += "&next_openid=" + nextOpenId;
  86. }
  87. return CommonJsonSend.Send<OpenIdResultJson>(null, url, null, CommonJsonSendType.GET);
  88. }, accessTokenOrAppId);
  89. }
  90. /// <summary>
  91. /// 修改关注者备注信息
  92. /// </summary>
  93. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  94. /// <param name="openId">普通用户的标识,对当前公众号唯一</param>
  95. /// <param name="remark">新的备注名,长度必须小于30字符</param>
  96. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  97. /// <returns></returns>
  98. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.UpdateRemark", true)]
  99. public static WxJsonResult UpdateRemark(string accessTokenOrAppId, string openId, string remark, int timeOut = Config.TIME_OUT)
  100. {
  101. return ApiHandlerWapper.TryCommonApi(accessToken =>
  102. {
  103. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info/updateremark?access_token={0}", accessToken.AsUrlData());
  104. var data = new
  105. {
  106. openid = openId,
  107. remark = remark
  108. };
  109. return CommonJsonSend.Send<WxJsonResult>(accessToken, url, data, timeOut: timeOut);
  110. }, accessTokenOrAppId);
  111. }
  112. /// <summary>
  113. /// 批量获取用户基本信息
  114. /// </summary>
  115. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  116. /// <param name="userList"></param>
  117. /// <param name="timeOut"></param>
  118. /// <returns></returns>
  119. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchGetUserInfo", true)]
  120. public static BatchGetUserInfoJsonResult BatchGetUserInfo(string accessTokenOrAppId, List<BatchGetUserInfoData> userList, int timeOut = Config.TIME_OUT)
  121. {
  122. return ApiHandlerWapper.TryCommonApi(accessToken =>
  123. {
  124. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info/batchget?access_token={0}", accessToken.AsUrlData());
  125. var data = new
  126. {
  127. user_list = userList,
  128. };
  129. return CommonJsonSend.Send<BatchGetUserInfoJsonResult>(accessToken, url, data, timeOut: timeOut);
  130. }, accessTokenOrAppId);
  131. }
  132. /// <summary>
  133. /// 获取黑名单
  134. /// </summary>
  135. /// <param name="accessTokenOrAppId"></param>
  136. /// <param name="beginOpenId"></param>
  137. /// <param name="timeOut"></param>
  138. /// <returns></returns>
  139. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.GetBlackList", true)]
  140. public static OpenIdResultJson GetBlackList(string accessTokenOrAppId, string beginOpenId, int timeOut = Config.TIME_OUT)
  141. {
  142. return ApiHandlerWapper.TryCommonApi<OpenIdResultJson>(accessToken =>
  143. {
  144. string url = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/getblacklist?access_token={0}", accessToken.AsUrlData());
  145. var data = new
  146. {
  147. begin_openid = beginOpenId
  148. };
  149. return CommonJsonSend.Send<OpenIdResultJson>(accessToken, url, data, timeOut: timeOut);
  150. }, accessTokenOrAppId, true);
  151. }
  152. /// <summary>
  153. /// 取消拉黑用户
  154. /// </summary>
  155. /// <param name="accessTokenOrAppId"></param>
  156. /// <param name="openidList"></param>
  157. /// <param name="timeOut"></param>
  158. /// <returns></returns>
  159. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchUnBlackList", true)]
  160. public static WxJsonResult BatchUnBlackList(string accessTokenOrAppId, List<string> openidList, int timeOut = Config.TIME_OUT)
  161. {
  162. return ApiHandlerWapper.TryCommonApi<OpenIdResultJson>(accessToken =>
  163. {
  164. string urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/batchunblacklist?access_token={0}", accessToken.AsUrlData());
  165. var data = new
  166. {
  167. openid_list = openidList
  168. };
  169. return CommonJsonSend.Send<OpenIdResultJson>(accessToken, urlFormat, data, timeOut: timeOut);
  170. }, accessTokenOrAppId, true);
  171. }
  172. /// <summary>
  173. /// 拉黑用户
  174. /// </summary>
  175. /// <param name="accessTokenOrAppId"></param>
  176. /// <param name="openidList"></param>
  177. /// <param name="timeOut"></param>
  178. /// <returns></returns>
  179. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchBlackList", true)]
  180. public static WxJsonResult BatchBlackList(string accessTokenOrAppId, List<string> openidList, int timeOut = Config.TIME_OUT)
  181. {
  182. return ApiHandlerWapper.TryCommonApi<OpenIdResultJson>(accessToken =>
  183. {
  184. string urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/batchblacklist?access_token={0}", accessToken.AsUrlData());
  185. var data = new
  186. {
  187. openid_list = openidList
  188. };
  189. return CommonJsonSend.Send<OpenIdResultJson>(accessToken, urlFormat, data, timeOut: timeOut);
  190. }, accessTokenOrAppId, true);
  191. }
  192. #endregion
  193. #if !NET35 && !NET40
  194. #region 异步方法
  195. /// <summary>
  196. /// 【异步方法】获取用户信息
  197. /// </summary>
  198. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  199. /// <param name="openId">普通用户的标识,对当前公众号唯一</param>
  200. /// <param name="lang">返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语</param>
  201. /// <returns></returns>
  202. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.InfoAsync", true)]
  203. public static async Task<UserInfoJson> InfoAsync(string accessTokenOrAppId, string openId, Language lang = Language.zh_CN)
  204. {
  205. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  206. {
  207. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}",
  208. accessToken.AsUrlData(), openId.AsUrlData(), lang.ToString("g").AsUrlData());
  209. return await CommonJsonSend.SendAsync<UserInfoJson>(null, url, null, CommonJsonSendType.GET);
  210. //错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):
  211. //{"errcode":40013,"errmsg":"invalid appid"}
  212. }, accessTokenOrAppId);
  213. }
  214. /// <summary>
  215. /// 【异步方法】获取关注者OpenId信息
  216. /// </summary>
  217. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  218. /// <param name="nextOpenId"></param>
  219. /// <returns></returns>
  220. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.GetAsync", true)]
  221. public static async Task<OpenIdResultJson> GetAsync(string accessTokenOrAppId, string nextOpenId)
  222. {
  223. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  224. {
  225. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/get?access_token={0}", accessToken.AsUrlData());
  226. if (!string.IsNullOrEmpty(nextOpenId))
  227. {
  228. url += "&next_openid=" + nextOpenId;
  229. }
  230. return await CommonJsonSend.SendAsync<OpenIdResultJson>(null, url, null, CommonJsonSendType.GET);
  231. }, accessTokenOrAppId);
  232. }
  233. /// <summary>
  234. /// 【异步方法】修改关注者备注信息
  235. /// </summary>
  236. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  237. /// <param name="openId">普通用户的标识,对当前公众号唯一</param>
  238. /// <param name="remark">新的备注名,长度必须小于30字符</param>
  239. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  240. /// <returns></returns>
  241. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.UpdateRemarkAsync", true)]
  242. public static async Task<WxJsonResult> UpdateRemarkAsync(string accessTokenOrAppId, string openId, string remark, int timeOut = Config.TIME_OUT)
  243. {
  244. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  245. {
  246. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info/updateremark?access_token={0}", accessToken.AsUrlData());
  247. var data = new
  248. {
  249. openid = openId,
  250. remark = remark
  251. };
  252. return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, url, data, timeOut: timeOut);
  253. }, accessTokenOrAppId);
  254. }
  255. /// <summary>
  256. /// 【异步方法】批量获取用户基本信息
  257. /// </summary>
  258. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  259. /// <param name="userList"></param>
  260. /// <param name="timeOut"></param>
  261. /// <returns></returns>
  262. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchGetUserInfoAsync", true)]
  263. public static async Task<BatchGetUserInfoJsonResult> BatchGetUserInfoAsync(string accessTokenOrAppId, List<BatchGetUserInfoData> userList, int timeOut = Config.TIME_OUT)
  264. {
  265. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  266. {
  267. string url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info/batchget?access_token={0}", accessToken.AsUrlData());
  268. var data = new
  269. {
  270. user_list = userList,
  271. };
  272. return await CommonJsonSend.SendAsync<BatchGetUserInfoJsonResult>(accessToken, url, data, timeOut: timeOut);
  273. }, accessTokenOrAppId);
  274. }
  275. /// <summary>
  276. /// 获取黑名单
  277. /// </summary>
  278. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  279. /// <param name="beginOpenId">当 begin_openid 为空时,默认从开头拉取。</param>
  280. /// <param name="timeOut"></param>
  281. /// <returns>每次调用最多可拉取 10000 个OpenID</returns>
  282. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.GetBlackListAsync", true)]
  283. public static async Task<OpenIdResultJson> GetBlackListAsync(string accessTokenOrAppId, string beginOpenId, int timeOut = Config.TIME_OUT)
  284. {
  285. return await ApiHandlerWapper.TryCommonApiAsync<OpenIdResultJson>(async accessToken =>
  286. {
  287. string url = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/getblacklist?access_token={0}", accessToken.AsUrlData());
  288. var data = new
  289. {
  290. begin_openid = beginOpenId
  291. };
  292. return await CommonJsonSend.SendAsync<OpenIdResultJson>(accessToken, url, data, timeOut: timeOut);
  293. }, accessTokenOrAppId, true);
  294. }
  295. /// <summary>
  296. /// 取消拉黑用户
  297. /// </summary>
  298. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  299. /// <param name="openidList">需要移除黑名单的用户的openid,一次移除最多允许20个</param>
  300. /// <param name="timeOut"></param>
  301. /// <returns></returns>
  302. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchUnBlackListAsync", true)]
  303. public static async Task<WxJsonResult> BatchUnBlackListAsync(string accessTokenOrAppId, List<string> openidList, int timeOut = Config.TIME_OUT)
  304. {
  305. return await ApiHandlerWapper.TryCommonApiAsync<OpenIdResultJson>(async accessToken =>
  306. {
  307. string urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/batchunblacklist?access_token={0}", accessToken.AsUrlData());
  308. var data = new
  309. {
  310. openid_list = openidList
  311. };
  312. return await CommonJsonSend.SendAsync<OpenIdResultJson>(accessToken, urlFormat, data, timeOut: timeOut);
  313. }, accessTokenOrAppId, true);
  314. }
  315. /// <summary>
  316. /// 拉黑用户
  317. /// </summary>
  318. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  319. /// <param name="openidList">需要拉入黑名单的用户的openid,一次拉黑最多允许20个</param>
  320. /// <param name="timeOut"></param>
  321. /// <returns></returns>
  322. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UserApi.BatchBlackListAsync", true)]
  323. public static async Task<WxJsonResult> BatchBlackListAsync(string accessTokenOrAppId, List<string> openidList, int timeOut = Config.TIME_OUT)
  324. {
  325. return await ApiHandlerWapper.TryCommonApiAsync<OpenIdResultJson>(async accessToken =>
  326. {
  327. string urlFormat = string.Format(Config.ApiMpHost + "/cgi-bin/tags/members/batchblacklist?access_token={0}", accessToken.AsUrlData());
  328. var data = new
  329. {
  330. openid_list = openidList
  331. };
  332. return await CommonJsonSend.SendAsync<OpenIdResultJson>(accessToken, urlFormat, data, timeOut: timeOut);
  333. }, accessTokenOrAppId, true);
  334. }
  335. #endregion
  336. #endif
  337. }
  338. }