Exemplo n.º 1
0
        public CreateDepartmentResult CreateDepartment(CreateDepartmentDto dto)
        {
            //dto.AccessToken = GetAccessToken(QyConfig.CorpID, QyConfig.CorpSecret);
            var result = MailListApi.CreateDepartment(dto.AccessToken, dto.Name, dto.ParentId, dto.Order, dto.ID);

            return(result);
        }
Exemplo n.º 2
0
        public JsonResult PostDept(DepartmentList objModal, int?Id)
        {
            //验证错误
            if (!BeforeAddOrUpdate(objModal, Id))
            {
                return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet));
            }

            //objModal.id = int.Parse(Request["deptid"]);
            try
            {
                if (Id.HasValue && Id.Value > 0)
                {
                    MailListApi.UpdateDepartment(GetToken(), objModal);
                }
                else
                {
                    MailListApi.CreateDepartment(GetToken(), objModal);
                }
            }
            catch (ErrorJsonResultException ex)
            {
                WxJsonResult jsResult = ex.JsonResult;
                string       errorMsg = ((ReturnCode_QY)jsResult.errcode).ToString();
                return(ErrorNotification(errorMsg));
            }

            _cacheManager.Remove("DepartmentList");

            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 3
0
 /// <summary>
 /// 新增子部门
 /// </summary>
 /// <param name="NewDepartmentName">新增子部門的名字</param>
 /// <param name="errMsg">返回錯誤信息</param>
 /// <returns>返回操作結果</returns>
 public bool AddChildDepartment(string NewDepartmentName, string UserID, ref string errMsg)
 {
     try
     {
         QYConfig.RegistWX();
         var accessToken = AccessTokenContainer.GetToken(QYConfig.CorpId);
         var result      = MailListApi.CreateDepartment(accessToken, NewDepartmentName, int.Parse(DepartmentID));
         if (result.errcode.ToString() == "请求成功")
         {
             if (DepartmentDA.AddDepartment(result.id.ToString(), DepartmentID, NewDepartmentName, "1", UserID))
             {
                 QYDepartmentBL newdep = GetByWXID(result.id.ToString());
                 Departments.Add(newdep);
                 return(true);
             }
             else
             {
                 errMsg = "数据库新增部门失败";
                 return(false);
             }
         }
         else
         {
             Logger.Log4Net.Error(result.errcode + ":" + result.errmsg);
             errMsg = result.errcode + ":" + result.errmsg;
         }
     }
     catch (Exception e)
     {
         Logger.Log4Net.Error("add department error:" + e);
         errMsg = "操作失敗:" + e.Message;
         return(false);
     }
     return(false);
 }
Exemplo n.º 4
0
        //[TestMethod]
        public long CreateDepartmentTest()
        {
            var accessToken = AccessTokenContainer.GetToken(_corpId, base._corpSecret);
            var result      = MailListApi.CreateDepartment(accessToken, "test", 1, id: 3);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.id > 0);
            return(result.id);
        }
Exemplo n.º 5
0
        public bool AddDepartment(Department dep)
        {
            if (env.IsDevelopment())
            {
                return(true);
            }
            var reObj = MailListApi.CreateDepartment(workSetting.Value.ContactsAccessTokenKey, dep.Name, Convert.ToInt64(dep.ParentID), dep.Order, Convert.ToInt32(dep.DepartmentID));

            return(reObj.errcode == ReturnCode_Work.请求成功);
        }
Exemplo n.º 6
0
        public IActionResult CreateDepartment([FromBody] CreateDepartmentModel model)
        {
            if (model == null || string.IsNullOrEmpty(model.Name))
            {
                return(BadRequest("参数无效"));
            }
            var result = MailListApi.CreateDepartment(APP_KEY, model.Name, model.ParentId);

            return(new ObjectResult(result));
        }
Exemplo n.º 7
0
        //同步部门使用
        public long WX_CreateBranchTB(JH_Auth_Branch Model)
        {
            int pid = 0;
            var bm  = new JH_Auth_BranchB().GetEntity(p => p.DeptCode == Model.DeptRoot && p.ComId == Model.ComId);

            if (bm != null)
            {
                pid = Int32.Parse(bm.WXBMCode.ToString());
            }
            return(MailListApi.CreateDepartment(GetToken(), Model.DeptName, pid, Model.DeptShort).id);
        }
        private int GetTargetDepartmentId(int accountManageId, string groupName, int groupId, List <DepartmentList> departments, int parentId)
        {
            int targetId = 0;

            //groupName = (groupId - 10000)+ "_" + groupName;
            if (groupName.Length > 32)
            {
                groupName = groupName.Substring(0, 32);
            }
            //var groupDepartment = departments.Find(d => d.name.Trim().Equals(groupName, StringComparison.CurrentCultureIgnoreCase));
            var groupDepartment         = departments.Find(d => d.id == groupId);
            var groupDepartmentSameName = departments.Find(d => d.name.Trim().Equals(groupName, StringComparison.CurrentCultureIgnoreCase) && d.parentid == parentId);

            if (groupDepartment == null)
            {
                if (groupDepartmentSameName != null)
                {
                    targetId = groupDepartmentSameName.id;
                }
                else
                {
                    var ret = MailListApi.CreateDepartment(GetToken(accountManageId), groupName, parentId, 1, groupId);
                    if (ret.errcode == Weixin.ReturnCode_QY.请求成功)
                    {
                        targetId = ret.id;
                        departments.Add(new DepartmentList()
                        {
                            id = ret.id, name = groupName, parentid = parentId, level = 0, order = 1
                        });
                    }
                }
            }
            else
            {
                targetId = groupDepartment.id;
                if (!groupDepartment.name.Equals(groupName, StringComparison.CurrentCultureIgnoreCase) ||
                    groupDepartment.parentid != parentId)
                {
                    var ret = MailListApi.UpdateDepartment(GetToken(accountManageId), groupId.ToString(), groupName, parentId);
                    if (ret.errcode == Weixin.ReturnCode_QY.请求成功)
                    {
                        groupDepartment.name     = groupName;
                        groupDepartment.parentid = parentId;
                    }
                }
            }
            return(targetId);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="_name"></param>
        /// <param name="_parentid"></param>
        /// <param name="_order"></param>
        /// <param name="_access_token"></param>
        /// <returns></returns>
        public static bool CreateDepartment(int _id, string _name, int _parentid, int _order)
        {
            AccessTokenResult access_token_result = new AccessTokenResult();
            QYConfigInfo      config = GetQYConfig();

            access_token_result = CommonApi.GetToken(config.QYCorpId, config.QYSecret);
            //调用接口
            var result = MailListApi.CreateDepartment(access_token_result.access_token, _name, _parentid, _order, _id);

            if (result.errmsg == "created")
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 10
0
Arquivo: WxFO.cs Projeto: Luyingjin/Qy
        /// <summary>
        /// 创建组织
        /// </summary>
        /// <param name="QyID">企业ID</param>
        /// <param name="name">部门名称。长度限制为1~64个字符</param>
        /// <param name="parentId">父亲部门id。根部门id为1 </param>
        /// <param name="order">在父部门中的次序。从1开始,数字越大排序越靠后</param>
        /// <param name="id">部门ID。用指定部门ID新建部门,不指定此参数时,则自动生成</param>
        /// <returns></returns>
        public CreateDepartmentResult CreateDepartment(string QyID, string name, int parentId, int order = 1, int?id = null)
        {
            CreateDepartmentResult result = null;

            try
            {
                result = MailListApi.CreateDepartment(GetAccessToken(QyID), name, parentId, order, id);
            }
            catch (Exception ex)
            {
                LogWriter.Error(ex, string.Format("创建QyID为{0}的部门失败", QyID));
                result = MailListApi.CreateDepartment(GetAccessToken(QyID, true), name, parentId, order, id);
            }
            if (result.errcode != ReturnCode_QY.请求成功)
            {
                LogWriter.Info(string.Format("创建QyID为{0}的部门失败,原因:{1}", QyID, result.errmsg));
            }
            return(result);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 创建部门
        /// 系统应用须拥有父部门的管理权限。
        /// 注意,部门的最大层级为15层;部门总数不能超过3万个;每个部门下的节点不能超过3万个。建议保证创建的部门和对应部门成员是串行化处理。
        /// </summary>
        /// <param name="accessTokenOrAppKey">调用接口凭证(AccessToken)或AppKey(根据AccessTokenContainer.BuildingKey(corpId, corpSecret)方法获得)</param>
        /// <param name="name">部门名称。长度限制为1~64个字节,字符不能包括\:?”<>|</param>
        /// <param name="parentId">父亲部门id。根部门id为1 </param>
        /// <param name="order">在父部门中的次序。从1开始,数字越大排序越靠后</param>
        /// <param name="id">部门ID。用指定部门ID新建部门,不指定此参数时,则自动生成</param>
        /// <param name="timeOut">代理请求超时时间(毫秒)</param>
        /// <returns></returns>
        public static CreateDepartmentResult CreateDepartment(string accessTokenOrAppKey, string name, long parentId, int order = 1, long?id = null)
        {
            var result = MailListApi.CreateDepartment(accessTokenOrAppKey, name, parentId, order, id);

            return(result.ToCreateDepartmentResult());
        }
Exemplo n.º 12
0
        public ActionResult UploadUser()
        {
            try
            {
                //同步部门
                GetDepartmentListResult result = MailListApi.GetDepartmentList(WeixinWorkService.GetToken());
                var departments = dbContext.SysDepartment.OrderByDescending(c => c.DepartmentName).ToList();
                foreach (var item in departments)
                {
                    if (item.DepartmentId == Guid.Parse("2379788E-45F0-417B-A103-0B6440A9D55D"))
                    {
                        continue;
                    }

                    var            parentId = Convert.ToInt64(departments.Where(c => c.DepartmentId == item.ParentId).FirstOrDefault().WeixinWorkId);
                    DepartmentList qyDep    = result.department.Where(c => c.id == item.WeixinWorkId).FirstOrDefault();
                    if (qyDep == null)
                    {
                        var createResult = MailListApi.CreateDepartment(WeixinWorkService.GetToken(), item.DepartmentName, parentId == 0 ? 1 : parentId, 1000 - (item.OrderNo.HasValue ? item.OrderNo.Value : 0));
                        item.WeixinWorkId = createResult.id;
                    }
                    else
                    {
                        MailListApi.UpdateDepartment(WeixinWorkService.GetToken(), qyDep.id, item.DepartmentName, parentId == 0 ? 1 : parentId, 1000 - (item.OrderNo.HasValue ? item.OrderNo.Value : 0));
                    }
                    dbContext.SaveChanges();
                }
                //同步用户
                var users = dbContext.SysUser.Where(c => c.UserCode != "admin").ToList();
                foreach (var userItem in users)
                {
                    long[] longArr = new long[1];
                    longArr[0] = Convert.ToInt64(dbContext.SysDepartment.Where(c => c.DepartmentId == userItem.DepartmentId).FirstOrDefault().WeixinWorkId);
                    try
                    {
                        var memberResult = MailListApi.GetMember(WeixinWorkService.GetToken(), userItem.UserCode);
                        if (memberResult.errcode == Senparc.Weixin.ReturnCode_Work.UserID不存在)
                        {
                            MemberCreateRequest request = new MemberCreateRequest();
                            request.email      = userItem.Email;
                            request.department = longArr;
                            request.enable     = 1;
                            request.mobile     = userItem.Mobile;
                            request.name       = userItem.UserName;
                            request.userid     = userItem.UserCode;
                            MailListApi.CreateMember(WeixinWorkService.GetToken(), request);
                        }
                        else
                        {
                            MemberUpdateRequest updateRequest = new MemberUpdateRequest();
                            updateRequest.department = longArr;
                            updateRequest.email      = userItem.Email;
                            updateRequest.enable     = 1;
                            updateRequest.mobile     = userItem.Mobile;
                            updateRequest.name       = userItem.UserName;
                            updateRequest.userid     = userItem.UserCode;
                            MailListApi.UpdateMember(WeixinWorkService.GetToken(), updateRequest);
                        }
                    }
                    catch { }
                }
                return(Json(new { Code = 0, Msg = "同步成功!" }));
            }
            catch (Exception ex)
            {
                LogHelper.SaveLog(ex);
                return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" }));
            }
        }
Exemplo n.º 13
0
 public QyCallResult CreateDepartment(string accessToken, Department department)
 {
     return(MailListApi.CreateDepartment(accessToken, department.Name, department.ParentId, department.Order, department.Id).ToQyCallResult());
 }
Exemplo n.º 14
0
        public static int CreateWxDepartment(string departmentName, int parentDepartmentId, int order)
        {
            var result = MailListApi.CreateDepartment(GetToken(), departmentName, parentDepartmentId, order);

            return(result.id);
        }