diff --git a/controllers/book.go b/controllers/book.go index 70041b88..9598dc9a 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -385,6 +385,7 @@ func (c *BookController) Users() { } } + // Create 创建项目. func (c *BookController) Create() { diff --git a/controllers/book_member.go b/controllers/book_member.go index 70e9fcab..dbc9092b 100644 --- a/controllers/book_member.go +++ b/controllers/book_member.go @@ -7,6 +7,7 @@ import ( "github.com/astaxie/beego/orm" "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" + "github.com/astaxie/beego" ) type BookMemberController struct { @@ -16,10 +17,10 @@ type BookMemberController struct { // AddMember 参加参与用户. func (c *BookMemberController) AddMember() { identify := c.GetString("identify") - account := c.GetString("account") + account,_ := c.GetInt("account") role_id, _ := c.GetInt("role_id", 3) - - if identify == "" || account == "" { + beego.Info(account) + if identify == "" || account <= 0 { c.JsonResult(6001, "参数错误") } book, err := c.IsPermission() @@ -28,9 +29,10 @@ func (c *BookMemberController) AddMember() { c.JsonResult(6001, err.Error()) } + member := models.NewMember() - if _, err := member.FindByAccount(account); err != nil { + if _, err := member.Find(account); err != nil { c.JsonResult(404, "用户不存在") } if member.Status == 1 { diff --git a/controllers/search.go b/controllers/search.go index d5483535..bcdc9e22 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -13,7 +13,7 @@ import ( type SearchController struct { BaseController } - +//搜索首页 func (c *SearchController) Index() { c.Prepare() c.TplName = "search/index.tpl" @@ -95,3 +95,41 @@ func (c *SearchController) Index() { c.Data["Lists"] = search_result } } + +//搜索用户 +func (c *SearchController) User() { + c.Prepare() + key := c.Ctx.Input.Param(":key") + keyword := strings.TrimSpace(c.GetString("q")) + if key == "" || keyword == ""{ + c.JsonResult(404,"参数错误") + } + + book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId) + if err != nil { + if err == models.ErrPermissionDenied { + c.JsonResult(403,"没有权限") + } + c.JsonResult(500,"项目不存在") + } + + members,err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId,10,"%"+keyword+"%") + if err != nil { + beego.Error("查询用户列表出错:" + err.Error()) + c.JsonResult(500,err.Error()) + } + 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) +} + diff --git a/models/member.go b/models/member.go index c5749e3c..7c7c8475 100644 --- a/models/member.go +++ b/models/member.go @@ -230,6 +230,20 @@ func (m *Member) FindByAccount(account string) (*Member, error) { } return m, err } +//批量查询用户 +func (m *Member) FindByAccountList(accounts ...string) ([]*Member,error) { + o := orm.NewOrm() + + var members []*Member + _,err := o.QueryTable(m.TableNameWithPrefix()).Filter("account__in", accounts).All(&members) + + if err == nil { + for _,item := range members { + item.ResolveRoleName() + } + } + return members, err +} //分页查找用户. func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int, error) { diff --git a/models/member_result.go b/models/member_result.go index 025e162e..f6c6e159 100644 --- a/models/member_result.go +++ b/models/member_result.go @@ -24,6 +24,14 @@ type MemberRelationshipResult struct { RoleName string `json:"role_name"` } +type SelectMemberResult struct { + Result []KeyValueItem `json:"results"` +} +type KeyValueItem struct { + Id int `json:"id"` + Text string `json:"text"` +} + func NewMemberRelationshipResult() *MemberRelationshipResult { return &MemberRelationshipResult{} } @@ -53,8 +61,8 @@ func (m *MemberRelationshipResult) ResolveRoleName() *MemberRelationshipResult { } return m } - -func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, pageSize int) ([]*MemberRelationshipResult, int, error) { +// 根据项目ID查询用户 +func (m *MemberRelationshipResult) FindForUsersByBookId(bookId, pageIndex, pageSize int) ([]*MemberRelationshipResult, int, error) { o := orm.NewOrm() var members []*MemberRelationshipResult @@ -65,7 +73,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page var total_count int - err := o.Raw(sql2, book_id).QueryRow(&total_count) + err := o.Raw(sql2, bookId).QueryRow(&total_count) if err != nil { return members, 0, err @@ -73,7 +81,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page offset := (pageIndex - 1) * pageSize - _, err = o.Raw(sql1, book_id, offset, pageSize).QueryRows(&members) + _, err = o.Raw(sql1, bookId, offset, pageSize).QueryRows(&members) if err != nil { return members, 0, err @@ -84,3 +92,46 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page } return members, total_count, nil } + +// 查询指定文档中不存在的用户列表 +func (m *MemberRelationshipResult) FindNotJoinUsersByAccount(bookId, limit int,account string) ([]*Member,error){ + o := orm.NewOrm() + + sql := "SELECT m.* FROM md_members as m LEFT JOIN md_relationship as rel ON m.member_id=rel.member_id WHERE (rel.book_id <> ? OR rel.relationship_id IS NULL) AND m.account LIKE ? LIMIT 0,?;" + + var members []*Member + + _,err := o.Raw(sql,bookId,account,limit).QueryRows(&members) + + return members,err +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/routers/router.go b/routers/router.go index 73c93a2a..0e82b3cc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -65,6 +65,7 @@ func init() { beego.Router("/api/:key/delete", &controllers.DocumentController{}, "post:Delete") beego.Router("/api/:key/content/?:id", &controllers.DocumentController{}, "*:Content") beego.Router("/api/:key/compare/:id", &controllers.DocumentController{}, "*:Compare") + beego.Router("/api/search/user/:key", &controllers.SearchController{}, "*:User") beego.Router("/history/get", &controllers.DocumentController{}, "get:History") beego.Router("/history/delete", &controllers.DocumentController{}, "*:DeleteHistory") diff --git a/views/book/users.tpl b/views/book/users.tpl index 27de2976..5633dd12 100644 --- a/views/book/users.tpl +++ b/views/book/users.tpl @@ -10,8 +10,8 @@ - - + +