Beispiel #1
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;
        }
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;
        }