1.安装Senparc包
2.代码示例
public ActionResult Home(string code)
{
//获取accesstoken
var accessToken = QYWorkHelper.GetToken();
var userInfoResult = OAuth2Api.GetUserId(accessToken, code);
if(string.IsNullOrEmpty(userInfoResult.UserId)|| string.IsNullOrEmpty(accessToken))
{
return new HttpNotFoundResult();
}
// 获取员工信息
var member = MailListApi.GetMember(accessToken, userInfoResult.UserId);
// 获取部门列表
var departmentList = MailListApi.GetDepartmentList(accessToken);
//获取成员列表
int departmentId = 1;
// 是否递归获取部门下的成员(1/0:是/否)
int fetchChild = 1;
// 获取部门成员
var memberList = MailListApi.GetDepartmentMember(accessToken, departmentId, fetchChild);
// 获取部门成员详情
var memberInfoList = MailListApi.GetDepartmentMemberInfo(accessToken, departmentId, fetchChild);
return Json(new{ member,departmentList, memberList, memberInfoList },JsonRequestBehavior.AllowGet);
}
public class QYWorkHelper
{
/// <summary>
/// 发送消息。文本消息
/// 消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。主页型应用只支持文本消息类型,且文本长度不超过20个字。
/// 需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
public static MassResult SendMessage(string content, string touser = null, string toparty = null, string totag = null, int safe = 0, int timeOut = 10000)
{
//获取accesstoken
var accessToken = GetToken();
MassResult result = MassApi.SendText(accessToken, AppSettingConfig.AgentId, content, touser, toparty, totag, safe, timeOut);
return result;
}
/// <summary>
/// 发送文本卡片消息
/// </summary>
public static MassResult SendMsgCard(string title, string description, string url, string btntxt = null, string toUser = null, string toParty = null, string toTag = null, int timeOut = 10000)
{
//获取accesstoken
var accessToken = GetToken();
MassResult result = MassApi.SendTextCard(accessToken, AppSettingConfig.AgentId, title, description, url, btntxt, toUser, toParty, toTag, timeOut);
return result;
}
/// <summary>
/// 获取token
/// </summary>
/// <returns></returns>
public static string GetToken()
{
string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", "AppId", "AppSecret");
string result = RequestImp(url);
var responseRes = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResult>(result);
if (responseRes.errcode == 0)
{
return responseRes.access_token;
}
return result;
}
public static string RequestImp(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream backStream = response.GetResponseStream();
StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8"));
string result = sr.ReadToEnd();
sr.Close();
backStream.Close();
response.Close();
return result;
}
}在这里获取accesstoken时没有使用盛派SDK来获取,官方说明获取token前需要对账号信息进行过注册,否则会出错。然而在实际开发中加了注册还是会报异常。所以自己封装了个获取token的方法。没有使用以下代码。
//获取token前对账号信息进行过注册,否则会出错。 //AccessTokenContainer.BuildingKey(appId, appSecret); //获取accesstoken //var accessToken = AccessTokenContainer.GetToken(appId, appSecret);

京公网安备 11010502036488号