123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- #region Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- either express or implied. See the License for the specific language governing permissions
- and limitations under the License.
- Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
- ----------------------------------------------------------------*/
- #endregion Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright (C) 2019 Senparc
-
- 文件名:GroupsAPI.cs
- 文件功能描述:用户组接口
-
-
- 创建标识:Senparc - 20150211
-
- 修改标识:Senparc - 20150303
- 修改描述:整理接口
-
- 修改标识:Senparc - 20150312
- 修改描述:开放代理请求超时时间
- 修改标识:Senparc - 20160718
- 修改描述:增加其接口的异步方法
- 修改标识:Senparc - 20170707
- 修改描述:v14.5.1 完善异步方法async/await
-
- 修改标识:Senparc - 20190129
- 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
- ----------------------------------------------------------------*/
- /*
- API地址:http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html
- */
- using System.Threading.Tasks;
- using Senparc.CO2NET.Extensions;
- using Senparc.NeuChar;
- using Senparc.Weixin.CommonAPIs;
- using Senparc.Weixin.Entities;
- using Senparc.Weixin.HttpUtility;
- using Senparc.Weixin.MP.AdvancedAPIs.Groups;
- using Senparc.Weixin.MP.CommonAPIs;
- namespace Senparc.Weixin.MP.AdvancedAPIs
- {
- /// <summary>
- /// 用户组接口
- /// </summary>
- public static class GroupsApi
- {
- #region 同步方法
- /// <summary>
- /// 创建分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="name">分组名字(30个字符以内)</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Create", true)]
- public static CreateGroupResult Create(string accessTokenOrAppId, string name, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/create?access_token={0}";
- var data = new
- {
- group = new
- {
- name = name
- }
- };
- return CommonJsonSend.Send<CreateGroupResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 获取所有分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Get", true)]
- public static GroupsJson Get(string accessTokenOrAppId)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/get?access_token={0}";
- var url = string.Format(urlFormat, accessToken.AsUrlData());
- return CommonJsonSend.Send< GroupsJson >(null, url, null, CommonJsonSendType.GET);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 获取用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetId", true)]
- public static GetGroupIdResult GetId(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/getid?access_token={0}";
- var data = new { openid = openId };
- return CommonJsonSend.Send<GetGroupIdResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 修改分组名
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="id"></param>
- /// <param name="name">分组名字(30个字符以内)</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Update", true)]
- public static WxJsonResult Update(string accessTokenOrAppId, int id, string name, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/update?access_token={0}";
- var data = new
- {
- group = new
- {
- id = id,
- name = name
- }
- };
- return CommonJsonSend.Send(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 移动用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="toGroupId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.MemberUpdate", true)]
- public static WxJsonResult MemberUpdate(string accessTokenOrAppId, string openId, int toGroupId, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/update?access_token={0}";
- var data = new
- {
- openid = openId,
- to_groupid = toGroupId
- };
- return CommonJsonSend.Send(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 批量移动用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">调用接口凭证</param>
- /// <param name="toGroupId">分组id</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="openIds">用户唯一标识符openid的列表(size不能超过50)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.BatchUpdate", true)]
- public static WxJsonResult BatchUpdate(string accessTokenOrAppId, int toGroupId, int timeOut = Config.TIME_OUT, params string[] openIds)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/batchupdate?access_token={0}";
- var data = new
- {
- openid_list = openIds,
- to_groupid = toGroupId
- };
- return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 删除分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="groupId">分组id</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.Delete", true)]
- public static WxJsonResult Delete(string accessTokenOrAppId, int groupId, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/delete?access_token={0}";
- var data = new
- {
- group = new
- {
- id = groupId
- }
- };
- return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
- }, accessTokenOrAppId);
- }
- #endregion
- #if !NET35 && !NET40
- #region 异步方法
- /// <summary>
- /// 【异步方法】创建分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="name">分组名字(30个字符以内)</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.CreateAsync", true)]
- public static async Task<CreateGroupResult> CreateAsync(string accessTokenOrAppId, string name, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/create?access_token={0}";
- var data = new
- {
- group = new
- {
- name = name
- }
- };
- return await CommonJsonSend.SendAsync<CreateGroupResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】获取所有分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetAsync", true)]
- public static async Task<GroupsJson> GetAsync(string accessTokenOrAppId)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/get?access_token={0}";
- var url = string.Format(urlFormat, accessToken.AsUrlData());
- return await CommonJsonSend.SendAsync<GroupsJson>(null, url, null, CommonJsonSendType.GET);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】获取用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetIdAsync", true)]
- public static async Task<GetGroupIdResult> GetIdAsync(string accessTokenOrAppId, string openId, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/getid?access_token={0}";
- var data = new { openid = openId };
- return await CommonJsonSend.SendAsync<GetGroupIdResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】修改分组名
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="id"></param>
- /// <param name="name">分组名字(30个字符以内)</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.UpdateAsync", true)]
- public static async Task<WxJsonResult> UpdateAsync(string accessTokenOrAppId, int id, string name, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/update?access_token={0}";
- var data = new
- {
- group = new
- {
- id = id,
- name = name
- }
- };
- return await CommonJsonSend.SendAsync(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】移动用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="openId"></param>
- /// <param name="toGroupId"></param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.MemberUpdateAsync", true)]
- public static async Task<WxJsonResult> MemberUpdateAsync(string accessTokenOrAppId, string openId, int toGroupId, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/update?access_token={0}";
- var data = new
- {
- openid = openId,
- to_groupid = toGroupId
- };
- return await CommonJsonSend.SendAsync(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 【异步方法】批量移动用户分组
- /// </summary>
- /// <param name="accessTokenOrAppId">调用接口凭证</param>
- /// <param name="toGroupId">分组id</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <param name="openIds">用户唯一标识符openid的列表(size不能超过50)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.BatchUpdateAsync", true)]
- public static async Task<WxJsonResult> BatchUpdateAsync(string accessTokenOrAppId, int toGroupId, int timeOut = Config.TIME_OUT, params string[] openIds)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/members/batchupdate?access_token={0}";
- var data = new
- {
- openid_list = openIds,
- to_groupid = toGroupId
- };
- return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
- }, accessTokenOrAppId);
- }
- /// <summary>
- /// 删除分组
- /// </summary>
- /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
- /// <param name="groupId">分组id</param>
- /// <param name="timeOut">代理请求超时时间(毫秒)</param>
- /// <returns></returns>
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.DeleteAsync", true)]
- public static async Task<WxJsonResult> DeleteAsync(string accessTokenOrAppId, int groupId, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/groups/delete?access_token={0}";
- var data = new
- {
- group = new
- {
- id = groupId
- }
- };
- return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
- }, accessTokenOrAppId);
- }
- #endregion
- #endif
- }
- }
|