实现添加项目成员时下拉提示

pull/219/head
Minho 2018-02-03 15:25:10 +08:00
parent c7d83576bd
commit ba9c390a13
7 changed files with 156 additions and 13 deletions

View File

@ -385,6 +385,7 @@ func (c *BookController) Users() {
} }
} }
// Create 创建项目. // Create 创建项目.
func (c *BookController) Create() { func (c *BookController) Create() {

View File

@ -7,6 +7,7 @@ import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models" "github.com/lifei6671/mindoc/models"
"github.com/astaxie/beego"
) )
type BookMemberController struct { type BookMemberController struct {
@ -16,10 +17,10 @@ type BookMemberController struct {
// AddMember 参加参与用户. // AddMember 参加参与用户.
func (c *BookMemberController) AddMember() { func (c *BookMemberController) AddMember() {
identify := c.GetString("identify") identify := c.GetString("identify")
account := c.GetString("account") account,_ := c.GetInt("account")
role_id, _ := c.GetInt("role_id", 3) role_id, _ := c.GetInt("role_id", 3)
beego.Info(account)
if identify == "" || account == "" { if identify == "" || account <= 0 {
c.JsonResult(6001, "参数错误") c.JsonResult(6001, "参数错误")
} }
book, err := c.IsPermission() book, err := c.IsPermission()
@ -28,9 +29,10 @@ func (c *BookMemberController) AddMember() {
c.JsonResult(6001, err.Error()) c.JsonResult(6001, err.Error())
} }
member := models.NewMember() member := models.NewMember()
if _, err := member.FindByAccount(account); err != nil { if _, err := member.Find(account); err != nil {
c.JsonResult(404, "用户不存在") c.JsonResult(404, "用户不存在")
} }
if member.Status == 1 { if member.Status == 1 {

View File

@ -13,7 +13,7 @@ import (
type SearchController struct { type SearchController struct {
BaseController BaseController
} }
//搜索首页
func (c *SearchController) Index() { func (c *SearchController) Index() {
c.Prepare() c.Prepare()
c.TplName = "search/index.tpl" c.TplName = "search/index.tpl"
@ -95,3 +95,41 @@ func (c *SearchController) Index() {
c.Data["Lists"] = search_result 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)
}

View File

@ -230,6 +230,20 @@ func (m *Member) FindByAccount(account string) (*Member, error) {
} }
return m, err 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) { func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int, error) {

View File

@ -24,6 +24,14 @@ type MemberRelationshipResult struct {
RoleName string `json:"role_name"` 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 { func NewMemberRelationshipResult() *MemberRelationshipResult {
return &MemberRelationshipResult{} return &MemberRelationshipResult{}
} }
@ -53,8 +61,8 @@ func (m *MemberRelationshipResult) ResolveRoleName() *MemberRelationshipResult {
} }
return m return m
} }
// 根据项目ID查询用户
func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, pageSize int) ([]*MemberRelationshipResult, int, error) { func (m *MemberRelationshipResult) FindForUsersByBookId(bookId, pageIndex, pageSize int) ([]*MemberRelationshipResult, int, error) {
o := orm.NewOrm() o := orm.NewOrm()
var members []*MemberRelationshipResult var members []*MemberRelationshipResult
@ -65,7 +73,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page
var total_count int var total_count int
err := o.Raw(sql2, book_id).QueryRow(&total_count) err := o.Raw(sql2, bookId).QueryRow(&total_count)
if err != nil { if err != nil {
return members, 0, err return members, 0, err
@ -73,7 +81,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page
offset := (pageIndex - 1) * pageSize 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 { if err != nil {
return members, 0, err return members, 0, err
@ -84,3 +92,46 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(book_id, pageIndex, page
} }
return members, total_count, nil 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
}

View File

@ -65,6 +65,7 @@ func init() {
beego.Router("/api/:key/delete", &controllers.DocumentController{}, "post:Delete") beego.Router("/api/:key/delete", &controllers.DocumentController{}, "post:Delete")
beego.Router("/api/:key/content/?:id", &controllers.DocumentController{}, "*:Content") beego.Router("/api/:key/content/?:id", &controllers.DocumentController{}, "*:Content")
beego.Router("/api/:key/compare/:id", &controllers.DocumentController{}, "*:Compare") 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/get", &controllers.DocumentController{}, "get:History")
beego.Router("/history/delete", &controllers.DocumentController{}, "*:DeleteHistory") beego.Router("/history/delete", &controllers.DocumentController{}, "*:DeleteHistory")

View File

@ -10,8 +10,8 @@
<!-- Bootstrap --> <!-- Bootstrap -->
<link href="{{cdncss "/static/bootstrap/css/bootstrap.min.css"}}" rel="stylesheet"> <link href="{{cdncss "/static/bootstrap/css/bootstrap.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/font-awesome/css/font-awesome.min.css"}}" rel="stylesheet"> <link href="{{cdncss "/static/font-awesome/css/font-awesome.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/select2/4.0.5/css/select2.min.css"}}" rel="stylesheet">
<link href="/static/css/main.css" rel="stylesheet"> <link href="{{cdncss "/static/css/main.css"}}" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
@ -106,7 +106,8 @@
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">账号</label> <label class="col-sm-2 control-label">账号</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" name="account" class="form-control" placeholder="用户账号" id="account" maxlength="50"> {{/*<input type="text" name="account" class="form-control" placeholder="用户账号" id="account" maxlength="50">*/}}
<select class="js-data-example-ajax form-control" multiple="multiple" name="account" id="account"></select>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -135,9 +136,17 @@
<script src="{{cdnjs "/static/bootstrap/js/bootstrap.min.js"}}"></script> <script src="{{cdnjs "/static/bootstrap/js/bootstrap.min.js"}}"></script>
<script src="{{cdnjs "/static/vuejs/vue.min.js"}}"></script> <script src="{{cdnjs "/static/vuejs/vue.min.js"}}"></script>
<script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script> <script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
<script src="/static/js/main.js" type="text/javascript"></script> <script src="/static/select2/4.0.5/js/select2.full.min.js"></script>
<script src="/static/select2/4.0.5/js/i18n/zh-CN.js"></script>
<script src="{{cdnjs "/static/js/main.js"}}" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
var modalCache = $("#addBookMemberDialogModal form").html();
/**
* 添加用户
*/
$("#addBookMemberDialogForm").ajaxForm({ $("#addBookMemberDialogForm").ajaxForm({
beforeSubmit : function () { beforeSubmit : function () {
var account = $.trim($("#account").val()); var account = $.trim($("#account").val());
@ -156,6 +165,33 @@
$("#btnAddMember").button("reset"); $("#btnAddMember").button("reset");
} }
}); });
$("#addBookMemberDialogModal").on("hidden.bs.modal",function () {
$(this).find("form").html(modalCache);
}).on("show.bs.modal",function () {
$('.js-data-example-ajax').select2({
language: "zh-CN",
minimumInputLength : 1,
minimumResultsForSearch: Infinity,
maximumSelectionLength:1,
width : "100%",
ajax: {
url: '{{urlfor "SearchController.User" ":key" .Model.Identify}}',
dataType: 'json',
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
console.log(data)
return {
results : data.data.results
}
}
}
});
});
var app = new Vue({ var app = new Vue({
el : "#userList", el : "#userList",