#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() 方法请求接口 ----------------------------------------------------------------*/ /* 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 { /// /// 用户组接口 /// public static class GroupsApi { #region 同步方法 /// /// 创建分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 分组名字(30个字符以内) /// 代理请求超时时间(毫秒) /// [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(accessToken, urlFormat, data, timeOut: timeOut); }, accessTokenOrAppId); } /// /// 获取所有分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// [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); } /// /// 获取用户分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// 代理请求超时时间(毫秒) /// [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(accessToken, urlFormat, data, timeOut: timeOut); }, accessTokenOrAppId); } /// /// 修改分组名 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// 分组名字(30个字符以内) /// 代理请求超时时间(毫秒) /// [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); } /// /// 移动用户分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// /// 代理请求超时时间(毫秒) /// [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); } /// /// 批量移动用户分组 /// /// 调用接口凭证 /// 分组id /// 代理请求超时时间(毫秒) /// 用户唯一标识符openid的列表(size不能超过50) /// [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(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut); }, accessTokenOrAppId); } /// /// 删除分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 分组id /// 代理请求超时时间(毫秒) /// [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(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut); }, accessTokenOrAppId); } #endregion #if !NET35 && !NET40 #region 异步方法 /// /// 【异步方法】创建分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 分组名字(30个字符以内) /// 代理请求超时时间(毫秒) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.CreateAsync", true)] public static async Task 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(accessToken, urlFormat, data, timeOut: timeOut); }, accessTokenOrAppId); } /// /// 【异步方法】获取所有分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetAsync", true)] public static async Task 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(null, url, null, CommonJsonSendType.GET); }, accessTokenOrAppId); } /// /// 【异步方法】获取用户分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// 代理请求超时时间(毫秒) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.GetIdAsync", true)] public static async Task 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(accessToken, urlFormat, data, timeOut: timeOut); }, accessTokenOrAppId); } /// /// 【异步方法】修改分组名 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// 分组名字(30个字符以内) /// 代理请求超时时间(毫秒) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.UpdateAsync", true)] public static async Task 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); } /// /// 【异步方法】移动用户分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// /// /// 代理请求超时时间(毫秒) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.MemberUpdateAsync", true)] public static async Task 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); } /// /// 【异步方法】批量移动用户分组 /// /// 调用接口凭证 /// 分组id /// 代理请求超时时间(毫秒) /// 用户唯一标识符openid的列表(size不能超过50) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.BatchUpdateAsync", true)] public static async Task 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(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut); }, accessTokenOrAppId); } /// /// 删除分组 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 分组id /// 代理请求超时时间(毫秒) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "GroupsApi.DeleteAsync", true)] public static async Task 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(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut); }, accessTokenOrAppId); } #endregion #endif } }