public CommonResult DeleteDept(string accessToken, int Id)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}";
var data = new
{
id = Id
};
var url = string.Format(urlFormat, accessToken, Id);
var postData = data.ToJSON();
CommonResult result = CorpJsonHelper<CommonResult>.ConvertJson(url, postData);
return result;
}
public static class JSONExtension
{
public static string ToJSON(this object obj)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
json.Serialize(obj, sb);
return sb.ToString();
}
public static T ParseJSON<T>(this string str)
{
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
return json.Deserialize<T>(str);
}
}
public class CorpJsonHelper<T>
{
public static T ConvertJson(string url, object postData)
{
HttpHelper helper = new HttpHelper();
var result = helper.GetHtml(url);
return result.ParseJSON<T>();
}
public static T ConvertJson(string url, object postData,string method)
{
HttpHelper helper = new HttpHelper();
var result = helper.GetHtml(url, postData.ToString(),method);
return result.ParseJSON<T>();
}
}
public class CorpAddressBookApi : ICorpAddressBookApi
{
/// <summary>
/// 创建部门。
/// 管理员须拥有“操作通讯录”的接口权限,以及父部门的管理权限。
/// </summary>
public CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}";
var data = new
{
name = name,
parentid = parentId,
order = 1,
//id = 100,
};
var url = string.Format(urlFormat, accessToken);
var postData = data.ToJSON();
CorpDeptCreateJson result = CorpJsonHelper<CorpDeptCreateJson>.ConvertJson(url, postData,"POST");
return result;
}
public CommonResult DeleteDept(string accessToken, int Id)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}";
var data = new
{
id = Id
};
var url = string.Format(urlFormat, accessToken, Id);
var postData = data.ToJSON();
CommonResult result = CorpJsonHelper<CommonResult>.ConvertJson(url, postData);
return result;
}
public CorpDeptListJson ListDept(string accessToken)
{
string _id = "0";
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={0}&id={1}";
var data = new
{
id = _id,
};
var postData = data.ToJSON();
var url = string.Format(urlFormat, accessToken, _id);
CorpDeptListJson result = CorpJsonHelper<CorpDeptListJson>.ConvertJson(url, postData);
return result;
}
public CommonResult UpdateDept(string accessToken, int Id, string newName)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}";
var data = new
{
name = newName,
//parentid = parentId,
order = 1,
id = Id,
};
var url = string.Format(urlFormat, accessToken);
var postData = data.ToJSON();
CommonResult result = CorpJsonHelper<CommonResult>.ConvertJson(url, postData, "POST");
return result;
}
}
ICorpAddressBookApi bll = new CorpAddressBookApi();