OA与域控集成后,在域控中禁用/删除用户后,OA同步禁用,但是对接的企业微信并未删除用户。
Quartz计划任务
class DeleteWeiXinUserJob : IJob
{
public static string corpid = ConfigurationManager.AppSettings["corpid"];
public static string secrect = ConfigurationManager.AppSettings["secrect"];
public static Common.Logging.ILog logObj = Common.Logging.LogManager.GetLogger<DeleteWeiXinUserJob>();
public async Task Execute(IJobExecutionContext context)
{
//string secrect = "YLCwIe_sxxxxxxxx5-KSI99eeeeeeeeeqU";
string access_token = "";
//string[] userids;
List<DepAndUser> depanduserList = new List<DepAndUser>();
List<Dep> depList = new List<Dep>();
using (var client = new HttpClient())
{
var responseString = client.GetStringAsync(string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", corpid, secrect));
responseString.Wait();
var resultObj = JsonConvert.DeserializeObject<TokenResult>(responseString.Result);
if (resultObj != null && resultObj.errcode == 0)
{
access_token = resultObj.access_token;
var depanduserTask = client.GetStringAsync(string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={0}&department_id={1}&fetch_child={2}", access_token, 1, 1));
depanduserTask.Wait();
var depanduserObj = JsonConvert.DeserializeObject<DepAndUserResult>(depanduserTask.Result);
if (depanduserObj != null && depanduserObj.errcode == 0)
{
depanduserList = depanduserObj.userlist;
}
//var depsTask = client.GetStringAsync(string.Format("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={0}", access_token)); //getalldep(access_token);
//var depListResult = JsonConvert.DeserializeObject<DepResult>(depsTask.Result);
//if (depListResult != null && depListResult.department.Count() > 0)
//{
// depList.AddRange(depListResult.department);
//}
}
}
List<string> needDeleteUserList = new List<string>();
var oauserlist = getoauser();
//Console.WriteLine("OA Active User Number :" + oauserlist.Count);
logObj.Debug("OA Active User Number :" + oauserlist.Count);
if(oauserlist == null || oauserlist.Count == 0)
{
logObj.Debug("No OA User Return.");
return;
}
foreach (var user in depanduserList)
{
if (false == oauserlist.Contains(user.userid))
{
needDeleteUserList.Add(user.userid);
}
}
//userids = needDeleteUserList.ToArray();
var deleteArray = new List<string>();
logObj.Debug("WeiXin need Delete User Number :" + needDeleteUserList.Count);
int count = 0;
for (int i = 0; i < needDeleteUserList.Count; i++)
{
deleteArray.Add(needDeleteUserList[i]);
if ((i + 1) % 20 == 0)
{
count += deleteArray.Count;
deleteWeixinUser(deleteArray.ToArray(), access_token);
deleteArray.Clear();
}
}
//收尾
if (deleteArray.Count > 0)
{
count += deleteArray.Count;
deleteWeixinUser(deleteArray.ToArray(), access_token);
deleteArray.Clear();
}
logObj.Debug("WeiXin Delete User Number :" + count);
//Console.ReadLine();
}
//查询OA中活动用户
private static List<string> getoauser()
{
List<string> oauseridList = new List<string>();
string sql = @"select a.emp_id, a.empname,c.orgname from org_employee a
LEFT JOIN org_organization_user b on a.emp_id = b.emp_id
left join org_organization c on b.org_id = c.org_id
where a.userisactive = 1 and a.userisdeleted = 0 and a.oa_enterprise_number = 1";
sql = "select a.emp_id from org_employee a where a.userisactive = 1 and a.userisdeleted = 0 and a.oa_enterprise_number = 1";
DbHelperOra oracleHelper = new DbHelperOra("OA");
DataSet ds = oracleHelper.Query(sql);
if(ds != null || ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
oauseridList.Add(row["EMP_ID"].ToString());
}
}
return oauseridList;
}
//删除企业微信中的用户
private static void deleteWeixinUser(string[] userids, string access_token)
{
using (var client = new HttpClient())
{
DeleteUserList list = new DeleteUserList();
list.useridlist = userids;
string body = JsonConvert.SerializeObject(list);
HttpContent content = new StringContent(body);
var deleteResultTask = client.PostAsync(string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={0}", access_token), content);
deleteResultTask.Wait();
string deleteResultString = deleteResultTask.Result.Content.ReadAsStringAsync().Result;
var deleteResult = JsonConvert.DeserializeObject<DeleteResult>(deleteResultString);
logObj.Debug("Delete WeiXin User: errcode:" + deleteResult.errcode + " errmsg:" + deleteResult.errmsg);
}
}
标签:Count,string,微信,OA,access,var,token,域控,new
From: https://blog.51cto.com/kenji/7873425