GroupsApi.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. 文件名:GroupsAPI.cs
  17. 文件功能描述:用户组接口
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150312
  22. 修改描述:开放代理请求超时时间
  23. 修改标识:Senparc - 20160718
  24. 修改描述:增加其接口的异步方法
  25. 修改标识:Senparc - 20170707
  26. 修改描述:v14.5.1 完善异步方法async/await
  27. 修改标识:Senparc - 20190129
  28. 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
  29. ----------------------------------------------------------------*/
  30. /*
  31. API地址:http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html
  32. */
  33. using System.Threading.Tasks;
  34. using Senparc.CO2NET.Extensions;
  35. using Senparc.NeuChar;
  36. using Senparc.Weixin.CommonAPIs;
  37. using Senparc.Weixin.Entities;
  38. using Senparc.Weixin.HttpUtility;
  39. using Senparc.Weixin.MP.AdvancedAPIs.Groups;
  40. using Senparc.Weixin.MP.CommonAPIs;
  41. namespace Senparc.Weixin.MP.AdvancedAPIs
  42. {
  43. /// <summary>
  44. /// 用户组接口
  45. /// </summary>
  46. public static class GroupsApi
  47. {
  48. #region 同步方法
  49. /// <summary>
  50. /// 创建分组
  51. /// </summary>
  52. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  53. /// <param name="name">分组名字(30个字符以内)</param>
  54. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  55. /// <returns></returns>
  56. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Create", true)]
  57. public static CreateGroupResult Create(string accessTokenOrAppId, string name, int timeOut = Config.TIME_OUT)
  58. {
  59. return ApiHandlerWapper.TryCommonApi(accessToken =>
  60. {
  61. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/create?access_token={0}";
  62. var data = new
  63. {
  64. group = new
  65. {
  66. name = name
  67. }
  68. };
  69. return CommonJsonSend.Send<CreateGroupResult>(accessToken, urlFormat, data, timeOut: timeOut);
  70. }, accessTokenOrAppId);
  71. }
  72. /// <summary>
  73. /// 获取所有分组
  74. /// </summary>
  75. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  76. /// <returns></returns>
  77. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Get", true)]
  78. public static GroupsJson Get(string accessTokenOrAppId)
  79. {
  80. return ApiHandlerWapper.TryCommonApi(accessToken =>
  81. {
  82. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/get?access_token={0}";
  83. var url = string.Format(urlFormat, accessToken.AsUrlData());
  84. return CommonJsonSend.Send< GroupsJson >(null, url, null, CommonJsonSendType.GET);
  85. }, accessTokenOrAppId);
  86. }
  87. /// <summary>
  88. /// 获取用户分组
  89. /// </summary>
  90. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  91. /// <param name="openId"></param>
  92. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  93. /// <returns></returns>
  94. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetId", true)]
  95. public static GetGroupIdResult GetId(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
  96. {
  97. return ApiHandlerWapper.TryCommonApi(accessToken =>
  98. {
  99. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/getid?access_token={0}";
  100. var data = new { openid = openId };
  101. return CommonJsonSend.Send<GetGroupIdResult>(accessToken, urlFormat, data, timeOut: timeOut);
  102. }, accessTokenOrAppId);
  103. }
  104. /// <summary>
  105. /// 修改分组名
  106. /// </summary>
  107. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  108. /// <param name="id"></param>
  109. /// <param name="name">分组名字(30个字符以内)</param>
  110. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  111. /// <returns></returns>
  112. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Update", true)]
  113. public static WxJsonResult Update(string accessTokenOrAppId, int id, string name, int timeOut = Config.TIME_OUT)
  114. {
  115. return ApiHandlerWapper.TryCommonApi(accessToken =>
  116. {
  117. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/update?access_token={0}";
  118. var data = new
  119. {
  120. group = new
  121. {
  122. id = id,
  123. name = name
  124. }
  125. };
  126. return CommonJsonSend.Send(accessToken, urlFormat, data, timeOut: timeOut);
  127. }, accessTokenOrAppId);
  128. }
  129. /// <summary>
  130. /// 移动用户分组
  131. /// </summary>
  132. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  133. /// <param name="openId"></param>
  134. /// <param name="toGroupId"></param>
  135. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  136. /// <returns></returns>
  137. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.MemberUpdate", true)]
  138. public static WxJsonResult MemberUpdate(string accessTokenOrAppId, string openId, int toGroupId, int timeOut = Config.TIME_OUT)
  139. {
  140. return ApiHandlerWapper.TryCommonApi(accessToken =>
  141. {
  142. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/update?access_token={0}";
  143. var data = new
  144. {
  145. openid = openId,
  146. to_groupid = toGroupId
  147. };
  148. return CommonJsonSend.Send(accessToken, urlFormat, data, timeOut: timeOut);
  149. }, accessTokenOrAppId);
  150. }
  151. /// <summary>
  152. /// 批量移动用户分组
  153. /// </summary>
  154. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  155. /// <param name="toGroupId">分组id</param>
  156. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  157. /// <param name="openIds">用户唯一标识符openid的列表(size不能超过50)</param>
  158. /// <returns></returns>
  159. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.BatchUpdate", true)]
  160. public static WxJsonResult BatchUpdate(string accessTokenOrAppId, int toGroupId, int timeOut = Config.TIME_OUT, params string[] openIds)
  161. {
  162. return ApiHandlerWapper.TryCommonApi(accessToken =>
  163. {
  164. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/batchupdate?access_token={0}";
  165. var data = new
  166. {
  167. openid_list = openIds,
  168. to_groupid = toGroupId
  169. };
  170. return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
  171. }, accessTokenOrAppId);
  172. }
  173. /// <summary>
  174. /// 删除分组
  175. /// </summary>
  176. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  177. /// <param name="groupId">分组id</param>
  178. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  179. /// <returns></returns>
  180. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Delete", true)]
  181. public static WxJsonResult Delete(string accessTokenOrAppId, int groupId, int timeOut = Config.TIME_OUT)
  182. {
  183. return ApiHandlerWapper.TryCommonApi(accessToken =>
  184. {
  185. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/delete?access_token={0}";
  186. var data = new
  187. {
  188. group = new
  189. {
  190. id = groupId
  191. }
  192. };
  193. return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
  194. }, accessTokenOrAppId);
  195. }
  196. #endregion
  197. #if !NET35 && !NET40
  198. #region 异步方法
  199. /// <summary>
  200. /// 【异步方法】创建分组
  201. /// </summary>
  202. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  203. /// <param name="name">分组名字(30个字符以内)</param>
  204. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  205. /// <returns></returns>
  206. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.CreateAsync", true)]
  207. public static async Task<CreateGroupResult> CreateAsync(string accessTokenOrAppId, string name, int timeOut = Config.TIME_OUT)
  208. {
  209. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  210. {
  211. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/create?access_token={0}";
  212. var data = new
  213. {
  214. group = new
  215. {
  216. name = name
  217. }
  218. };
  219. return await CommonJsonSend.SendAsync<CreateGroupResult>(accessToken, urlFormat, data, timeOut: timeOut);
  220. }, accessTokenOrAppId);
  221. }
  222. /// <summary>
  223. /// 【异步方法】获取所有分组
  224. /// </summary>
  225. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  226. /// <returns></returns>
  227. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetAsync", true)]
  228. public static async Task<GroupsJson> GetAsync(string accessTokenOrAppId)
  229. {
  230. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  231. {
  232. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/get?access_token={0}";
  233. var url = string.Format(urlFormat, accessToken.AsUrlData());
  234. return await CommonJsonSend.SendAsync<GroupsJson>(null, url, null, CommonJsonSendType.GET);
  235. }, accessTokenOrAppId);
  236. }
  237. /// <summary>
  238. /// 【异步方法】获取用户分组
  239. /// </summary>
  240. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  241. /// <param name="openId"></param>
  242. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  243. /// <returns></returns>
  244. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetIdAsync", true)]
  245. public static async Task<GetGroupIdResult> GetIdAsync(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
  246. {
  247. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  248. {
  249. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/getid?access_token={0}";
  250. var data = new { openid = openId };
  251. return await CommonJsonSend.SendAsync<GetGroupIdResult>(accessToken, urlFormat, data, timeOut: timeOut);
  252. }, accessTokenOrAppId);
  253. }
  254. /// <summary>
  255. /// 【异步方法】修改分组名
  256. /// </summary>
  257. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  258. /// <param name="id"></param>
  259. /// <param name="name">分组名字(30个字符以内)</param>
  260. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  261. /// <returns></returns>
  262. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.UpdateAsync", true)]
  263. public static async Task<WxJsonResult> UpdateAsync(string accessTokenOrAppId, int id, string name, int timeOut = Config.TIME_OUT)
  264. {
  265. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  266. {
  267. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/update?access_token={0}";
  268. var data = new
  269. {
  270. group = new
  271. {
  272. id = id,
  273. name = name
  274. }
  275. };
  276. return await CommonJsonSend.SendAsync(accessToken, urlFormat, data, timeOut: timeOut);
  277. }, accessTokenOrAppId);
  278. }
  279. /// <summary>
  280. /// 【异步方法】移动用户分组
  281. /// </summary>
  282. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  283. /// <param name="openId"></param>
  284. /// <param name="toGroupId"></param>
  285. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  286. /// <returns></returns>
  287. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.MemberUpdateAsync", true)]
  288. public static async Task<WxJsonResult> MemberUpdateAsync(string accessTokenOrAppId, string openId, int toGroupId, int timeOut = Config.TIME_OUT)
  289. {
  290. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  291. {
  292. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/update?access_token={0}";
  293. var data = new
  294. {
  295. openid = openId,
  296. to_groupid = toGroupId
  297. };
  298. return await CommonJsonSend.SendAsync(accessToken, urlFormat, data, timeOut: timeOut);
  299. }, accessTokenOrAppId);
  300. }
  301. /// <summary>
  302. /// 【异步方法】批量移动用户分组
  303. /// </summary>
  304. /// <param name="accessTokenOrAppId">调用接口凭证</param>
  305. /// <param name="toGroupId">分组id</param>
  306. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  307. /// <param name="openIds">用户唯一标识符openid的列表(size不能超过50)</param>
  308. /// <returns></returns>
  309. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.BatchUpdateAsync", true)]
  310. public static async Task<WxJsonResult> BatchUpdateAsync(string accessTokenOrAppId, int toGroupId, int timeOut = Config.TIME_OUT, params string[] openIds)
  311. {
  312. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  313. {
  314. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/batchupdate?access_token={0}";
  315. var data = new
  316. {
  317. openid_list = openIds,
  318. to_groupid = toGroupId
  319. };
  320. return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
  321. }, accessTokenOrAppId);
  322. }
  323. /// <summary>
  324. /// 删除分组
  325. /// </summary>
  326. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  327. /// <param name="groupId">分组id</param>
  328. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  329. /// <returns></returns>
  330. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.DeleteAsync", true)]
  331. public static async Task<WxJsonResult> DeleteAsync(string accessTokenOrAppId, int groupId, int timeOut = Config.TIME_OUT)
  332. {
  333. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  334. {
  335. var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/delete?access_token={0}";
  336. var data = new
  337. {
  338. group = new
  339. {
  340. id = groupId
  341. }
  342. };
  343. return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
  344. }, accessTokenOrAppId);
  345. }
  346. #endregion
  347. #endif
  348. }
  349. }