コード例 #1
0
        /// <summary>
        /// 钉钉,新增人员同步钉钉
        /// </summary>
        /// <param name="emp">部门基本信息</param>
        /// <returns></returns>
        public CreateUser_PostVal GPM_Ding_CreateEmp(Emp emp)
        {
            string access_token = getAccessToken();
            string url          = "https://oapi.dingtalk.com/user/create?access_token=" + access_token;

            try
            {
                IDictionary <string, object> list = new Dictionary <string, object>();
                //如果用户编号存在则按照此账号进行新建
                if (!(DataType.IsNullOrEmpty(emp.No) || string.IsNullOrWhiteSpace(emp.No)))
                {
                    list.Add("userid", emp.No);
                }
                list.Add("name", emp.Name);
                //部门数组
                List <string> listArrary = new List <string>();
                listArrary.Add(emp.FK_Dept);

                list.Add("department", listArrary);
                list.Add("mobile", emp.Tel);
                list.Add("email", emp.Email);

                string str = BP.Tools.FormatToJson.ToJson_FromDictionary(list);
                str = new HttpWebResponseUtility().HttpResponsePost_Json(url, str);
                CreateUser_PostVal postVal = FormatToJson.ParseFromJson <CreateUser_PostVal>(str);

                //请求返回信息
                if (postVal != null)
                {
                    if (postVal.errcode != "0")
                    {
                        //在钉钉通讯录已经存在
                        if (postVal.errcode == "60102")
                        {
                            postVal.userid = emp.No;
                        }
                        BP.DA.Log.DefaultLogWriteLineError("钉钉新增人员失败:" + postVal.errcode + "-" + postVal.errmsg);
                    }
                    return(postVal);
                }
            }
            catch (Exception ex)
            {
                BP.DA.Log.DefaultLogWriteLineError(ex.Message);
            }
            return(null);
        }
コード例 #2
0
        /// <summary>
        /// 获取用户ID
        /// </summary>
        /// <param name="code"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public string GetUserID(string code)
        {
            string access_token = getAccessToken();
            string url          = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + access_token + "&code=" + code;

            try
            {
                string             str  = new HttpWebResponseUtility().HttpResponseGet(url);
                CreateUser_PostVal user = new CreateUser_PostVal();
                user = FormatToJson.ParseFromJson <CreateUser_PostVal>(str);
                //BP.DA.Log.DefaultLogWriteLineError(access_token + "code:" + code + "1." + user.userid + "2." + user.errcode + "3." + user.errmsg);
                if (!DataType.IsNullOrEmpty(user.userid))
                {
                    return(user.userid);
                }
            }
            catch (Exception ex)
            {
                BP.DA.Log.DefaultLogWriteLineError(ex.Message);
                return(ex.Message);
            }
            return("");
        }
コード例 #3
0
        /// <summary>
        /// 钉钉,编辑人员同步钉钉
        /// </summary>
        /// <param name="emp">部门基本信息</param>
        /// <returns></returns>
        public Ding_Post_ReturnVal GPM_Ding_EditEmp(Emp emp, List <string> deptIds = null)
        {
            string access_token = getAccessToken();
            string url          = "https://oapi.dingtalk.com/user/update?access_token=" + access_token;

            try
            {
                IDictionary <string, object> list = new Dictionary <string, object>();
                list.Add("userid", emp.No);
                list.Add("name", emp.Name);
                list.Add("email", emp.Email);
                list.Add("mobile", emp.Tel);
                list.Add("position", "");
                //钉钉根据此从其他部门删除或增加到其他部门
                if (deptIds != null && deptIds.Count > 0)
                {
                    list.Add("department", deptIds);
                }
                string str = BP.Tools.FormatToJson.ToJson_FromDictionary(list);
                str = new HttpWebResponseUtility().HttpResponsePost_Json(url, str);
                Ding_Post_ReturnVal postVal = FormatToJson.ParseFromJson <Ding_Post_ReturnVal>(str);

                //请求返回信息
                if (postVal != null)
                {
                    bool create_Ding_user = false;
                    //40022企业中的手机号码和登陆钉钉的手机号码不一致,暂时不支持修改用户信息,可以删除后重新添加
                    if (postVal.errcode == "40022" || postVal.errcode == "40021")
                    {
                        create_Ding_user = true;
                        postVal          = GPM_Ding_DeleteEmp(emp.No);
                        //删除失败
                        if (postVal.errcode != "0")
                        {
                            create_Ding_user = false;
                        }
                    }
                    else if (postVal.errcode == "60121")//60121找不到该用户
                    {
                        create_Ding_user = true;
                    }

                    //需要新增人员
                    if (create_Ding_user == true)
                    {
                        CreateUser_PostVal postUserVal = GPM_Ding_CreateEmp(emp);
                        //消息传递
                        postVal.errcode = postUserVal.errcode;
                        postVal.errmsg  = postUserVal.errmsg;
                    }

                    if (postVal.errcode != "0")
                    {
                        BP.DA.Log.DefaultLogWriteLineError("钉钉修改人员失败:" + postVal.errcode + "-" + postVal.errmsg);
                    }
                    return(postVal);
                }
            }
            catch (Exception ex)
            {
                BP.DA.Log.DefaultLogWriteLineError(ex.Message);
            }
            return(null);
        }