增加删除评论

pull/678/head
wangbin05 2021-04-03 22:44:43 +08:00
parent 7fb0e66ddc
commit 3d12583eba
7 changed files with 157 additions and 72 deletions

View File

@ -16,16 +16,14 @@ type CommentController struct {
}
func (c *CommentController) Lists() {
docid, _ := c.GetInt("docid", 0)
pageIndex, _ := c.GetInt("page", 1)
beego.Info("CommentController.Lists", docid, pageIndex)
// 获取评论、分页
comments, count := models.NewComment().QueryCommentByDocumentId(docid, pageIndex, conf.PageSize)
comments, count, pageIndex := models.NewComment().QueryCommentByDocumentId(docid, pageIndex, conf.PageSize, c.Member.MemberId)
page := pagination.PageUtil(int(count), pageIndex, conf.PageSize, comments)
beego.Info("docid=", docid, "Page", page)
var data struct {
DocId int `json:"doc_id"`
@ -57,11 +55,30 @@ func (c *CommentController) Create() {
beego.Info(m)
m.Insert()
c.JsonResult(0, "ok")
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)
beego.Info("delete id=", id)
m := models.NewComment()
m.CommentId = id
err := m.Delete()
if err != nil {
c.JsonResult(1, "删除错误")
} else {
c.JsonResult(0, "ok")
}
}
}

View File

@ -70,10 +70,9 @@ func (c *DocumentController) Index() {
c.Data["DocumentId"] = doc.DocumentId
// 获取评论、分页
comments, count := models.NewComment().QueryCommentByDocumentId(doc.DocumentId, 1, conf.PageSize)
comments, count, _ := models.NewComment().QueryCommentByDocumentId(doc.DocumentId, 1, conf.PageSize, c.Member.MemberId)
page := pagination.PageUtil(int(count), 1, conf.PageSize, comments)
c.Data["Page"] = page
beego.Info("docid=", doc.DocumentId, "Page", page)
}
} else {
c.Data["Title"] = "概要"
@ -156,10 +155,9 @@ func (c *DocumentController) Read() {
c.Data["ViewCount"] = doc.ViewCount + 1
// 获取评论、分页
comments, count := models.NewComment().QueryCommentByDocumentId(doc.DocumentId, 1, conf.PageSize)
comments, count, _ := models.NewComment().QueryCommentByDocumentId(doc.DocumentId, 1, conf.PageSize, c.Member.MemberId)
page := pagination.PageUtil(int(count), 1, conf.PageSize, comments)
c.Data["Page"] = page
beego.Info("docid=", doc.DocumentId, "Page", page)
if c.IsAjax() {
var data struct {

View File

@ -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 获取对应数据库表名.
@ -64,11 +66,26 @@ func (m *Comment) Find(id int) (*Comment, error) {
}
// 根据文档id查询文档评论
func (m *Comment) QueryCommentByDocumentId(doc_id, page, pagesize int) (comments []Comment, count int64) {
func (m *Comment) QueryCommentByDocumentId(doc_id, page, pagesize, userid int) (comments []Comment, count int64, ret_page int) {
o := orm.NewOrm()
offset := (page - 1) * pagesize
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).OrderBy("comment_date").Offset(offset).Limit(pagesize).All(&comments)
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)
for i := 0; i < len(comments); i++ {
comments[i].Index = (i + 1) + (page - 1) * pagesize
if userid == comments[i].MemberId {
comments[i].ShowDel = 1
}
}
return
}
@ -145,3 +162,10 @@ func (m *Comment) Insert() error {
return err
}
// 删除一条评论
func (m *Comment) Delete() error {
o := orm.NewOrm()
_, err := o.Delete(m)
return err
}

View File

@ -134,6 +134,7 @@ func init() {
beego.Router("/attach_files/:key/:attach_id", &controllers.DocumentController{}, "get:DownloadAttachment")
beego.Router("/comment/create", &controllers.CommentController{}, "post:Create")
beego.Router("/comment/delete", &controllers.CommentController{}, "post:Delete")
beego.Router("/comment/lists", &controllers.CommentController{}, "get:Lists")
beego.Router("/comment/index", &controllers.CommentController{}, "*:Index")

File diff suppressed because one or more lines are too long

View File

@ -42,12 +42,28 @@ var events = function () {
}();
function format(d) {
return d < 10 ? "0" + d : "" + d;
function format($d) {
return $d < 10 ? "0" + $d : "" + $d;
}
function timeFormat(time) {
var span = Date.parse(time)
function showError($msg, $id) {
if (!$id) {
$id = "#form-error-message"
}
$($id).addClass("text-danger").removeClass("text-success").text($msg);
return false;
}
function showSuccess($msg, $id) {
if (!$id) {
$id = "#form-error-message"
}
$($id).addClass("text-success").removeClass("text-danger").text($msg);
return true;
}
function timeFormat($time) {
var span = Date.parse($time)
var date = new Date(span)
var year = date.getFullYear();
var month = format(date.getMonth() + 1);
@ -58,19 +74,14 @@ function timeFormat(time) {
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
}
function onPageClicked(page, docid) {
// 点击翻页
function pageClicked($page, $docid) {
$.ajax({
url : "/comment/lists?page=" + page + "&docid=" + docid,
url : "/comment/lists?page=" + $page + "&docid=" + $docid,
type : "GET",
beforeSend : function (xhr) {
NProgress.start();
},
success : function (res) {
console.log(res.data);
loadComment(res.data.page, res.data.doc_id);
},
complete : function () {
NProgress.done();
success : function ($res) {
console.log($res.data);
loadComment($res.data.page, $res.data.doc_id);
},
error : function () {
layer.msg("加载失败");
@ -79,31 +90,33 @@ function onPageClicked(page, docid) {
}
// 加载评论
function loadComment(page, docid) {
function loadComment($page, $docid) {
$("#commentList").empty();
var html = ""
var c = page.List;
var c = $page.List;
for (var i = 0; c && i < c.length; i++) {
html += "<div class=\"comment-item\" data-id=\"" + c[i].comment_id + "\">";
html += "<p class=\"info\"><a class=\"name\">" + c[i].author + "</a><span class=\"date\">" + timeFormat(c[i].comment_date) + "</span></p>";
html += "<div class=\"content\">" + c[i].content + "</div>";
html += "<p class=\"util\">";
html += "<span class=\"operate\">";
html += "<span class=\"number\">" + i + "#</span>";
if (c[i].show_del == 1) html += "<span class=\"operate toggle\">";
else html += "<span class=\"operate\">";
html += "<span class=\"number\">" + c[i].index + "#</span>";
if (c[i].show_del == 1) html += "<i class=\"delete e-delete glyphicon glyphicon-remove\" style=\"color:red\" onclick=\"onDelComment(" + c[i].comment_id + ")\"></i>";
html += "</span>";
html += "</p>";
html += "</div>";
}
$("#commentList").append(html);
if (page.TotalPage > 1) {
if ($page.TotalPage > 1) {
$("#page").bootstrapPaginator({
currentPage: page.PageNo,
totalPages: page.TotalPage,
currentPage: $page.PageNo,
totalPages: $page.TotalPage,
bootstrapMajorVersion: 3,
size: "middle",
onPageClicked: function(e,originalEvent,type,page){
onPageClicked(page, docid);
onPageClicked: function(e, originalEvent, type, page){
pageClicked(page, $docid);
}
});
} else {
@ -111,15 +124,36 @@ function loadComment(page, docid) {
}
}
// 删除评论
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);
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);
}
/***
@ -132,7 +166,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") {
@ -150,19 +184,19 @@ function loadDocument($url, $id, $callback) {
NProgress.start();
},
success : function (res) {
if (res.errcode === 0) {
renderPage(res.data);
success : function ($res) {
if ($res.errcode === 0) {
renderPage($res.data);
$body = res.data.body;
$body = $res.data.body;
if (typeof $callback === "function" ) {
$body = $callback(body);
}
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("加载失败");
@ -349,4 +383,25 @@ $(function () {
console.log($param);
}
};
// 提交评论
$("#commentForm").ajaxForm({
beforeSubmit : function () {
$("#btnSubmitComment").button("loading");
},
success : function (res) {
if(res.errcode === 0){
showSuccess("保存成功")
}else{
showError("保存失败")
}
$("#btnSubmitComment").button("reset");
$("#commentContent").val("");
pageClicked(-1, res.data.doc_id); // -1 表示请求最后一页
},
error : function () {
showError("服务错误");
$("#btnSaveBookInfo").button("reset");
}
});
});

View File

@ -167,6 +167,7 @@
</div>
</div>
</div>
<div class="article-content">
<!-- 文章内容 -->
<div class="article-body {{if eq .Model.Editor "markdown"}}markdown-body editormd-preview-container{{else}}editor-content{{end}}" id="page-content">
@ -182,8 +183,11 @@
<p class="info"><a class="name">{{$c.Author}}</a><span class="date">{{date $c.CommentDate "Y-m-d H:i:s"}}</span></p>
<div class="content">{{$c.Content}}</div>
<p class="util">
<span class="operate">
<span class="number">{{$i}}#</span>
<span class="operate {{if eq $c.ShowDel 1}}toggle{{end}}">
<span class="number">{{$c.Index}}#</span>
{{if eq $c.ShowDel 1}}
<i class="delete e-delete glyphicon glyphicon-remove" style="color:red" onclick="onDelComment({{$c.CommentId}})"></i>
{{end}}
</span>
</p>
</div>
@ -197,11 +201,12 @@
<div class="comment-post">
<form class="form" id="commentForm" action="{{urlfor "CommentController.Create"}}" method="post">
<label class="enter w-textarea textarea-full">
<textarea class="textarea-input form-control" name="content" placeholder="文明上网,理性发言" style="height: 72px;"></textarea>
<textarea class="textarea-input form-control" name="content" id="commentContent" placeholder="文明上网,理性发言" style="height: 72px;"></textarea>
<input type="hidden" name="doc_id" id="doc_id" value="{{.DocumentId}}">
</label>
<div class="pull-right">
<button class="btn btn-success btn-sm" type="submit">发布</button>
<span id="form-error-message" class="error-message"></span>
<button class="btn btn-success btn-sm" type="submit" id="btnSubmitComment" data-loading-text="提交中...">提交评论</button>
</div>
</form>
</div>
@ -297,7 +302,7 @@ if ({{.Page.TotalPage}} > 1) {
bootstrapMajorVersion: 3,
size: "middle",
onPageClicked: function(e, originalEvent, type, page){
onPageClicked(page, {{.DocumentId}});
pageClicked(page, {{.DocumentId}});
}
});
}
@ -349,22 +354,6 @@ $(function () {
window.jsTree.jstree().open_all()
}
})
// 提交评论
$("#commentForm").ajaxForm({
beforeSubmit : function () {
},
success : function (res) {
if(res.errcode === 0){
console.log("success")
}else{
console.log("error")
}
},
error : function () {
console.log("server error")
}
});
});
</script>
{{.Scripts}}