Beispiel #1
0
        public string Add(Model.User user, UserInfo ui, List<string> roleidlist)
        {
            #region risk user
            ISqlMapper mapper = Common.GetMapperFromSession();
            if (user == null)
            {
                throw new Exception("user不能为null!");
            }
            UserDao dao = new UserDao(mapper);
            var exist = dao.Query(new UserQueryForm { Name = user.Name });
            if (exist.Count > 0) throw new Exception("已存在用户名:" + user.Name);
            if (string.IsNullOrEmpty(ui.WX) && string.IsNullOrEmpty(ui.Mobile)) throw new Exception("微信号或者手机不能为空");
            string id = dao.Add(user);
            if (ui == null)
            {
                ui = new UserInfo();
            }
            UserInfoDao infodao = new UserInfoDao(mapper);
            ui.ID = id;
            infodao.Add(ui);
            if (roleidlist == null) return id;
            User_RoleDao urdao = new User_RoleDao(mapper);
            foreach (var role in roleidlist)
            {
                User_Role ur = new User_Role { RoleID = role, UserID = user.ID };
                urdao.Add(ur);
            }
            #endregion

            #region weixin user
            RoleDao roledao = new RoleDao(mapper);
            var roles = roledao.Query(new RoleQueryForm { IDs = roleidlist });
            var weixinids = (from r in roles
                             where !string.IsNullOrEmpty(r.WeiXinID)
                             select Convert.ToInt32(r.WeiXinID)).ToArray();
            try
            {
                SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                {
                    department = weixinids,
                    enable = 1,
                    mobile = ui.Mobile,
                    name = ui.CnName,
                    weixinid = ui.WX,
                    userid = user.Name,
                });
            }
            catch (SOAFramework.Library.WeiXin.WeiXinException ex)
            {
                switch (ex.Code)
                {
                    case "60004":
                    case "60003":
                        foreach (var role in roles)
                        {
                            //部门不存在就新建部门
                            Role parentrole = null;
                            if (!string.IsNullOrEmpty(role.ParentID)) roledao.Query(new RoleQueryForm { ID = role.ParentID }).FirstOrDefault();
                            var department = new SOAFramework.Library.WeiXin.Department
                            {
                                name = role.Name,
                            };
                            if (parentrole != null) department.parentid = parentrole.WeiXinID;
                            var response = SOAFramework.Library.WeiXin.WeiXinApi.Department.Create(department);
                            roledao.Update(new RoleUpdateForm
                            {
                                Entity = new Role { WeiXinID = response.id },
                                RoleQueryForm = new RoleQueryForm { ID = role.ID },
                            });
                        }
                        SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                        {
                            department = weixinids,
                            enable = 1,
                            mobile = ui.Mobile,
                            name = ui.CnName,
                            weixinid = ui.WX,
                            userid = user.Name,
                        });
                        break;
                    case "60102"://用户已存在
                        SOAFramework.Library.WeiXin.WeiXinApi.User.Update(new SOAFramework.Library.WeiXin.User
                        {
                            department = weixinids,
                            mobile = ui.Mobile,
                            name = ui.CnName,
                            weixinid = ui.WX,
                            userid = user.Name,
                        });
                        break;
                    default:
                        throw ex;
                }
            }
            #endregion
            return id;
        }
Beispiel #2
0
        public string Add(Model.User user, UserInfo ui, List <string> roleidlist)
        {
            #region risk user
            ISqlMapper mapper = Common.GetMapperFromSession();
            if (user == null)
            {
                throw new Exception("user不能为null!");
            }
            UserDao dao   = new UserDao(mapper);
            var     exist = dao.Query(new UserQueryForm {
                Name = user.Name
            });
            if (exist.Count > 0)
            {
                throw new Exception("已存在用户名:" + user.Name);
            }
            if (string.IsNullOrEmpty(ui.WX) && string.IsNullOrEmpty(ui.Mobile))
            {
                throw new Exception("微信号或者手机不能为空");
            }
            string id = dao.Add(user);
            if (ui == null)
            {
                ui = new UserInfo();
            }
            UserInfoDao infodao = new UserInfoDao(mapper);
            ui.ID = id;
            infodao.Add(ui);
            if (roleidlist == null)
            {
                return(id);
            }
            User_RoleDao urdao = new User_RoleDao(mapper);
            foreach (var role in roleidlist)
            {
                User_Role ur = new User_Role {
                    RoleID = role, UserID = user.ID
                };
                urdao.Add(ur);
            }
            #endregion

            #region weixin user
            RoleDao roledao = new RoleDao(mapper);
            var     roles   = roledao.Query(new RoleQueryForm {
                IDs = roleidlist
            });
            var weixinids = (from r in roles
                             where !string.IsNullOrEmpty(r.WeiXinID)
                             select Convert.ToInt32(r.WeiXinID)).ToArray();
            try
            {
                SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                {
                    department = weixinids,
                    enable     = 1,
                    mobile     = ui.Mobile,
                    name       = ui.CnName,
                    weixinid   = ui.WX,
                    userid     = user.Name,
                });
            }
            catch (SOAFramework.Library.WeiXin.WeiXinException ex)
            {
                switch (ex.Code)
                {
                case "60004":
                case "60003":
                    foreach (var role in roles)
                    {
                        //部门不存在就新建部门
                        Role parentrole = null;
                        if (!string.IsNullOrEmpty(role.ParentID))
                        {
                            roledao.Query(new RoleQueryForm {
                                ID = role.ParentID
                            }).FirstOrDefault();
                        }
                        var department = new SOAFramework.Library.WeiXin.Department
                        {
                            name = role.Name,
                        };
                        if (parentrole != null)
                        {
                            department.parentid = parentrole.WeiXinID;
                        }
                        var response = SOAFramework.Library.WeiXin.WeiXinApi.Department.Create(department);
                        roledao.Update(new RoleUpdateForm
                        {
                            Entity = new Role {
                                WeiXinID = response.id
                            },
                            RoleQueryForm = new RoleQueryForm {
                                ID = role.ID
                            },
                        });
                    }
                    SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                    {
                        department = weixinids,
                        enable     = 1,
                        mobile     = ui.Mobile,
                        name       = ui.CnName,
                        weixinid   = ui.WX,
                        userid     = user.Name,
                    });
                    break;

                case "60102":    //用户已存在
                    SOAFramework.Library.WeiXin.WeiXinApi.User.Update(new SOAFramework.Library.WeiXin.User
                    {
                        department = weixinids,
                        mobile     = ui.Mobile,
                        name       = ui.CnName,
                        weixinid   = ui.WX,
                        userid     = user.Name,
                    });
                    break;

                default:
                    throw ex;
                }
            }
            #endregion
            return(id);
        }