diff --git a/commands/command.go b/commands/command.go index 1691371f..a3539331 100644 --- a/commands/command.go +++ b/commands/command.go @@ -111,7 +111,8 @@ func RegisterModel() { new(models.TeamMember), new(models.TeamRelationship), new(models.Itemsets), - new(models.WorkWeixinAccount), + new(models.Comment), + new(models.WorkWeixinAccount), ) gob.Register(models.Blog{}) gob.Register(models.Document{}) diff --git a/controllers/BookController.go b/controllers/BookController.go index 2e26708d..23ddd666 100644 --- a/controllers/BookController.go +++ b/controllers/BookController.go @@ -518,7 +518,6 @@ func (c *BookController) Create() { book.Identify = identify book.DocCount = 0 book.MemberId = c.Member.MemberId - book.CommentCount = 0 book.Version = time.Now().Unix() book.IsEnableShare = 0 book.IsUseFirstDocument = 1 @@ -636,7 +635,6 @@ func (c *BookController) Import() { book.Identify = identify book.DocCount = 0 book.MemberId = c.Member.MemberId - book.CommentCount = 0 book.Version = time.Now().Unix() book.ItemId = itemId diff --git a/controllers/CommentController.go b/controllers/CommentController.go new file mode 100644 index 00000000..487c855b --- /dev/null +++ b/controllers/CommentController.go @@ -0,0 +1,97 @@ +package controllers + +import ( + "strings" + "time" + + "github.com/mindoc-org/mindoc/conf" + "github.com/mindoc-org/mindoc/models" + "github.com/mindoc-org/mindoc/utils/pagination" +) + +type CommentController struct { + BaseController +} + +func (c *CommentController) Lists() { + docid, _ := c.GetInt("docid", 0) + pageIndex, _ := c.GetInt("page", 1) + + // 获取评论、分页 + comments, count, pageIndex := models.NewComment().QueryCommentByDocumentId(docid, pageIndex, conf.PageSize, c.Member) + page := pagination.PageUtil(int(count), pageIndex, conf.PageSize, comments) + + var data struct { + DocId int `json:"doc_id"` + Page pagination.Page `json:"page"` + } + data.DocId = docid + data.Page = page + + c.JsonResult(0, "ok", data) + return +} + +func (c *CommentController) Create() { + content := c.GetString("content") + id, _ := c.GetInt("doc_id") + + _, err := models.NewDocument().Find(id) + if err != nil { + c.JsonResult(1, "文章不存在") + } + + m := models.NewComment() + m.DocumentId = id + if len(c.Member.RealName) != 0 { + m.Author = c.Member.RealName + } else { + m.Author = c.Member.Account + } + m.MemberId = c.Member.MemberId + m.IPAddress = c.Ctx.Request.RemoteAddr + m.IPAddress = strings.Split(m.IPAddress, ":")[0] + m.CommentDate = time.Now() + m.Content = content + m.Insert() + + var data struct { + DocId int `json:"doc_id"` + } + data.DocId = id + + c.JsonResult(0, "ok", data) +} + +func (c *CommentController) Index() { + c.Prepare() + c.TplName = "comment/index.tpl" +} + +func (c *CommentController) Delete() { + if c.Ctx.Input.IsPost() { + id, _ := c.GetInt("id", 0) + m, err := models.NewComment().Find(id) + if err != nil { + c.JsonResult(1, "评论不存在") + } + + doc, err := models.NewDocument().Find(m.DocumentId) + if err != nil { + c.JsonResult(1, "文章不存在") + } + + // 判断是否有权限删除 + bookRole, _ := models.NewRelationship().FindForRoleId(doc.BookId, c.Member.MemberId) + if m.CanDelete(c.Member.MemberId, bookRole) { + err := m.Delete() + if err != nil { + c.JsonResult(1, "删除错误") + } else { + c.JsonResult(0, "ok") + } + } else { + c.JsonResult(1, "没有权限删除") + } + } +} diff --git a/controllers/comment.go b/controllers/comment.go deleted file mode 100644 index ff70e555..00000000 --- a/controllers/comment.go +++ /dev/null @@ -1,19 +0,0 @@ -package controllers - -type CommentController struct { - BaseController -} - -func (c *CommentController) Lists() { - -} - -func (c *CommentController) Create() { - - c.JsonResult(0, "ok") -} - -func (c *CommentController) Index() { - c.Prepare() - c.TplName = "comment/index.tpl" -} diff --git a/models/BookModel.go b/models/BookModel.go index c91c1af7..a64c6733 100644 --- a/models/BookModel.go +++ b/models/BookModel.go @@ -541,7 +541,7 @@ WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0` if err != nil { return } - sql2 := `SELECT book.*,rel1.*,member.account AS create_name,member.real_name FROM md_books AS book + sql2 := `SELECT book.*,rel1.*,mdmb.account AS create_name,mdmb.real_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 book_id,min(role_id) AS role_id from (select book_id,role_id @@ -549,7 +549,7 @@ WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0` 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 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 + LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0 ORDER BY order_index desc,book.book_id DESC LIMIT ?,?` _, err = o.Raw(sql2, memberId, memberId, offset, pageSize).QueryRows(&books) @@ -563,9 +563,9 @@ WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0 ORDER BY o } totalCount = int(count) - sql := `SELECT book.*,rel.*,member.account AS create_name,member.real_name FROM md_books AS book + sql := `SELECT book.*,rel.*,mdmb.account AS create_name,mdmb.real_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 + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id WHERE book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` _, err = o.Raw(sql, offset, pageSize).QueryRows(&books) @@ -595,14 +595,14 @@ WHERE (relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > if err != nil { return } - sql2 := `SELECT book.*,rel1.*,member.account AS create_name FROM md_books AS book + sql2 := `SELECT book.*,rel1.*,mdmb.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.role_id,t.team_member_id,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 + LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id WHERE (rel.relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > 0) AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` @@ -619,9 +619,9 @@ WHERE (relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > } totalCount = int(count) - sql := `SELECT book.*,rel.*,member.account AS create_name FROM md_books AS book + sql := `SELECT book.*,rel.*,mdmb.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 + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id WHERE book.privately_owned = 0 AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` _, err = o.Raw(sql, keyword, offset, pageSize).QueryRows(&books) diff --git a/models/BookResult.go b/models/BookResult.go index 1692f6fb..9647f6e3 100644 --- a/models/BookResult.go +++ b/models/BookResult.go @@ -240,6 +240,17 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { m.ItemName = item.ItemName } } + if m.CommentStatus == "closed" { + m.IsDisplayComment = false + } else if m.CommentStatus == "open" { + m.IsDisplayComment = true + } else if m.CommentStatus == "registered_only" { + // todo + } else if m.CommentStatus == "group_only" { + // todo + } else { + m.IsDisplayComment = false; + } return m } diff --git a/models/comment.go b/models/CommentModel.go similarity index 68% rename from models/comment.go rename to models/CommentModel.go index 0786a464..37b65f0a 100644 --- a/models/comment.go +++ b/models/CommentModel.go @@ -33,6 +33,8 @@ type Comment struct { ParentId int `orm:"column(parent_id);type(int);default(0)" json:"parent_id"` AgreeCount int `orm:"column(agree_count);type(int);default(0)" json:"agree_count"` AgainstCount int `orm:"column(against_count);type(int);default(0)" json:"against_count"` + Index int `orm:"-" json:"index"` + ShowDel int `orm:"-" json:"show_del"` } // TableName 获取对应数据库表名. @@ -52,14 +54,41 @@ func (m *Comment) TableNameWithPrefix() string { func NewComment() *Comment { return &Comment{} } -func (m *Comment) Find(id int) (*Comment, error) { - if id <= 0 { - return m, ErrInvalidParameter - } - o := orm.NewOrm() - err := o.Read(m) - return m, err +// 是否有权限删除 +func (m *Comment) CanDelete(user_memberid int, user_bookrole conf.BookRole) bool { + return user_memberid == m.MemberId || user_bookrole == conf.BookFounder || user_bookrole == conf.BookAdmin +} + +// 根据文档id查询文档评论 +func (m *Comment) QueryCommentByDocumentId(doc_id, page, pagesize int, member *Member) (comments []Comment, count int64, ret_page int) { + doc, err := NewDocument().Find(doc_id) + if err != nil { + return + } + + o := orm.NewOrm() + count, _ = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Count() + if -1 == page { // 请求最后一页 + var total int = int(count) + if total % pagesize == 0 { + page = total / pagesize + } else { + page = total / pagesize + 1 + } + } + offset := (page - 1) * pagesize + ret_page = page + o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).OrderBy("comment_date").Offset(offset).Limit(pagesize).All(&comments) + + bookRole, _ := NewRelationship().FindForRoleId(doc.BookId, member.MemberId) + for i := 0; i < len(comments); i++ { + comments[i].Index = (i + 1) + (page - 1) * pagesize + if comments[i].CanDelete(member.MemberId, bookRole) { + comments[i].ShowDel = 1 + } + } + return } func (m *Comment) Update(cols ...string) error { @@ -135,3 +164,18 @@ func (m *Comment) Insert() error { return err } + +// 删除一条评论 +func (m *Comment) Delete() error { + o := orm.NewOrm() + _, err := o.Delete(m) + return err +} + +func (m *Comment) Find(id int, cols ...string) (*Comment, error) { + o := orm.NewOrm() + if err := o.QueryTable(m.TableNameWithPrefix()).Filter("comment_id", id).One(m, cols...); err != nil { + return m, err + } + return m, nil +} \ No newline at end of file diff --git a/models/DocumentSearchResult.go b/models/DocumentSearchResult.go index 78048bf7..afb751de 100644 --- a/models/DocumentSearchResult.go +++ b/models/DocumentSearchResult.go @@ -53,12 +53,12 @@ FROM ( book.identify AS book_identify, book.book_name, rel.member_id, - member.account AS author, + mdmb.account AS author, 'document' AS search_type FROM md_documents AS doc LEFT JOIN md_books AS book ON doc.book_id = book.book_id LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0 - LEFT JOIN md_members AS member ON rel.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE ?) UNION ALL SELECT @@ -71,11 +71,11 @@ SELECT book.identify AS book_identify, book.book_name, rel.member_id, - member.account AS author, + mdmb.account AS author, 'book' AS search_type FROM md_books AS book LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0 - LEFT JOIN md_members AS member ON rel.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id WHERE book.privately_owned = 0 AND (book.book_name LIKE ? OR book.description LIKE ?) UNION ALL @@ -89,10 +89,10 @@ WHERE book.privately_owned = 0 AND (book.book_name LIKE ? OR book.description LI blog.blog_identify, blog.blog_title as book_name, blog.member_id, - member.account, + mdmb.account, 'blog' AS search_type FROM md_blogs AS blog - LEFT JOIN md_members AS member ON blog.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON blog.member_id = mdmb.member_id WHERE blog.blog_status = 'public' AND (blog.blog_release LIKE ? OR blog.blog_title LIKE ?) ) AS union_table ORDER BY create_time DESC @@ -156,12 +156,12 @@ FROM ( book.identify AS book_identify, book.book_name, rel.member_id, - member.account AS author, + mdmb.account AS author, 'document' AS search_type FROM md_documents AS doc LEFT JOIN md_books AS book ON doc.book_id = book.book_id LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0 - LEFT JOIN md_members AS member ON rel.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id LEFT JOIN md_relationship AS rel1 ON doc.book_id = rel1.book_id AND rel1.member_id = ? LEFT JOIN (SELECT * FROM (SELECT @@ -187,11 +187,11 @@ FROM ( book.identify AS book_identify, book.book_name, rel.member_id, - member.account AS author, + mdmb.account AS author, 'book' AS search_type FROM md_books AS book LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0 - LEFT JOIN md_members AS member ON rel.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id LEFT JOIN md_relationship AS rel1 ON book.book_id = rel1.book_id AND rel1.member_id = ? LEFT JOIN (SELECT * FROM (SELECT @@ -216,10 +216,10 @@ FROM ( blog.blog_identify AS book_identify, blog.blog_title as book_name, blog.member_id, - member.account, + mdmb.account, 'blog' AS search_type FROM md_blogs AS blog - LEFT JOIN md_members AS member ON blog.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON blog.member_id = mdmb.member_id WHERE (blog.blog_status = 'public' OR blog.member_id = ?) AND blog.blog_type = 0 AND (blog.blog_release LIKE ? OR blog.blog_title LIKE ?) ) AS union_table diff --git a/models/Itemsets.go b/models/Itemsets.go index de7ba523..07b8894f 100644 --- a/models/Itemsets.go +++ b/models/Itemsets.go @@ -236,7 +236,7 @@ WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team logs.Error("查询项目空间时出错 ->", key, err) return } - sql2 := `SELECT book.*,rel1.*,member.account AS create_name FROM md_books AS book + sql2 := `SELECT book.*,rel1.*,mdmb.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 book_id,min(role_id) as role_id from (select book_id,role_id from md_team_relationship as mtr @@ -244,7 +244,7 @@ WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team as t group by 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 + LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team.role_id >= 0) ORDER BY order_index desc,book.book_id DESC LIMIT ?,?` @@ -261,9 +261,9 @@ as t group by book_id) as team } totalCount = int(count) - sql := `SELECT book.*,rel.*,member.account AS create_name FROM md_books AS book + sql := `SELECT book.*,rel.*,mdmb.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 + LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.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) diff --git a/models/MemberResult.go b/models/MemberResult.go index 2acd1c88..3926ae0a 100644 --- a/models/MemberResult.go +++ b/models/MemberResult.go @@ -72,9 +72,9 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(lang string, bookId, pag var members []*MemberRelationshipResult - sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as member ON rel.member_id = member.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC LIMIT ?,?" + sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC LIMIT ?,?" - 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 = ?" + sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ?" var total_count int diff --git a/models/TeamMember.go b/models/TeamMember.go index 449bc7c6..4243638c 100644 --- a/models/TeamMember.go +++ b/models/TeamMember.go @@ -213,11 +213,11 @@ func (m *TeamMember) FindNotJoinMemberByAccount(teamId int, account string, limi } o := orm.NewOrm() - sql := `select member.member_id,member.account,member.real_name,team.team_member_id -from md_members as member - left join md_team_member as team on team.team_id = ? and member.member_id = team.member_id - where member.account like ? or member.real_name like ? AND team_member_id IS NULL - order by member.member_id desc + sql := `select mdmb.member_id,mdmb.account,mdmb.real_name,team.team_member_id +from md_members as mdmb + left join md_team_member as team on team.team_id = ? and mdmb.member_id = team.member_id + where mdmb.account like ? or mdmb.real_name like ? AND team_member_id IS NULL + order by mdmb.member_id desc limit ?;` members := make([]*Member, 0) diff --git a/models/comment_result.go b/models/comment_result.go index acbd1eec..d1a852e4 100644 --- a/models/comment_result.go +++ b/models/comment_result.go @@ -16,10 +16,10 @@ func (m *CommentResult) FindForDocumentToPager(doc_id, page_index, page_size int SELECT comment.* , parent.* , - member.account AS author, + mdmb.account AS author, p_member.account AS reply_account FROM md_comments AS comment - LEFT JOIN md_members AS member ON comment.member_id = member.member_id + LEFT JOIN md_members AS mdmb ON comment.member_id = mdmb.member_id LEFT JOIN md_comments AS parent ON comment.parent_id = parent.comment_id LEFT JOIN md_members AS p_member ON p_member.member_id = parent.member_id diff --git a/routers/router.go b/routers/router.go index 891cb15b..c0e9d5a4 100644 --- a/routers/router.go +++ b/routers/router.go @@ -251,6 +251,7 @@ func init() { web.Router("/attach_files/:key/:attach_id", &controllers.DocumentController{}, "get:DownloadAttachment") web.Router("/comment/create", &controllers.CommentController{}, "post:Create") + web.Router("/comment/delete", &controllers.CommentController{}, "post:Delete") web.Router("/comment/lists", &controllers.CommentController{}, "get:Lists") web.Router("/comment/index", &controllers.CommentController{}, "*:Index") diff --git a/static/js/bootstrap-paginator.min.js b/static/js/bootstrap-paginator.min.js new file mode 100644 index 00000000..d482d962 --- /dev/null +++ b/static/js/bootstrap-paginator.min.js @@ -0,0 +1 @@ +!function($){"use strict";var BootstrapPaginator=function(element,options){this.init(element,options)},old=null;BootstrapPaginator.prototype={init:function(element,options){this.$element=$(element);var version=options&&options.bootstrapMajorVersion?options.bootstrapMajorVersion:$.fn.bootstrapPaginator.defaults.bootstrapMajorVersion,id=this.$element.attr("id");if(2===version&&!this.$element.is("div"))throw"in Bootstrap version 2 the pagination must be a div element. Or if you are using Bootstrap pagination 3. Please specify it in bootstrapMajorVersion in the option";if(version>2&&!this.$element.is("ul"))throw"in Bootstrap version 3 the pagination root item must be an ul element.";this.currentPage=1,this.lastPage=1,this.setOptions(options),this.initialized=!0},setOptions:function(options){this.options=$.extend({},this.options||$.fn.bootstrapPaginator.defaults,options),this.totalPages=parseInt(this.options.totalPages,10),this.numberOfPages=parseInt(this.options.numberOfPages,10),options&&"undefined"!=typeof options.currentPage&&this.setCurrentPage(options.currentPage),this.listen(),this.render(),this.initialized||this.lastPage===this.currentPage||this.$element.trigger("page-changed",[this.lastPage,this.currentPage])},listen:function(){this.$element.off("page-clicked"),this.$element.off("page-changed"),"function"==typeof this.options.onPageClicked&&this.$element.bind("page-clicked",this.options.onPageClicked),"function"==typeof this.options.onPageChanged&&this.$element.on("page-changed",this.options.onPageChanged),this.$element.bind("page-clicked",this.onPageClicked)},destroy:function(){this.$element.off("page-clicked"),this.$element.off("page-changed"),this.$element.removeData("bootstrapPaginator"),this.$element.empty()},show:function(page){this.setCurrentPage(page),this.render(),this.lastPage!==this.currentPage&&this.$element.trigger("page-changed",[this.lastPage,this.currentPage])},showNext:function(){var pages=this.getPages();pages.next&&this.show(pages.next)},showPrevious:function(){var pages=this.getPages();pages.prev&&this.show(pages.prev)},showFirst:function(){var pages=this.getPages();pages.first&&this.show(pages.first)},showLast:function(){var pages=this.getPages();pages.last&&this.show(pages.last)},onPageItemClicked:function(event){var type=event.data.type,page=event.data.page;this.$element.trigger("page-clicked",[event,type,page])},onPageClicked:function(event,originalEvent,type,page){var currentTarget=$(event.currentTarget);switch(type){case"first":currentTarget.bootstrapPaginator("showFirst");break;case"prev":currentTarget.bootstrapPaginator("showPrevious");break;case"next":currentTarget.bootstrapPaginator("showNext");break;case"last":currentTarget.bootstrapPaginator("showLast");break;case"page":currentTarget.bootstrapPaginator("show",page)}},render:function(){var containerClass=this.getValueFromOption(this.options.containerClass,this.$element),size=this.options.size||"normal",alignment=this.options.alignment||"left",pages=this.getPages(),listContainer=2===this.options.bootstrapMajorVersion?$(""):this.$element,listContainerClass=2===this.options.bootstrapMajorVersion?this.getValueFromOption(this.options.listContainerClass,listContainer):null,first=null,prev=null,next=null,last=null,p=null,i=0;switch(this.$element.prop("class",""),this.$element.addClass("pagination"),size.toLowerCase()){case"large":case"small":case"mini":this.$element.addClass($.fn.bootstrapPaginator.sizeArray[this.options.bootstrapMajorVersion][size.toLowerCase()])}if(2===this.options.bootstrapMajorVersion)switch(alignment.toLowerCase()){case"center":this.$element.addClass("pagination-centered");break;case"right":this.$element.addClass("pagination-right")}for(this.$element.addClass(containerClass),this.$element.empty(),2===this.options.bootstrapMajorVersion&&(this.$element.append(listContainer),listContainer.addClass(listContainerClass)),this.pageRef=[],pages.first&&(first=this.buildPageItem("first",pages.first),first&&listContainer.append(first)),pages.prev&&(prev=this.buildPageItem("prev",pages.prev),prev&&listContainer.append(prev)),i=0;i"),itemContent=$(""),text="",title="",itemContainerClass=this.options.itemContainerClass(type,page,this.currentPage),itemContentClass=this.getValueFromOption(this.options.itemContentClass,type,page,this.currentPage),tooltipOpts=null;switch(type){case"first":if(!this.getValueFromOption(this.options.shouldShowPage,type,page,this.currentPage))return;text=this.options.itemTexts(type,page,this.currentPage),title=this.options.tooltipTitles(type,page,this.currentPage);break;case"last":if(!this.getValueFromOption(this.options.shouldShowPage,type,page,this.currentPage))return;text=this.options.itemTexts(type,page,this.currentPage),title=this.options.tooltipTitles(type,page,this.currentPage);break;case"prev":if(!this.getValueFromOption(this.options.shouldShowPage,type,page,this.currentPage))return;text=this.options.itemTexts(type,page,this.currentPage),title=this.options.tooltipTitles(type,page,this.currentPage);break;case"next":if(!this.getValueFromOption(this.options.shouldShowPage,type,page,this.currentPage))return;text=this.options.itemTexts(type,page,this.currentPage),title=this.options.tooltipTitles(type,page,this.currentPage);break;case"page":if(!this.getValueFromOption(this.options.shouldShowPage,type,page,this.currentPage))return;text=this.options.itemTexts(type,page,this.currentPage),title=this.options.tooltipTitles(type,page,this.currentPage)}return itemContainer.addClass(itemContainerClass).append(itemContent),itemContent.addClass(itemContentClass).html(text).on("click",null,{type:type,page:page},$.proxy(this.onPageItemClicked,this)),this.options.pageUrl&&itemContent.attr("href",this.getValueFromOption(this.options.pageUrl,type,page,this.currentPage)),this.options.useBootstrapTooltip?(tooltipOpts=$.extend({},this.options.bootstrapTooltipOptions,{title:title}),itemContent.tooltip(tooltipOpts)):itemContent.attr("title",title),itemContainer},setCurrentPage:function(page){if(page>this.totalPages||1>page)throw"Page out of range";this.lastPage=this.currentPage,this.currentPage=parseInt(page,10)},getPages:function(){var totalPages=this.totalPages,pageStart=0===this.currentPage%this.numberOfPages?(parseInt(this.currentPage/this.numberOfPages,10)-1)*this.numberOfPages+1:parseInt(this.currentPage/this.numberOfPages,10)*this.numberOfPages+1,output=[],i=0,counter=0;for(pageStart=1>pageStart?1:pageStart,i=pageStart,counter=0;counter=i;i+=1,counter+=1)output.push(i);return output.first=1,output.prev=this.currentPage>1?this.currentPage-1:1,output.next=this.currentPage"; + html += "

" + c[i].author + "" + timeFormat(c[i].comment_date) + "

"; + html += "
" + c[i].content + "
"; + html += "

"; + if (c[i].show_del == 1) html += ""; + else html += ""; + html += "" + c[i].index + "#"; + if (c[i].show_del == 1) html += ""; + html += ""; + html += "

"; + html += ""; + } + $("#commentList").append(html); + + if ($page.TotalPage > 1) { + $("#page").bootstrapPaginator({ + currentPage: $page.PageNo, + totalPages: $page.TotalPage, + bootstrapMajorVersion: 3, + size: "middle", + onPageClicked: function(e, originalEvent, type, page){ + pageClicked(page, $docid); + } + }); + } else { + $("#page").find("li").remove(); + } +} + +// 删除评论 +function onDelComment($id) { + console.log($id); + $.ajax({ + url : "/comment/delete", + data : {"id": $id}, + type : "POST", + success : function ($res) { + if ($res.errcode == 0) { + layer.msg("删除成功"); + $("div[data-id=" + $id + "]").remove(); + } else { + layer.msg($res.message); + } + }, + error : function () { + layer.msg("删除失败"); + } + }); +} + +// 重新渲染页面 +function renderPage($data) { + $("#page-content").html($data.body); + $("title").text($data.title); + $("#article-title").text($data.doc_title); + $("#article-info").text($data.doc_info); + $("#view_count").text("阅读次数:" + $data.view_count); + $("#doc_id").val($data.doc_id); + loadComment($data.page, $data.doc_id); +} + /*** * 加载文档到阅读区 * @param $url @@ -53,7 +150,7 @@ function loadDocument($url, $id, $callback) { $.ajax({ url : $url, type : "GET", - beforeSend : function (xhr) { + beforeSend : function () { var data = events.data($id); if(data) { if (typeof $callback === "function") { @@ -61,11 +158,7 @@ function loadDocument($url, $id, $callback) { }else if(data.version && data.version != $callback){ return true; } - $("#page-content").html(data.body); - $("title").text(data.title); - $("#article-title").text(data.doc_title); - $("#article-info").text(data.doc_info); - $("#view_count").text("阅读次数:" + data.view_count); + renderPage(data); events.trigger('article.open', {$url: $url, $id: $id}); @@ -75,29 +168,19 @@ function loadDocument($url, $id, $callback) { NProgress.start(); }, - success : function (res) { - if (res.errcode === 0) { - var body = res.data.body; - var doc_title = res.data.doc_title; - var title = res.data.title; - var doc_info = res.data.doc_info; - var view_count = res.data.view_count; + success : function ($res) { + if ($res.errcode === 0) { + renderPage($res.data); - $body = body; + $body = $res.data.body; if (typeof $callback === "function" ) { $body = $callback(body); } - $("#page-content").html($body); - $("title").text(title); - $("#article-title").text(doc_title); - $("#article-info").text(doc_info); - $("#view_count").text("阅读次数:" + view_count); - - events.data($id, res.data); + events.data($id, $res.data); events.trigger('article.open', { $url : $url, $id : $id }); - } else if (res.errcode === 6000) { + } else if ($res.errcode === 6000) { window.location.href = "/"; } else { layer.msg("加载失败"); @@ -129,9 +212,6 @@ function initHighlighting() { } } - - - $(function () { $(".view-backtop").on("click", function () { $('.manual-right').animate({ scrollTop: '0px' }, 200); @@ -287,4 +367,25 @@ $(function () { console.log($param); } }; + + // 提交评论 + $("#commentForm").ajaxForm({ + beforeSubmit : function () { + $("#btnSubmitComment").button("loading"); + }, + success : function (res) { + if(res.errcode === 0){ + layer.msg("保存成功"); + }else{ + layer.msg("保存失败"); + } + $("#btnSubmitComment").button("reset"); + $("#commentContent").val(""); + pageClicked(-1, res.data.doc_id); // -1 表示请求最后一页 + }, + error : function () { + layer.msg("服务错误"); + $("#btnSubmitComment").button("reset"); + } + }); }); \ No newline at end of file diff --git a/utils/pagination/pagination.go b/utils/pagination/pagination.go index 6a3067c8..ecce6c8c 100644 --- a/utils/pagination/pagination.go +++ b/utils/pagination/pagination.go @@ -132,3 +132,29 @@ func (p *Pagination) getLang() string { } return ulang } + +type Page struct { + PageNo int `json:"PageNo"` + PageSize int `json:"PageSize"` + TotalPage int `json:"TotalPage"` + TotalCount int `json:"TotalCount"` + FirstPage bool `json:"FirstPage"` + LastPage bool `json:"LastPage"` + List interface{} `json:"List"` +} + +func PageUtil(count int, pageNo int, pageSize int, list interface{}) Page { + tp := count / pageSize + if count%pageSize > 0 { + tp = count/pageSize + 1 + } + return Page { + PageNo: pageNo, + PageSize: pageSize, + TotalPage: tp, + TotalCount: count, + FirstPage: pageNo == 1, + LastPage: pageNo == tp, + List: list, + } +} diff --git a/views/book/setting.tpl b/views/book/setting.tpl index 1ad8fa74..858b99ea 100644 --- a/views/book/setting.tpl +++ b/views/book/setting.tpl @@ -96,6 +96,23 @@ +
+ +
+ + + +
+
{{if eq .Model.PrivatelyOwned 1}}