webapi获取企业微信通讯录成员完整部门

前言:需求需要获取企业微信通讯录成员部门名称,通过api只能返回最后一级部门名称,现在需要获取完整部门,分多级。

      首先通过手机号调用企业微信api获取个人详细信息,获取部门id,下面方法直接返回部门全称

public string getnames(int id)
        {

            string str = "";
            int pid;
            partment department = null;  //定义好几个null后面用来组装或者替换数据
            string name = null;

            department = DepartmentAPI.get(id)[0];
            pid = department.getParentid();
            name = department.getName();//根据id得到部门名称和父id
            if (pid == 1)
            {
                str = name+"/";
            }
            while (pid!=1)
            {  //当父id不为1时候,循环,父id为“1”时候表示已经取到最顶层“微信企业号”了
                str = name + "/" + str; //拼接部门:海直通用航空有限责任公司/维修工程部/生产计划与控制室 /
                pid = department.getParentid(); //得到父id
                department = DepartmentAPI.get(pid)[0]; //根据父id再取上一层部门
                name = department.getName(); //重新赋值给name
            }
            str = str.Substring(0, str.Length - 1); //去除最后一个“/”:海直通用航空有限责任公司/维修工程部/生产计划与控制室 
            return str;
        }

 

 上面用到的DepartmentAPI.get(id)   department.getParentid()  department.getName() 需要两个类,partment.cs和DepartmentAPI .cs

 public class partment
    {
        public int id { get; set; }
        public string name { get; set; }
        public int parentid { get; set; }
        public int getId()
        {
            return id;
        }
        public void setId(int id)
        {
            this.id = id;
        }
        public string getName()
        {
            return name;
        }
        public int getParentid()
        {
            return parentid;
        }
        public void setParentid(int parentid)
        {
            this.parentid = parentid;
        }
        public void setName(string name)
        {
            this.name = name;
        }
    }


 public class DepartmentAPI
    {

        public static List<partment> getAllDepartment() {
            try
            {
                db_cohccardEntities db = new db_cohccardEntities();
                WeiXin weixin = new WeiXin();
                string mess = string.Empty;
                List<News> list = new List<News>();
                News newsitem = new News();
                News news = db.News.Where(u => u.Sys_CoID == 33).FirstOrDefault();
                var client = new RestClient("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=***************");
                var request = new RestRequest(Method.GET);
                request.AddHeader("Postman-Token", "0cc27133-4ee9-4af6-ae75-223e75883142");
                request.AddHeader("cache-control", "no-cache");
                IRestResponse response = client.Execute(request);
                dynamic obj = JsonConvert.DeserializeObject<dynamic>(response.Content.ToString());
                List<partment> partments =obj.department.ToObject<List<partment>>();
                return partments;
            }
            catch (Exception ex) {
                return null;
            }
        }
        public static List<partment> get(int id)
        {
            List<partment> list = getAllDepartment();
            List<partment> dList = new List<partment>();
            if (list != null)
            {
                foreach (partment item in list)
                {
                    if (item.getId().Equals(id))
                    {
                        dList.Add(item);
                    }
                }
                return dList;
            }
            return null;
        }
    }

 

 


 

上一篇:mysql连接查询


下一篇:MYSQLsql99语法内连接