From 91df6bb0249af2bcf0da41d4229a813ff437a696 Mon Sep 17 00:00:00 2001 From: lifei6671 Date: Tue, 20 Nov 2018 20:36:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=9E=E7=8E=B0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E9=9B=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 20 +-- commands/install.go | 14 +- controllers/BookController.go | 35 +++- controllers/ItemsetsController.go | 87 ++++++++++ controllers/ManagerController.go | 77 +++++++++ models/BookModel.go | 9 +- models/BookResult.go | 8 + models/Itemsets.go | 251 +++++++++++++++++++++++++++++ routers/router.go | 10 ++ utils/cryptil/cryptil.go | 36 ++++- views/book/index.tpl | 75 ++++++++- views/book/setting.tpl | 32 +++- views/items/index.tpl | 74 +++++++++ views/items/list.tpl | 66 ++++++++ views/manager/itemsets.tpl | 257 ++++++++++++++++++++++++++++++ views/manager/widgets.tpl | 2 + views/widgets/header.tpl | 4 +- 17 files changed, 1034 insertions(+), 23 deletions(-) create mode 100644 controllers/ItemsetsController.go create mode 100644 models/Itemsets.go create mode 100644 views/items/index.tpl create mode 100644 views/items/list.tpl create mode 100644 views/manager/itemsets.tpl diff --git a/commands/command.go b/commands/command.go index dab7d06b..9fe36249 100644 --- a/commands/command.go +++ b/commands/command.go @@ -105,6 +105,7 @@ func RegisterModel() { new(models.Team), new(models.TeamMember), new(models.TeamRelationship), + new(models.Itemsets), ) gob.Register(models.Blog{}) gob.Register(models.Document{}) @@ -449,32 +450,33 @@ func RegisterAutoLoadConfig() { } } } + //注册错误处理方法. -func RegisterError() { +func RegisterError() { beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) { var buf bytes.Buffer - data :=make(map[string]interface{}) + data := make(map[string]interface{}) data["ErrorCode"] = 404 data["ErrorMessage"] = "页面未找到或已删除" - if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil { - fmt.Fprint(writer,buf.String()) + if err := beego.ExecuteViewPathTemplate(&buf, "errors/error.tpl", beego.BConfig.WebConfig.ViewsPath, data); err == nil { + fmt.Fprint(writer, buf.String()) } else { - fmt.Fprint(writer,data["ErrorMessage"]) + fmt.Fprint(writer, data["ErrorMessage"]) } }) beego.ErrorHandler("401", func(writer http.ResponseWriter, request *http.Request) { var buf bytes.Buffer - data :=make(map[string]interface{}) + data := make(map[string]interface{}) data["ErrorCode"] = 401 data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。" - if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil { - fmt.Fprint(writer,buf.String()) + if err := beego.ExecuteViewPathTemplate(&buf, "errors/error.tpl", beego.BConfig.WebConfig.ViewsPath, data); err == nil { + fmt.Fprint(writer, buf.String()) } else { - fmt.Fprint(writer,data["ErrorMessage"]) + fmt.Fprint(writer, data["ErrorMessage"]) } }) } diff --git a/commands/install.go b/commands/install.go index bd4e5c29..336c4b0f 100644 --- a/commands/install.go +++ b/commands/install.go @@ -135,8 +135,18 @@ func initialization() { book.Theme = "default" if err := book.Insert(); err != nil { - panic("Book.Insert => " + err.Error()) - os.Exit(0) + panic("初始化项目失败 -> " + err.Error()) + os.Exit(1) + } + } + + if !models.NewItemsets().Exist(1) { + item := models.NewItemsets() + item.ItemName = "默认项目" + item.MemberId = 1 + if err := item.Save(); err != nil { + panic("初始化项目集失败 -> " + err.Error()) + os.Exit(1) } } } diff --git a/controllers/BookController.go b/controllers/BookController.go index db21ddf6..83911dee 100644 --- a/controllers/BookController.go +++ b/controllers/BookController.go @@ -144,6 +144,7 @@ func (c *BookController) SaveBook() { enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on" isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on" autoSave := strings.TrimSpace(c.GetString("auto_save")) == "on" + itemId,_ := c.GetInt("itemId") if strings.Count(description, "") > 500 { c.JsonResult(6004, "项目描述不能大于500字") @@ -157,6 +158,9 @@ func (c *BookController) SaveBook() { c.JsonResult(6005, "最多允许添加10个标签") } } + if !models.NewItemsets().Exist(itemId) { + c.JsonResult(6006,"项目集不存在") + } if editor != "markdown" && editor != "html" { editor = "markdown" } @@ -170,6 +174,7 @@ func (c *BookController) SaveBook() { book.HistoryCount = historyCount book.IsDownload = 0 book.BookPassword = c.GetString("bPassword") + book.ItemId = itemId if autoRelease { book.AutoRelease = 1 @@ -432,6 +437,7 @@ func (c *BookController) Create() { description := strings.TrimSpace(c.GetString("description", "")) privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned")) commentStatus := c.GetString("comment_status") + itemId, _ := c.GetInt("itemId") if bookName == "" { c.JsonResult(6001, "项目名称不能为空") @@ -451,6 +457,9 @@ func (c *BookController) Create() { if privatelyOwned != 0 && privatelyOwned != 1 { privatelyOwned = 1 } + if !models.NewItemsets().Exist(itemId) { + c.JsonResult(6005, "项目集不存在") + } if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" { commentStatus = "closed" } @@ -503,6 +512,7 @@ func (c *BookController) Create() { book.IsUseFirstDocument = 1 book.IsDownload = 1 book.AutoRelease = 0 + book.ItemId = itemId book.Editor = "markdown" book.Theme = "default" @@ -563,6 +573,7 @@ func (c *BookController) Import() { identify := strings.TrimSpace(c.GetString("identify")) description := strings.TrimSpace(c.GetString("description", "")) privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned")) + itemId, _ := c.GetInt("itemId") if bookName == "" { c.JsonResult(6001, "项目名称不能为空") @@ -576,6 +587,9 @@ func (c *BookController) Import() { if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil { c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头") } + if !models.NewItemsets().Exist(itemId) { + c.JsonResult(6007, "项目集不存在") + } if strings.Count(identify, "") > 50 { c.JsonResult(6004, "文档标识不能超过50字") } @@ -612,6 +626,7 @@ func (c *BookController) Import() { book.MemberId = c.Member.MemberId book.CommentCount = 0 book.Version = time.Now().Unix() + book.ItemId = itemId book.Editor = "markdown" book.Theme = "default" @@ -896,7 +911,7 @@ func (c *BookController) TeamDelete() { teamId, _ := c.GetInt("teamId") if teamId <= 0 { - c.JsonResult(5001,"参数错误") + c.JsonResult(5001, "参数错误") } book, err := c.IsPermission() @@ -915,6 +930,7 @@ func (c *BookController) TeamDelete() { c.JsonResult(0, "OK") } +//团队搜索. func (c *BookController) TeamSearch() { c.Prepare() @@ -928,7 +944,22 @@ func (c *BookController) TeamSearch() { searchResult, err := models.NewTeamRelationship().FindNotJoinBookByBookIdentify(book.BookId, keyword, 10) if err != nil { - c.JsonResult(500, err.Error()) + c.JsonResult(500, err.Error(), searchResult) + } + c.JsonResult(0, "OK", searchResult) + +} + +//项目集搜索. +func (c *BookController) ItemsetsSearch() { + c.Prepare() + + keyword := strings.TrimSpace(c.GetString("q")) + + searchResult, err := models.NewItemsets().FindItemsetsByName(keyword, 10) + + if err != nil { + c.JsonResult(500, err.Error(), searchResult) } c.JsonResult(0, "OK", searchResult) diff --git a/controllers/ItemsetsController.go b/controllers/ItemsetsController.go new file mode 100644 index 00000000..38272d34 --- /dev/null +++ b/controllers/ItemsetsController.go @@ -0,0 +1,87 @@ +package controllers + +import ( + "github.com/lifei6671/mindoc/conf" + "github.com/lifei6671/mindoc/models" + "github.com/astaxie/beego/orm" + "github.com/lifei6671/mindoc/utils/pagination" + "github.com/astaxie/beego" +) + +type ItemsetsController struct { + BaseController +} +func (c *ItemsetsController) Prepare() { + c.BaseController.Prepare() + + //如果没有开启你们访问则跳转到登录 + if !c.EnableAnonymous && c.Member == nil { + c.Redirect(conf.URLFor("AccountController.Login"), 302) + return + } +} +func (c *ItemsetsController) Index() { + c.Prepare() + c.TplName = "items/index.tpl" + pageIndex, _ := c.GetInt("page", 0) + + items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, conf.PageSize) + + if err != nil && err != orm.ErrNoRows { + c.ShowErrorPage(500, err.Error()) + } + c.Data["TotalPages"] = pageIndex + if err == orm.ErrNoRows || len(items) <= 0 { + c.Data["Lists"] = items + c.Data["PageHtml"] = "" + return + } + + if totalCount > 0 { + pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) + c.Data["PageHtml"] = pager.HtmlPages() + } else { + c.Data["PageHtml"] = "" + } + + c.Data["Lists"] = items +} + +func (c *ItemsetsController) List() { + c.Prepare() + c.TplName = "items/list.tpl" + itemKey := c.Ctx.Input.Param(":key") + pageIndex, _ := c.GetInt("page", 1) + + if itemKey == "" { + c.Abort("404") + } + item, err := models.NewItemsets().FindFirst(itemKey) + + if err != nil { + if err == orm.ErrNoRows { + c.Abort("404") + } else { + beego.Error(err) + c.Abort("500") + } + } + memberId := 0 + if c.Member != nil { + memberId = c.Member.MemberId + } + searchResult, totalCount, err := models.NewItemsets().FindItemsetsByItemKey(itemKey, pageIndex, conf.PageSize, memberId) + + if err != nil && err != orm.ErrNoRows { + c.ShowErrorPage(500, "查询文档列表时出错") + } + if totalCount > 0 { + pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) + c.Data["PageHtml"] = pager.HtmlPages() + } else { + c.Data["PageHtml"] = "" + } + c.Data["Lists"] = searchResult + + c.Data["Model"] = item +} diff --git a/controllers/ManagerController.go b/controllers/ManagerController.go index 4c26ada8..a9d2a3d7 100644 --- a/controllers/ManagerController.go +++ b/controllers/ManagerController.go @@ -1073,6 +1073,8 @@ func (c *ManagerController) TeamSearchBook() { c.JsonResult(0, "OK", searchResult) } + +//删除团队项目. func (c *ManagerController) TeamBookDelete() { c.Prepare() teamRelationshipId, _ := c.GetInt("teamRelId") @@ -1088,3 +1090,78 @@ func (c *ManagerController) TeamBookDelete() { } c.JsonResult(0, "OK") } + +//项目集列表. +func (c *ManagerController) Itemsets() { + c.Prepare() + c.TplName = "manager/itemsets.tpl" + pageIndex, _ := c.GetInt("page", 0) + + items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, conf.PageSize) + + if err != nil && err != orm.ErrNoRows { + c.ShowErrorPage(500, err.Error()) + } + if err == orm.ErrNoRows || len(items) <= 0 { + c.Data["Lists"] = items + c.Data["PageHtml"] = "" + return + } + + if totalCount > 0 { + pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) + c.Data["PageHtml"] = pager.HtmlPages() + } else { + c.Data["PageHtml"] = "" + } + + c.Data["Lists"] = items + + +} + +//编辑或添加项目集. +func (c *ManagerController) ItemsetsEdit() { + c.Prepare() + itemId, _ := c.GetInt("itemId") + itemName := c.GetString("itemName") + itemKey := c.GetString("itemKey") + if itemName == "" || itemKey == "" { + c.JsonResult(5001, "参数错误") + } + var item *models.Itemsets + var err error + if itemId > 0 { + if item, err = models.NewItemsets().First(itemId); err != nil { + if err == orm.ErrNoRows { + c.JsonResult(5002, "项目集不存在") + } else { + c.JsonResult(5003, "查询项目集出错") + } + } + } else { + item = models.NewItemsets() + } + + item.ItemKey = itemKey + item.ItemName = itemName + item.MemberId = c.Member.MemberId + item.ModifyAt = c.Member.MemberId + + if err := item.Save(); err != nil { + c.JsonResult(5004, err.Error()) + } + + c.JsonResult(0, "OK") +} + +//删除项目集. +func (c *ManagerController) ItemsetsDelete() { + c.Prepare() + itemId, _ := c.GetInt("itemId") + + if err := models.NewItemsets().Delete(itemId); err != nil { + c.JsonResult(5001, err.Error()) + } + c.JsonResult(0, "OK") +} diff --git a/models/BookModel.go b/models/BookModel.go index 55948014..edbc7936 100644 --- a/models/BookModel.go +++ b/models/BookModel.go @@ -31,6 +31,8 @@ type Book struct { BookId int `orm:"pk;auto;unique;column(book_id)" json:"book_id"` // BookName 项目名称. BookName string `orm:"column(book_name);size(500)" json:"book_name"` + //所属项目集 + ItemId int `orm:"column(item_id);type(int);default(1)" json:"item_id"` // Identify 项目唯一标识. Identify string `orm:"column(identify);size(100);unique" json:"identify"` //是否是自动发布 0 否/1 是 @@ -112,6 +114,9 @@ func (book *Book) Insert() error { o := orm.NewOrm() // o.Begin() book.BookName = utils.StripTags(book.BookName) + if book.ItemId <= 0 { + book.ItemId = 1 + } _, err := o.Insert(book) if err == nil { @@ -125,7 +130,7 @@ func (book *Book) Insert() error { relationship.MemberId = book.MemberId err = relationship.Insert() if err != nil { - logs.Error("插入项目与用户关联 => ", err) + logs.Error("插入项目与用户关联 -> ", err) //o.Rollback() return err } @@ -435,7 +440,7 @@ func (book *Book) ThoroughDeleteBook(id int) error { o.Rollback() return err } - _,err = o.Raw(fmt.Sprintf("DELETE FROM %s WHERE book_id=?",NewTeamRelationship().TableNameWithPrefix()), book.BookId).Exec() + _, err = o.Raw(fmt.Sprintf("DELETE FROM %s WHERE book_id=?", NewTeamRelationship().TableNameWithPrefix()), book.BookId).Exec() if err != nil { o.Rollback() return err diff --git a/models/BookResult.go b/models/BookResult.go index 49976596..f782b526 100644 --- a/models/BookResult.go +++ b/models/BookResult.go @@ -33,6 +33,8 @@ var ( type BookResult struct { BookId int `json:"book_id"` BookName string `json:"book_name"` + ItemId int `json:"item_id"` + ItemName string `json:"item_name"` Identify string `json:"identify"` OrderIndex int `json:"order_index"` Description string `json:"description"` @@ -203,6 +205,7 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { m.HistoryCount = book.HistoryCount m.IsDownload = book.IsDownload == 0 m.AutoSave = book.AutoSave == 1 + m.ItemId = book.ItemId if book.Theme == "" { m.Theme = "default" @@ -224,6 +227,11 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { m.LastModifyText = member2.Account + " 于 " + doc.ModifyTime.Local().Format("2006-01-02 15:04:05") } + if m.ItemId > 0 { + if item,err := NewItemsets().First(m.ItemId); err == nil { + m.ItemName = item.ItemName + } + } return m } diff --git a/models/Itemsets.go b/models/Itemsets.go new file mode 100644 index 00000000..c8abd1ac --- /dev/null +++ b/models/Itemsets.go @@ -0,0 +1,251 @@ +package models + +import ( + "time" + "github.com/lifei6671/mindoc/conf" + "github.com/astaxie/beego/orm" + "github.com/astaxie/beego" + "errors" + "github.com/lifei6671/mindoc/utils/cryptil" +) + +//项目集 +type Itemsets struct { + ItemId int `orm:"column(item_id);pk;auto;unique" json:"item_id"` + ItemName string `orm:"column(item_name);size(500)" json:"item_name"` + ItemKey string `orm:"column(item_key);size(200);unique" json:"item_key"` + Description string `orm:"column(description);type(text);null" json:"description"` + MemberId int `orm:"column(member_id);size(100)" json:"member_id"` + CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"` + ModifyTime time.Time `orm:"column(modify_time);type(datetime);null;auto_now" json:"modify_time"` + ModifyAt int `orm:"column(modify_at);type(int)" json:"modify_at"` + + BookNumber int `orm:"-" json:"book_number"` + CreateTimeString string `orm:"-" json:"create_time_string"` +} + +// TableName 获取对应数据库表名. +func (item *Itemsets) TableName() string { + return "itemsets" +} + +// TableEngine 获取数据使用的引擎. +func (item *Itemsets) TableEngine() string { + return "INNODB" +} +func (item *Itemsets) TableNameWithPrefix() string { + return conf.GetDatabasePrefix() + item.TableName() +} + +func (item *Itemsets) QueryTable() orm.QuerySeter { + return orm.NewOrm().QueryTable(item.TableNameWithPrefix()) +} + +func NewItemsets() *Itemsets { + return &Itemsets{} +} + +func (item *Itemsets) First(itemId int) (*Itemsets, error) { + if itemId <= 0 { + return nil, ErrInvalidParameter + } + err := item.QueryTable().Filter("item_id", itemId).One(item) + if err != nil { + beego.Error("查询项目集失败 -> item_id=", itemId, err) + } else { + item.Include() + } + return item, err +} + +func (item *Itemsets) FindFirst(itemKey string) (*Itemsets,error) { + err := item.QueryTable().Filter("item_key",itemKey).One(item) + if err != nil { + beego.Error("查询项目集失败 -> itemKey=", itemKey, err) + } else { + item.Include() + } + return item,err +} + +func (item *Itemsets) Exist(itemId int) bool { + return item.QueryTable().Filter("item_id", itemId).Exist() +} + +//保存 +func (item *Itemsets) Save() (err error) { + + if item.ItemName == "" { + return errors.New("项目集名称不能为空") + } + if item.ItemKey == "" { + item.ItemKey = cryptil.NewRandChars(16) + } + + if item.QueryTable().Filter("item_id__ne", item.ItemId).Filter("item_key", item.ItemKey).Exist() { + return errors.New("项目集标识已存在") + } + if item.ItemId > 0 { + _, err = orm.NewOrm().Update(item) + } else { + _, err = orm.NewOrm().Insert(item) + } + return +} + +//删除. +func (item *Itemsets) Delete(itemId int) (err error) { + if itemId <= 0 { + return ErrInvalidParameter + } + if itemId == 1 { + return errors.New("默认项目集不能删除") + } + if !item.Exist(itemId) { + return errors.New("项目集不存在") + } + o := orm.NewOrm() + if err := o.Begin(); err != nil { + beego.Error("开启事物失败 ->", err) + return err + } + _, err = o.QueryTable(item.TableNameWithPrefix()).Filter("item_id", itemId).Delete() + if err != nil { + beego.Error("删除项目集失败 -> item_id=", itemId, err) + o.Rollback() + } + _, err = o.Raw("update md_books set item_id=1 where item_id=?;", itemId).Exec() + if err != nil { + beego.Error("删除项目集失败 -> item_id=", itemId, err) + o.Rollback() + } + + return o.Commit() +} + +func (item *Itemsets) Include() (*Itemsets, error) { + + item.CreateTimeString = item.CreateTime.Format("2006-01-02 15:04:05") + + i, err := NewBook().QueryTable().Filter("item_id", item.ItemId).Count() + if err != nil && err != orm.ErrNoRows { + return item, err + } + item.BookNumber = int(i) + + return item, nil +} + +//分页查询. +func (item *Itemsets) FindToPager(pageIndex, pageSize int) (list []*Itemsets, totalCount int, err error) { + + offset := (pageIndex - 1) * pageSize + + _, err = item.QueryTable().OrderBy("-item_id").Offset(offset).Limit(pageSize).All(&list) + + if err != nil { + return + } + + c, err := item.QueryTable().Count() + if err != nil { + return + } + totalCount = int(c) + + for _, item := range list { + item.Include() + } + return +} + +//根据项目集名称查询. +func (item *Itemsets) FindItemsetsByName(name string, limit int) (*SelectMemberResult, error) { + result := SelectMemberResult{} + + var itemsets []*Itemsets + var err error + if name == "" { + _, err = item.QueryTable().Limit(limit).All(&itemsets) + + } else { + _, err = item.QueryTable().Filter("item_name__icontains", name).Limit(limit).All(&itemsets) + } + if err != nil { + beego.Error("查询项目集失败 ->", err) + return &result, err + } + + items := make([]KeyValueItem, 0) + + for _, m := range itemsets { + item := KeyValueItem{} + item.Id = m.ItemId + item.Text = m.ItemName + items = append(items, item) + } + result.Result = items + + return &result, err +} + +//根据项目集标识查询项目集的项目列表. +func (item *Itemsets) FindItemsetsByItemKey(key string, pageIndex, pageSize, memberId int) (books []*BookResult, totalCount int, err error){ + o := orm.NewOrm() + + err = item.QueryTable().Filter("item_key",key).One(item) + + if err != nil { + return nil,0,err + } + offset := (pageIndex - 1) * pageSize + //如果是登录用户 + if memberId > 0 { + sql1 := `SELECT COUNT(*) +FROM md_books AS book + LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? + left join (select * + from (select book_id,team_member_id,role_id + from md_team_relationship as mtr + left join md_team_member as mtm on mtm.team_id=mtr.team_id and mtm.member_id=? order by role_id desc )as t group by t.book_id) as team on team.book_id = book.book_id +WHERE book.item_id = ? AND (relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > 0)` + + err = o.Raw(sql1, memberId, memberId, item.ItemId).QueryRow(&totalCount) + if err != nil { + return + } + sql2 := `SELECT book.*,rel1.*,member.account AS create_name FROM md_books AS book + LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? + left join (select * from (select book_id,team_member_id,role_id + from md_team_relationship as mtr + left join md_team_member as mtm on mtm.team_id=mtr.team_id and mtm.member_id=? order by role_id desc )as t group by t.book_id) as team + on team.book_id = book.book_id + LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0 + LEFT JOIN md_members AS member ON rel1.member_id = member.member_id + WHERE book.item_id = ? AND (rel.relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > 0) + ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` + + _, err = o.Raw(sql2, memberId, memberId, item.ItemId, offset, pageSize).QueryRows(&books) + + return + + } else { + count, err1 := o.QueryTable(NewBook().TableNameWithPrefix()).Filter("privately_owned", 0).Filter("item_id", item.ItemId).Count() + + if err1 != nil { + err = err1 + return + } + totalCount = int(count) + + sql := `SELECT book.*,rel.*,member.account AS create_name FROM md_books AS book + LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0 + LEFT JOIN md_members AS member ON rel.member_id = member.member_id + WHERE book.item_id = ? AND book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` + + _, err = o.Raw(sql, item.ItemId, offset, pageSize).QueryRows(&books) + + return + + } +} \ No newline at end of file diff --git a/routers/router.go b/routers/router.go index f9b01344..8af91bbc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -57,6 +57,10 @@ func init() { beego.Router("/manager/team/book/delete", &controllers.ManagerController{}, "POST:TeamBookDelete") beego.Router("/manager/team/book/search", &controllers.ManagerController{}, "*:TeamSearchBook") + beego.Router("/manager/itemsets", &controllers.ManagerController{},"*:Itemsets") + beego.Router("/manager/itemsets/edit", &controllers.ManagerController{},"post:ItemsetsEdit") + beego.Router("/manager/itemsets/delete", &controllers.ManagerController{},"post:ItemsetsDelete") + beego.Router("/setting", &controllers.SettingController{}, "*:Index") beego.Router("/setting/password", &controllers.SettingController{}, "*:Password") @@ -72,6 +76,8 @@ func init() { beego.Router("/book/create", &controllers.BookController{}, "*:Create") + beego.Router("/book/itemsets/search", &controllers.BookController{}, "*:ItemsetsSearch") + beego.Router("/book/users/create", &controllers.BookMemberController{}, "post:AddMember") beego.Router("/book/users/change", &controllers.BookMemberController{}, "post:ChangeRole") beego.Router("/book/users/delete", &controllers.BookMemberController{}, "post:RemoveMember") @@ -139,4 +145,8 @@ func init() { beego.Router("/tag/:key", &controllers.LabelController{}, "get:Index") beego.Router("/tags", &controllers.LabelController{}, "get:List") + + beego.Router("/items", &controllers.ItemsetsController{},"get:Index") + beego.Router("/items/:key", &controllers.ItemsetsController{},"get:List") + } diff --git a/utils/cryptil/cryptil.go b/utils/cryptil/cryptil.go index 6df493b6..3c4df8f4 100644 --- a/utils/cryptil/cryptil.go +++ b/utils/cryptil/cryptil.go @@ -13,6 +13,8 @@ import ( "crypto/rand" ) +var stdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") + //对称加密与解密之加密【从Beego中提取出来的】 //@param value 需要加密的字符串 //@param secret 加密密钥 @@ -71,7 +73,6 @@ func Sha1Crypt(str string, salt ...interface{}) (CryptStr string) { return fmt.Sprintf("%x", sha1.Sum([]byte(str))) } - //生成Guid字串 func UniqueId() string { b := make([]byte, 48) @@ -80,4 +81,35 @@ func UniqueId() string { return "" } return Md5Crypt(base64.URLEncoding.EncodeToString(b)) -} \ No newline at end of file +} + +//生成指定长度的字符串. +func NewRandChars(length int) string { + if length == 0 { + return "" + } + clen := len(stdChars) + if clen < 2 || clen > 256 { + panic("Wrong charset length for NewLenChars()") + } + maxrb := 255 - (256 % clen) + b := make([]byte, length) + r := make([]byte, length+(length/4)) // storage for random bytes. + i := 0 + for { + if _, err := rand.Read(r); err != nil { + panic("Error reading random bytes: " + err.Error()) + } + for _, rb := range r { + c := int(rb) + if c > maxrb { + continue // Skip this number to avoid modulo bias. + } + b[i] = stdChars[c%clen] + i++ + if i == length { + return string(b) + } + } + } +} diff --git a/views/book/index.tpl b/views/book/index.tpl index 868aafae..453cfb2f 100644 --- a/views/book/index.tpl +++ b/views/book/index.tpl @@ -12,6 +12,7 @@ + @@ -137,8 +138,19 @@