示例#1
0
        public List <string> Handle(ActivityAuth auth)
        {
            ISqlMapper    mapper  = Common.GetMapperFromSession();
            RoleDao       roledao = new RoleDao(mapper);
            User_RoleDao  urdao   = new User_RoleDao(mapper);
            List <string> result  = new List <string>();
            var           role    = roledao.Query(new RoleQueryForm {
                ID = auth.Value, IsDeleted = 0
            }).FirstOrDefault();

            if (role != null)
            {
                var userrole = urdao.Query(new User_RoleQueryForm {
                    RoleID = role.ID
                });
                if (userrole != null)
                {
                    foreach (var ur in userrole)
                    {
                        if (!result.Contains(ur.UserID))
                        {
                            result.Add(ur.UserID);
                        }
                    }
                }
            }
            return(result);
        }
 public List<string> Handle(ActivityAuth auth)
 {
     List<string> result = new List<string>();
     ISqlMapper mapper = Common.GetMapperFromSession();
     UserBLL bll = new UserBLL();
     string userid = bll.GetCurrentUser().User.ID;
     //string userid = "16";
     RoleDao roledao = new RoleDao(mapper);
     User_RoleDao urdao = new User_RoleDao(mapper);
     string[] roleids = auth.Value.Split(',');
     var subroles = roledao.Query(new RoleQueryForm { ParentIDs = roleids.ToList() });
     var urs = urdao.Query(new User_RoleQueryForm { UserID = userid });
     var roles = subroles.FindAll(t => urs.Exists(p => p.RoleID == t.ID));
     if (roles == null || roles.Count == 0)
     {
         result.Add(userid);
     }
     else
     {
         IWorkflowAuthorityHandler handler = new RoleWorkflowAuthorityHandler();
         foreach (var role in roles)
         {
             if (roleids.Any(t => t == role.ParentID))
             {
                 result.AddRange(handler.Handle(new ActivityAuth
                 {
                     Value = role.ParentID,
                 }));
             }
         }
     }
     return result;
 }
示例#3
0
        public LoginResultForm Login(string username, string password)
        {
            LoginResultForm result = new LoginResultForm();
            ISqlMapper mapper = MapperHelper.GetMapper();
            UserDao userdao = new UserDao(mapper);
            UserInfoDao userInfoDao = new UserInfoDao(mapper);
            RoleDao roleDao = new RoleDao(mapper);
            User_RoleDao urdao = new User_RoleDao(mapper);
            LogonHistoryDao historyDao = new LogonHistoryDao(mapper);
            MenuDao menudao = new MenuDao(mapper);
            Menu_RoleDao mrdao = new Menu_RoleDao(mapper);
            var user = userdao.Query(new UserQueryForm { Name = username, Password = password }).FirstOrDefault();
            if (user != null)
            {
                if (user.Enabled == 0) throw new Exception("该用户已被禁用,请联系管理员!");
                string token = Guid.NewGuid().ToString().Replace("-", "");
                var userinfo = userInfoDao.Query(new UserInfoQueryForm { ID = user.ID }).FirstOrDefault();
                var ur = urdao.Query(new User_RoleQueryForm { UserID = user.ID });
                List<string> roleidlist = new List<string>();
                ur.ForEach(t =>
                {
                    roleidlist.Add(t.RoleID);
                });
                var roles = roleDao.Query(new RoleQueryForm { IDs = roleidlist });

                var mrs = mrdao.Query(new Menu_RoleQueryForm { RoleIDs = roleidlist });
                var menuids = (from mr in mrs select mr.MenuID).Distinct().ToList();

                result.Menu = menudao.Query(new MenuQueryForm { IDs = menuids, Enabled = 1 });

                UserEntireInfo u = new UserEntireInfo
                {
                    User = user,
                    UserInfo = userinfo,
                    Role = roles,
                };

                CacheItem item = new CacheItem(token, u);
                LogonHistory history = new LogonHistory
                {
                    LogonTime = DateTime.Now,
                    Token = token,
                    UserID = user.ID,
                    ActiveTime = DateTime.Now,
                };
                historyDao.Add(history);
                result.User = u;
                result.token = token;
                cache.AddItem(item, 1800);
                //MonitorCache.GetInstance().PushMessage(new CacheMessage { Message = "login user:"******",token:" + token }, SOAFramework.Library.CacheEnum.FormMonitor);
                return result;
            }
            else
            {
                throw new Exception("用户名或者密码错误!请输入正确的用户名和密码!");
            }
        }
示例#4
0
 public bool DeleteRole(string id)
 {
     if (string.IsNullOrEmpty(id)) throw new Exception("删除角色时没有ID");
     ISqlMapper mapper = MapperHelper.GetMapper();
     RoleDao dao = new RoleDao(mapper);
     Role_Module_ActionDao rmadao = new Role_Module_ActionDao(mapper);
     rmadao.Delete(new Role_Module_ActionQueryForm { RoleID = id });
     Role currentrole = dao.Query(new RoleQueryForm { ID = id }).FirstOrDefault();
     return dao.Delete(new RoleQueryForm { ID = id });
 }
示例#5
0
        /// <summary>
        /// 查询角色
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public List <FullRoleInfo> Query(RoleQueryForm form)
        {
            ISqlMapper            mapper = Common.GetMapperFromSession();
            RoleDao               dao    = new RoleDao(mapper);
            Role_Module_ActionDao rmadao = new Role_Module_ActionDao(mapper);
            List <FullRoleInfo>   result = new List <FullRoleInfo>();

            form.IsDeleted = 0;
            var roles   = dao.Query(form);
            var roleids = (from r in roles select r.ID).ToList();
            var rmas    = rmadao.Query(new Role_Module_ActionQueryForm {
                RoleIDs = roleids
            });
            AuthorityMapping mapping = XMLHelper.DeserializeFromFile <AuthorityMapping>(Common.AuthorityMappingFile);

            foreach (var role in roles)
            {
                var data       = role.ConvertTo <FullRoleInfo>();
                var parentrole = roles.Find(t => t.ID == role.ParentID);
                if (parentrole != null)
                {
                    data.ParentRoleName = parentrole.Name;
                }
                #region 查询权限
                var role_rmas = rmas.FindAll(t => t.RoleID.Equals(role.ID));
                data.Authority = new List <AuthorityNodeForCheck>();
                foreach (var auth in mapping.AuthNode)
                {
                    auth.Checked = (from au in auth.Item
                                    from r in role_rmas
                                    where au.ModuleID == r.ModuleID && au.ActionID == r.ActionID
                                    select au).Count() == auth.Item.Count;
                    data.Authority.Add(new AuthorityNodeForCheck {
                        ID = auth.ID, Checked = auth.Checked, Name = auth.Name
                    });
                }
                #endregion
                result.Add(data);
            }
            return(result);
        }
示例#6
0
        public bool DeleteRole(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new Exception("删除角色时没有ID");
            }
            ISqlMapper            mapper = Common.GetMapperFromSession();
            RoleDao               dao    = new RoleDao(mapper);
            Role_Module_ActionDao rmadao = new Role_Module_ActionDao(mapper);

            rmadao.Delete(new Role_Module_ActionQueryForm {
                RoleID = id
            });
            Role currentrole = dao.Query(new RoleQueryForm {
                ID = id
            }).FirstOrDefault();

            return(dao.Delete(new RoleQueryForm {
                ID = id
            }));
        }
        public List <string> Handle(ActivityAuth auth)
        {
            List <string> result = new List <string>();
            ISqlMapper    mapper = Common.GetMapperFromSession();
            UserBLL       bll    = new UserBLL();
            string        userid = bll.GetCurrentUser().User.ID;
            //string userid = "16";
            RoleDao      roledao = new RoleDao(mapper);
            User_RoleDao urdao   = new User_RoleDao(mapper);

            string[] roleids  = auth.Value.Split(',');
            var      subroles = roledao.Query(new RoleQueryForm {
                ParentIDs = roleids.ToList()
            });
            var urs = urdao.Query(new User_RoleQueryForm {
                UserID = userid
            });
            var roles = subroles.FindAll(t => urs.Exists(p => p.RoleID == t.ID));

            if (roles == null || roles.Count == 0)
            {
                result.Add(userid);
            }
            else
            {
                IWorkflowAuthorityHandler handler = new RoleWorkflowAuthorityHandler();
                foreach (var role in roles)
                {
                    if (roleids.Any(t => t == role.ParentID))
                    {
                        result.AddRange(handler.Handle(new ActivityAuth
                        {
                            Value = role.ParentID,
                        }));
                    }
                }
            }
            return(result);
        }
 public List<string> Handle(ActivityAuth auth)
 {
     ISqlMapper mapper = Common.GetMapperFromSession();
     RoleDao roledao = new RoleDao(mapper);
     User_RoleDao urdao = new User_RoleDao(mapper);
     List<string> result = new List<string>();
     var role = roledao.Query(new RoleQueryForm { ID = auth.Value, IsDeleted = 0 }).FirstOrDefault();
     if (role != null)
     {
         var userrole = urdao.Query(new User_RoleQueryForm { RoleID = role.ID });
         if (userrole != null)
         {
             foreach (var ur in userrole)
             {
                 if (!result.Contains(ur.UserID))
                 {
                     result.Add(ur.UserID);
                 }
             }
         }
     }
     return result;
 }
        private void RescGetUserIDs(List <string> result, string roleid, int level, int deepth)
        {
            if (deepth > level)
            {
                return;
            }
            var role = roledao.Query(new RoleQueryForm {
                ID = roleid
            }).FirstOrDefault();

            if (role == null || string.IsNullOrEmpty(role.ParentID))
            {
                var urs = urdao.Query(new User_RoleQueryForm {
                    RoleID = roleid
                });
                var userids = (from ur in urs
                               select ur.UserID).ToList();
                result.AddRange(userids);
                return;
            }
            string parentroleid = role.ParentID;

            if (deepth == level)
            {
                var urs = urdao.Query(new User_RoleQueryForm {
                    RoleID = parentroleid
                });
                var userids = (from ur in urs
                               select ur.UserID).ToList();
                result.AddRange(userids);
            }
            else
            {
                deepth++;
                RescGetUserIDs(result, parentroleid, level, deepth);
            }
        }
示例#10
0
        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        public List <FullUser> Query(FullUserQueryForm form)
        {
            ISqlMapper   mapper   = Common.GetMapperFromSession();
            FullUserDao  dao      = new FullUserDao(mapper);
            User_RoleDao urdao    = new User_RoleDao(mapper);
            RoleDao      roledao  = new RoleDao(mapper);
            var          userlist = dao.Query(form);
            var          userids  = (from u in userlist select u.ID).ToList();
            var          urs      = urdao.Query(new User_RoleQueryForm {
                UserIDs = userids
            });
            var roleids = (from ur in urs select ur.RoleID).Distinct().ToList();
            var roles   = roledao.Query(new RoleQueryForm {
                IDs = roleids
            });

            foreach (var u in userlist)
            {
                var ur_temp  = urs.FindAll(t => t.UserID == u.ID);
                var rolelist = (from ur in ur_temp join r in roles on ur.RoleID equals r.ID select r).ToList();
                u.RoleList = rolelist;
            }
            return(userlist);
        }
示例#11
0
        public bool Update(Model.User user, UserInfo ui, List <string> roleidlist)
        {
            #region risk update
            ISqlMapper  mapper = Common.GetMapperFromSession();
            UserDao     udao   = new UserDao(mapper);
            UserInfoDao uidao  = new UserInfoDao(mapper);
            if (user != null)
            {
                Model.User entity = new User
                {
                    ID      = user.ID,
                    Enabled = user.Enabled,
                };
                udao.Update(new UserUpdateForm {
                    Entity = entity, UserQueryForm = new UserQueryForm {
                        ID = user.ID
                    }
                });
            }
            if (ui != null)
            {
                uidao.Update(new UserInfoUpdateForm {
                    Entity = ui, UserInfoQueryForm = new UserInfoQueryForm {
                        ID = ui.ID
                    }
                });
            }
            if (roleidlist != null)
            {
                User_RoleDao urdao = new User_RoleDao(mapper);
                urdao.Delete(new User_RoleQueryForm {
                    UserID = user.ID
                });
                foreach (var role in roleidlist)
                {
                    User_Role ur = new User_Role {
                        RoleID = role, UserID = user.ID
                    };
                    urdao.Add(ur);
                }
            }
            #endregion

            #region weixin api
            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();
            var user_temp = udao.Query(new UserQueryForm {
                ID = user.ID
            }).FirstOrDefault();
            var ui_temp = uidao.Query(new UserInfoQueryForm {
                ID = user.ID
            }).FirstOrDefault();
            try
            {
                SOAFramework.Library.WeiXin.WeiXinApi.User.Update(new SOAFramework.Library.WeiXin.User
                {
                    department = weixinids,
                    mobile     = ui_temp.Mobile,
                    name       = ui_temp.CnName,
                    weixinid   = ui_temp.WX,
                    userid     = user_temp.Name,
                    enable     = 1,
                });
            }
            catch (SOAFramework.Library.WeiXin.WeiXinException ex)
            {
                switch (ex.Code)
                {
                case "60111":    //如果微信上不存在用户,就新建
                    SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                    {
                        enable     = 1,
                        userid     = user_temp.Name,
                        name       = ui_temp.CnName,
                        mobile     = ui_temp.Mobile,
                        weixinid   = ui_temp.WX,
                        department = weixinids,
                    });
                    break;

                default:
                    throw ex;
                }
            }
            #endregion
            return(true);
        }
示例#12
0
 public List<FullUser> Query(FullUserQueryForm form)
 {
     ISqlMapper mapper = MapperHelper.GetMapper();
     UserDao dao = new UserDao(mapper);
     User_RoleDao urdao = new User_RoleDao(mapper);
     RoleDao roledao = new RoleDao(mapper);
     var users = dao.QueryFullUser(form);
     var userids = (from u in users select u.ID).ToList();
     var urs = urdao.Query(new User_RoleQueryForm { UserIDs = userids });
     var roleids = (from ur in urs select ur.RoleID).Distinct().ToList();
     var roles = roledao.Query(new RoleQueryForm { IDs = roleids });
     foreach (var u in users)
     {
         u.Roles = (from ur in urs join role in roles on ur.RoleID equals role.ID
                    where ur.UserID.Equals(u.ID) select role).ToList();
     }
     return users;
 }
示例#13
0
        public bool Update(Model.User user, UserInfo ui, List<string> roleidlist)
        {
            #region risk update
            ISqlMapper mapper = Common.GetMapperFromSession();
            UserDao udao = new UserDao(mapper);
            UserInfoDao uidao = new UserInfoDao(mapper);
            if (user != null)
            {
                Model.User entity = new User
                {
                    ID = user.ID,
                    Enabled = user.Enabled,
                };
                udao.Update(new UserUpdateForm { Entity = entity, UserQueryForm = new UserQueryForm { ID = user.ID } });
            }
            if (ui != null)
            {
                uidao.Update(new UserInfoUpdateForm { Entity = ui, UserInfoQueryForm = new UserInfoQueryForm { ID = ui.ID } });
            }
            if (roleidlist != null)
            {
                User_RoleDao urdao = new User_RoleDao(mapper);
                urdao.Delete(new User_RoleQueryForm { UserID = user.ID });
                foreach (var role in roleidlist)
                {
                    User_Role ur = new User_Role { RoleID = role, UserID = user.ID };
                    urdao.Add(ur);
                }
            }
            #endregion

            #region weixin api
            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();
            var user_temp = udao.Query(new UserQueryForm { ID = user.ID }).FirstOrDefault();
            var ui_temp = uidao.Query(new UserInfoQueryForm { ID = user.ID }).FirstOrDefault();
            try
            {
                SOAFramework.Library.WeiXin.WeiXinApi.User.Update(new SOAFramework.Library.WeiXin.User
                {
                    department = weixinids,
                    mobile = ui_temp.Mobile,
                    name = ui_temp.CnName,
                    weixinid = ui_temp.WX,
                    userid = user_temp.Name,
                    enable = 1,
                });
            }
            catch (SOAFramework.Library.WeiXin.WeiXinException ex)
            {
                switch (ex.Code)
                {
                    case "60111"://如果微信上不存在用户,就新建
                        SOAFramework.Library.WeiXin.WeiXinApi.User.Create(new SOAFramework.Library.WeiXin.User
                        {
                            enable = 1,
                            userid = user_temp.Name,
                            name = ui_temp.CnName,
                            mobile = ui_temp.Mobile,
                            weixinid = ui_temp.WX,
                            department = weixinids,
                        });
                        break;
                    default:
                        throw ex;
                }
            }
            #endregion
            return true;
        }
示例#14
0
 /// <summary>
 /// 查询所有用户
 /// </summary>
 /// <returns></returns>
 public List<FullUser> Query(FullUserQueryForm form)
 {
     ISqlMapper mapper = Common.GetMapperFromSession();
     FullUserDao dao = new FullUserDao(mapper);
     User_RoleDao urdao = new User_RoleDao(mapper);
     RoleDao roledao = new RoleDao(mapper);
     var userlist = dao.Query(form);
     var userids = (from u in userlist select u.ID).ToList();
     var urs = urdao.Query(new User_RoleQueryForm { UserIDs = userids });
     var roleids = (from ur in urs select ur.RoleID).Distinct().ToList();
     var roles = roledao.Query(new RoleQueryForm { IDs = roleids });
     foreach (var u in userlist)
     {
         var ur_temp = urs.FindAll(t => t.UserID == u.ID);
         var rolelist = (from ur in ur_temp join r in roles on ur.RoleID equals r.ID select r).ToList();
         u.RoleList = rolelist;
     }
     return userlist;
 }
示例#15
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;
        }
示例#16
0
        /// <summary>
        /// 查询角色
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public List<FullRoleInfo> QueryFullRole(RoleQueryForm form)
        {
            ISqlMapper mapper = MapperHelper.GetMapper();
            RoleDao dao = new RoleDao(mapper);
            Role_Module_ActionDao rmadao = new Role_Module_ActionDao(mapper);
            Menu_RoleDao mrdao = new Menu_RoleDao(mapper);
            MenuDao menudao = new MenuDao(mapper);
            List<FullRoleInfo> result = new List<FullRoleInfo>();
            form.IsDeleted = 0;
            var roles = dao.Query(form);
            var roleids = (from r in roles select r.ID).ToList();
            var rmas = rmadao.Query(new Role_Module_ActionQueryForm { RoleIDs = roleids });
            var mrs = mrdao.Query(new Menu_RoleQueryForm { RoleIDs = roleids });
            var menuids = (from mr in mrs select mr.MenuID).Distinct().ToList();
            var menus = menudao.Query(new MenuQueryForm { IDs = menuids, Enabled = 1 });
            AuthorityMapping mapping = XMLHelper.DeserializeFromFile<AuthorityMapping>(Common.AuthorityMappingFile);
            foreach (var role in roles)
            {
                var data = role.ConvertTo<FullRoleInfo>();
                var parentrole = roles.Find(t => t.ID == role.ParentID);
                if (parentrole != null) data.ParentRoleName = parentrole.Name;
                #region 查询权限
                var role_rmas = rmas.FindAll(t => t.RoleID.Equals(role.ID));
                data.Authority = new List<AuthorityNodeForCheck>();
                foreach (var auth in mapping.AuthNode)
                {
                    auth.Checked = (from au in auth.Item
                                    from r in role_rmas
                                    where au.ModuleID == r.ModuleID && au.ActionID == r.ActionID
                                    select au).Count() == auth.Item.Count;
                    data.Authority.Add(new AuthorityNodeForCheck { ID = auth.ID, Checked = auth.Checked, Name = auth.Name });
                }
                #endregion

                #region 查询菜单
                data.Menus = (from mr in mrs join m in menus on mr.MenuID equals m.ID where mr.RoleID.Equals(role.ID) select m).ToList();
                #endregion
                result.Add(data);
            }
            return result;
        }
示例#17
0
 public List<Role> Query(RoleQueryForm form)
 {
     ISqlMapper mapper = MapperHelper.GetMapper();
     RoleDao dao = new RoleDao(mapper);
     return dao.Query(form);
 }
示例#18
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);
        }