From 8d2d4995f718dcd7ccb153e17418c7532c07d3fb Mon Sep 17 00:00:00 2001 From: Minho Date: Mon, 16 Apr 2018 17:35:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=94=A8=E6=88=B7=E7=BB=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/BookController.go | 10 +- controllers/ManagerController.go | 102 ++++++++++- models/MemberGroupMemberModel.go | 65 +++++-- models/MemberGroupModel.go | 22 ++- models/dashboard.go | 9 +- models/member.go | 19 ++- models/member_result.go | 6 +- routers/router.go | 9 +- views/book/users.tpl | 1 - views/manager/index.tpl | 8 +- views/manager/member_group_list.tpl | 4 +- views/manager/member_group_member_list.tpl | 189 ++++----------------- 12 files changed, 249 insertions(+), 195 deletions(-) diff --git a/controllers/BookController.go b/controllers/BookController.go index a11fe5e9..15f79169 100644 --- a/controllers/BookController.go +++ b/controllers/BookController.go @@ -705,13 +705,13 @@ func (c *BookController) SaveSort() { c.Abort("404") } - book_id := 0 + bookId := 0 if c.Member.IsAdministrator() { book, err := models.NewBook().FindByFieldFirst("identify", identify) if err != nil { } - book_id = book.BookId + bookId = book.BookId } else { bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) if err != nil { @@ -722,7 +722,7 @@ func (c *BookController) SaveSort() { if bookResult.RoleId == conf.BookObserver { c.JsonResult(6002, "项目不存在或权限不足") } - book_id = bookResult.BookId + bookId = bookResult.BookId } content := c.Ctx.Input.RequestBody @@ -743,7 +743,7 @@ func (c *BookController) SaveSort() { beego.Error(err) continue } - if doc.BookId != book_id { + if doc.BookId != bookId { logs.Info("%s", "权限错误") continue } @@ -758,7 +758,7 @@ func (c *BookController) SaveSort() { continue } if parent_id > 0 { - if parent, err := models.NewDocument().Find(int(parent_id)); err != nil || parent.BookId != book_id { + if parent, err := models.NewDocument().Find(int(parent_id)); err != nil || parent.BookId != bookId { continue } } diff --git a/controllers/ManagerController.go b/controllers/ManagerController.go index 8fb89254..3ac0bbc9 100644 --- a/controllers/ManagerController.go +++ b/controllers/ManagerController.go @@ -757,7 +757,7 @@ func (c *ManagerController) MemberGroupEdit() { c.Prepare() c.TplName = "manager/member_group_edit.tpl" - if c.Member.Role != 0 { + if !c.Member.IsSuperAdministrator() { c.ShowErrorPage(500,"只有超级管理员才能编辑或添加用户组") } @@ -818,7 +818,7 @@ func (c *ManagerController) MemberGroupEdit() { func (c *ManagerController) MemberGroupDelete() { c.Prepare() - if c.Member.Role != 0 { + if !c.Member.IsSuperAdministrator() { c.JsonResult(5001,"只有超级管理员才能删除用户组") } @@ -877,6 +877,101 @@ func (c *ManagerController) MemberGroupMemberList() { //添加用户组成员 func (c *ManagerController) MemberGroupMemberEdit() { c.Prepare() + if !c.Member.IsSuperAdministrator() { + c.JsonResult(5000,"只有超级管理员才能添加用户") + } + if c.Ctx.Input.IsPost() { + memberId, err := c.GetInt("member_id", 0) + + if err != nil || memberId <= 0 { + c.JsonResult(6001, "用户参数错误") + } + groupId, err := c.GetInt("group_id", 0) + + if err != nil || groupId <= 0 { + c.JsonResult(6002, "用户组参数错误") + } + + if ! models.NewMember().Exist("member_id", memberId) { + beego.Error("用户不存在 =>", err) + c.JsonResult(6001, "用户不存在") + } + + if models.NewMemberGroupMembers().IsJoin(groupId, memberId) { + c.JsonResult(6003, "用户已加入当前用户组") + } + if !models.NewMember().Exist("member_id", memberId) { + c.JsonResult(6004, "用户不存在") + } + + memberGroupMember := models.NewMemberGroupMembers() + + memberGroupMember.MemberId = memberId + memberGroupMember.CreateAt = c.Member.MemberId + memberGroupMember.GroupId = groupId + memberGroupMember.CreateTime = time.Now() + + if err := memberGroupMember.InsertOrUpdate(); err != nil { + beego.Error("添加用户失败 =>", err) + c.JsonResult(6005, "添加用户失败") + } else { + c.JsonResult(0, "添加成功",memberGroupMember.ToMemberRelationshipResult()) + } + } else { + c.ShowErrorPage(405, "不支持的请求方式") + } +} + +//删除用户组中的成员 +func (c *ManagerController) MemberGroupMemberDelete() { + c.Prepare() + if !c.Member.IsSuperAdministrator() { + c.JsonResult(5000,"只有超级管理员才能删除用户") + } + if c.Ctx.Input.IsPost() { + + memberGroupId,err := c.GetInt("id",0) + + if err != nil || memberGroupId <= 0 { + c.JsonResult(500,"参数错误") + } + + if err := models.NewMemberGroupMembers().Delete(memberGroupId);err != nil { + beego.Error("添加用户失败 =>",err) + c.JsonResult(6005,"删除用户失败") + }else{ + c.JsonResult(0,"删除成功") + } + }else{ + c.ShowErrorPage(405,"不支持的请求方式") + } +} + +func (c *ManagerController) MemberGroupMemberSearch() { + c.Prepare() + groupId,err := c.GetInt("group_id",0) + + if err != nil || groupId <= 0 { + c.JsonResult(6002,"用户组参数错误") + } + q := strings.TrimSpace(c.GetString("q")) + + members,err := models.NewMemberGroupMembers().FindMemberGroupMemberNoJoinSearchResult(groupId,q) + + result := models.SelectMemberResult{} + items := make([]models.KeyValueItem, 0) + + for _, member := range members { + item := models.KeyValueItem{} + item.Id = member.MemberId + item.Text = member.Account + items = append(items, item) + } + + result.Result = items + + c.JsonResult(0, "OK", result) + } @@ -905,9 +1000,6 @@ func (c *ManagerController) MemberGroupMemberEdit() { - - - diff --git a/models/MemberGroupMemberModel.go b/models/MemberGroupMemberModel.go index 4b5c90a5..0bbaac72 100644 --- a/models/MemberGroupMemberModel.go +++ b/models/MemberGroupMemberModel.go @@ -5,7 +5,6 @@ import ( "time" "github.com/astaxie/beego/orm" "github.com/astaxie/beego" - "fmt" "errors" ) @@ -18,14 +17,21 @@ type MemberGroupMembers struct { } type MemberGroupMemberResult struct { - GroupMemberId int - MemberId int - Account string - RealName string - Avatar string - GroupId int - CreateTime time.Time - CreateAt int + GroupMemberId int `json:"group_member_id"` + MemberId int `json:"member_id"` + Account string `json:"account"` + RealName string `json:"real_name"` + RoleName string `json:"role_name"` + Avatar string `json:"avatar"` + GroupId int `json:"group_id"` + CreateTime time.Time `json:"create_time"` + CreateAt int `json:"create_at"` +} + +//搜索未加入当前用户组的用户信息 +type MemberGroupMemberNoJoinSearchResult struct { + MemberId int `json:"member_id"` + Account string `json:"account"` } // TableName 获取对应数据库表名. @@ -86,6 +92,14 @@ func (m *MemberGroupMembers) FindByGroupId(groupId int) ([]*MemberGroupMemberRes } return groupMembers,nil } + +//判断一个用户是否加入了指定用户组 +func (m *MemberGroupMembers) IsJoin(groupId ,memberId int) (bool) { + o := orm.NewOrm() + + return o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).Filter("member_id",memberId).Exist() +} + //添加或更新用户组成员 func (m *MemberGroupMembers) InsertOrUpdate(cols ...string) error { o := orm.NewOrm() @@ -104,16 +118,22 @@ func (m *MemberGroupMembers) InsertOrUpdate(cols ...string) error { if err != nil { beego.Error("添加用户组成员失败 =>",err) }else{ - o.Raw(fmt.Sprintf("UPDATE %s SET group_number=group_number+1 WHERE group_id=%d",NewMemberGroup().TableNameWithPrefix(), m.GroupId)).Exec() + go NewMemberGroup().ResetMemberGroupNumber(m.GroupId) } return err } } + //删除用户组成员 func (m *MemberGroupMembers) Delete(id int) error { o := orm.NewOrm() + if err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_member_id",id).One(m);err != nil { + beego.Error("删除用户组成员失败 =>",err) + return err + } + i,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_member_id",id).Delete() if err != nil { @@ -124,6 +144,8 @@ func (m *MemberGroupMembers) Delete(id int) error { beego.Info("删除用户组成员返回行数 =>",i) } + go NewMemberGroup().ResetMemberGroupNumber(m.GroupId) + return nil } @@ -168,22 +190,37 @@ func (m *MemberGroupMembers) ToMemberRelationshipResult() *MemberGroupMemberResu memberGroupMemberResult.CreateAt = m.CreateAt memberGroupMemberResult.CreateTime = m.CreateTime + if m.MemberId > 0 { - o := orm.NewOrm() - member := NewMember() - _,err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", m.MemberId).All(&member) + member,err := NewMember().Find(memberGroupMemberResult.MemberId) if err != nil { beego.Error("查询用户组成员信息时出错 =>",err) }else{ memberGroupMemberResult.RealName = member.RealName memberGroupMemberResult.Avatar = member.Avatar memberGroupMemberResult.Account = member.Account + memberGroupMemberResult.RoleName = member.RoleName } } return memberGroupMemberResult } +//获取未加入用户组的用户成员 +func (m *MemberGroupMembers) FindMemberGroupMemberNoJoinSearchResult(groupId int,q string) ([]*MemberGroupMemberNoJoinSearchResult,error) { + o := orm.NewOrm() + + sql := "select member.member_id,member.account from md_members as member left join md_member_group_members as member_group on member_group.member_id = member.member_id and member_group.group_id=? where member_group.member_id isnull and account like ?limit 20;" + var memberGroupMembers []*MemberGroupMemberNoJoinSearchResult + + _,err := o.Raw(sql,groupId,"%" + q + "%").QueryRows(&memberGroupMembers) + + if err != nil { + beego.Error("获取未加入用户组的用户失败 =>",err) + } + return memberGroupMembers,err +} + @@ -215,8 +252,6 @@ func (m *MemberGroupMembers) ToMemberRelationshipResult() *MemberGroupMemberResu - - diff --git a/models/MemberGroupModel.go b/models/MemberGroupModel.go index bfaf9c08..024b0153 100644 --- a/models/MemberGroupModel.go +++ b/models/MemberGroupModel.go @@ -160,8 +160,26 @@ func (m *MemberGroup) InsertOrUpdate(cols...string) error { return err } - - +//重置用户组数量 +func (m *MemberGroup) ResetMemberGroupNumber(groupId int) error { + o := orm.NewOrm() + + i,err := o.QueryTable(NewMemberGroupMembers().TableNameWithPrefix()).Filter("group_id",groupId).Count() + if err != nil { + beego.Error("重置用户组用户数量失败 =>",err) + }else{ + err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).One(m) + if err != nil { + beego.Error("重置用户组用户数量失败 =>",err) + return err + }else{ + m.GroupNumber = int(i) + _,err = o.Update(m) + return err + } + } + return nil +} diff --git a/models/dashboard.go b/models/dashboard.go index 9b9ae45f..1afac326 100644 --- a/models/dashboard.go +++ b/models/dashboard.go @@ -8,6 +8,7 @@ type Dashboard struct { MemberNumber int64 `json:"member_number"` CommentNumber int64 `json:"comment_number"` AttachmentNumber int64 `json:"attachment_number"` + MemberGroupNumber int64 `json:"member_group_number"` } func NewDashboard() *Dashboard { @@ -30,9 +31,13 @@ func (m *Dashboard) Query() *Dashboard { //comment_number,_ := o.QueryTable(NewComment().TableNameWithPrefix()).Count() m.CommentNumber = 0 - attachment_number, _ := o.QueryTable(NewAttachment().TableNameWithPrefix()).Count() + attachmentNumber, _ := o.QueryTable(NewAttachment().TableNameWithPrefix()).Count() - m.AttachmentNumber = attachment_number + m.AttachmentNumber = attachmentNumber + + memberGroupNumber,_ := o.QueryTable(NewMemberGroup().TableNameWithPrefix()).Count() + + m.MemberGroupNumber = memberGroupNumber return m } diff --git a/models/member.go b/models/member.go index d31651c8..f1616e2c 100644 --- a/models/member.go +++ b/models/member.go @@ -236,6 +236,7 @@ func (m *Member) FindByAccount(account string) (*Member, error) { } return m, err } + //批量查询用户 func (m *Member) FindByAccountList(accounts ...string) ([]*Member,error) { o := orm.NewOrm() @@ -277,12 +278,26 @@ func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int, error) { return members, int(totalCount), nil } +//指定的用户是否存在 +func (m *Member) Exist(field string,value interface{}) bool { + o := orm.NewOrm() + + return o.QueryTable(m.TableNameWithPrefix()).Filter(field,value).Exist() +} +//是否是管理员 func (c *Member) IsAdministrator() bool { if c == nil || c.MemberId <= 0 { return false } return c.Role == 0 || c.Role == 1 } +//是否是超级管理员 +func (c *Member) IsSuperAdministrator() bool { + if c == nil || c.MemberId <= 0 { + return false + } + return c.Role == 0 +} //根据指定字段查找用户. func (m *Member) FindByFieldFirst(field string, value interface{}) (*Member, error) { @@ -294,7 +309,7 @@ func (m *Member) FindByFieldFirst(field string, value interface{}) (*Member, err } //校验用户. -func (m *Member) Valid(is_hash_password bool) error { +func (m *Member) Valid(isHashPassword bool) error { //邮箱不能为空 if m.Email == "" { @@ -315,7 +330,7 @@ func (m *Member) Valid(is_hash_password bool) error { return ErrMemberEmailFormatError } //如果是未加密密码,需要校验密码格式 - if !is_hash_password { + if !isHashPassword { if l := strings.Count(m.Password, ""); m.Password == "" || l > 50 || l < 6 { return ErrMemberPasswordFormatError } diff --git a/models/member_result.go b/models/member_result.go index 839b4231..cc5898c5 100644 --- a/models/member_result.go +++ b/models/member_result.go @@ -73,9 +73,9 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(bookId, pageIndex, pageS sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as member ON rel.member_id = member.member_id WHERE rel.book_id = ?" - var total_count int + var totalCount int - err := o.Raw(sql2, bookId).QueryRow(&total_count) + err := o.Raw(sql2, bookId).QueryRow(&totalCount) if err != nil { return members, 0, err @@ -92,7 +92,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(bookId, pageIndex, pageS for _, item := range members { item.ResolveRoleName() } - return members, total_count, nil + return members, totalCount, nil } // 查询指定文档中不存在的用户列表 diff --git a/routers/router.go b/routers/router.go index 80bd1490..112d6bf9 100644 --- a/routers/router.go +++ b/routers/router.go @@ -36,11 +36,12 @@ func init() { beego.Router("/manager/label/list", &controllers.ManagerController{},"get:LabelList") beego.Router("/manager/label/delete/:id", &controllers.ManagerController{},"post:LabelDelete") beego.Router("/manager/user_groups", &controllers.ManagerController{},"*:MemberGroupList") - beego.Router("/manager/user_groups/edit/?:id", &controllers.ManagerController{},"*:MemberGroupEdit") + beego.Router("/manager/user_groups/edit/?:id:int", &controllers.ManagerController{},"*:MemberGroupEdit") beego.Router("/manager/user_group/delete/", &controllers.ManagerController{},"*:MemberGroupDelete") - beego.Router("/manager/user_group_member/:id", &controllers.ManagerController{},"*:MemberGroupMemberList") - beego.Router("/manager/user_group_member/edit/?:id", &controllers.ManagerController{},"*:MemberGroupMemberEdit") - + beego.Router("/manager/user_group_member/edit/?:id:int", &controllers.ManagerController{},"post:MemberGroupMemberEdit") + beego.Router("/manager/user_group_member/delete", &controllers.ManagerController{},"post:MemberGroupMemberDelete") + beego.Router("/manager/user_group_member/search", &controllers.ManagerController{},"*:MemberGroupMemberSearch") + beego.Router("/manager/user_group_member/list/:id:int", &controllers.ManagerController{},"*:MemberGroupMemberList") beego.Router("/setting", &controllers.SettingController{}, "*:Index") beego.Router("/setting/password", &controllers.SettingController{}, "*:Password") diff --git a/views/book/users.tpl b/views/book/users.tpl index b3706194..0b3534b1 100644 --- a/views/book/users.tpl +++ b/views/book/users.tpl @@ -190,7 +190,6 @@ }; }, processResults: function (data, params) { - console.log(data) return { results : data.data.results } diff --git a/views/manager/index.tpl b/views/manager/index.tpl index be19f36c..9fd1b7ab 100644 --- a/views/manager/index.tpl +++ b/views/manager/index.tpl @@ -47,10 +47,15 @@ {{.Model.DocumentNumber}} - + 会员数量 {{.Model.MemberNumber}} + + + 用户组数量 + {{.Model.MemberGroupNumber}} + - + @@ -56,39 +56,17 @@ 账号 姓名 角色 - 类型 状态 操作 - ${item.member_id} + ${item.group_member_id} ${item.account} ${item.real_name} - - - - - - - ${item.auth_method} - + ${item.role_name}