[ADD]评论功能
This commit is contained in:
parent
4f636c9f4a
commit
3f720e9776
|
@ -3,9 +3,9 @@ package controllers
|
|||
import (
|
||||
"errors"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/satori/go.uuid"
|
||||
"liteblog/models"
|
||||
"liteblog/syserrors"
|
||||
"github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
const SESSION_USER_KEY = "SESSION_USER_KEY"
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"liteblog/models"
|
||||
"liteblog/syserrors"
|
||||
"log"
|
||||
)
|
||||
|
||||
type CommentController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
// 新增评论
|
||||
// @router /new/:key [post]
|
||||
func (c *CommentController) NewComment() {
|
||||
log.Print("开始评论了....")
|
||||
|
||||
c.MustLogin()
|
||||
key := c.Ctx.Input.Param(":key")
|
||||
content := c.GetMustString("content", "请输入评论内容")
|
||||
k := c.UUID()
|
||||
|
||||
comment := &models.Comment{
|
||||
Key: k,
|
||||
NoteKey: key,
|
||||
User: c.User,
|
||||
UserId: int(c.User.ID),
|
||||
Content: content,
|
||||
}
|
||||
|
||||
if err := models.SaveComment(comment); err != nil {
|
||||
log.Print("保存失败了")
|
||||
|
||||
c.Abort500(syserrors.NewError("保存失败", err))
|
||||
}
|
||||
|
||||
c.JSONOkH("保存成功", nil)
|
||||
}
|
|
@ -46,7 +46,7 @@ func (c *IndexController) Get() {
|
|||
|
||||
// 显示文章
|
||||
// @router /details/:key [get]
|
||||
func (c *IndexController) GetDetail() {
|
||||
func (c *IndexController) GetDetail() {
|
||||
// 得到页面传过来的key
|
||||
key := c.Ctx.Input.Param(":key")
|
||||
// 到数据查询对应key的文章
|
||||
|
@ -56,10 +56,31 @@ func (c *IndexController) GetDetail() {
|
|||
c.Abort500(syserrors.NewError("文章不存在", err))
|
||||
}
|
||||
|
||||
comments, err := models.QueryCommentByNoteKey(key)
|
||||
if err != nil {
|
||||
c.Abort500(syserrors.NewError("文章不存在", err))
|
||||
}
|
||||
|
||||
c.Data["note"] = note
|
||||
c.Data["comments"] = comments
|
||||
c.TplName = "details.html"
|
||||
|
||||
}
|
||||
|
||||
// 显示文章评论
|
||||
// @router /comment/:key [get]
|
||||
func (c *IndexController) GetComment() {
|
||||
key := c.Ctx.Input.Param(":key")
|
||||
note, err := models.QueryNoteByKey(key)
|
||||
|
||||
if err != nil {
|
||||
c.Abort500(syserrors.NewError("文章不存在", err))
|
||||
}
|
||||
|
||||
c.Data["note"] = note
|
||||
c.TplName = "comment.html"
|
||||
}
|
||||
|
||||
//留言
|
||||
// @router /message [get]
|
||||
func (c *IndexController) GetMessage() {
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package models
|
||||
|
||||
type Comment struct {
|
||||
Model
|
||||
Key string `gorm:"unique_index; not null" json:"key"`
|
||||
Content string `json:"content"`
|
||||
UserId int `json:"user_id"`
|
||||
User User `json:"user"`
|
||||
NoteKey string `json:"note_key"`
|
||||
Praise int `gorm:"default:0" json:"praise"`
|
||||
}
|
||||
|
||||
func QueryCommentByNoteKey(noteKey string) (comments []*Comment, err error) {
|
||||
return comments, db.Preload("User").Where("`note_key` = ?", noteKey).Order("`updated_at` desc").Find(&comments).Error
|
||||
}
|
||||
|
||||
func SaveComment(comment *Comment) (err error) {
|
||||
return db.Save(comment).Error
|
||||
}
|
|
@ -31,7 +31,7 @@ func init() {
|
|||
// 自动迁移模式
|
||||
db.AutoMigrate(&User{},
|
||||
&Note{},
|
||||
//&Model.UserAuthsModel{},
|
||||
&Comment{},
|
||||
)
|
||||
|
||||
var count int
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
type Note struct {
|
||||
Model
|
||||
Key string `gorm:"unique_index; not null"` // 文章唯一标识
|
||||
|
|
|
@ -17,5 +17,9 @@ func init() {
|
|||
"note",
|
||||
beego.NSInclude(&controllers.NoteController{}),
|
||||
),
|
||||
beego.NewNamespace(
|
||||
"comment",
|
||||
beego.NSInclude(&controllers.CommentController{}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -12,26 +12,17 @@
|
|||
<div class="container container-message container-details container-comment">
|
||||
<div class="contar-wrap">
|
||||
<div class="item">
|
||||
<div class="item-box layer-photos-demo1 layer-photos-demo">
|
||||
<h3>拥有诗意的心态,才能拥有诗意的生活</h3>
|
||||
<h5>发布于:<span>刚刚</span></h5>
|
||||
<p>父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌,手把手教我走路、骑车,却会在该放手的时刻果断地放开让自己去大胆尝试,那个时候期望快快长大,能够做自己想做的事,不用受父亲的“控制”。父亲是智慧树,他无所不知、无所不晓,虽然你有十万个为什么,但是也难不倒他。</p>
|
||||
<img src="/static/images/item.png"
|
||||
alt="帅哥">
|
||||
<div class="count layui-clear">
|
||||
<span class="pull-left">阅读 <em>100000+</em></span>
|
||||
<span class="pull-right like"><i class="layui-icon layui-icon-praise"></i><em>999</em></span>
|
||||
</div>
|
||||
</div>
|
||||
{{ template "shares/note_summary_tpl.html" .note}}
|
||||
</div>
|
||||
<form class="layui-form" action="">
|
||||
<input type="hidden" name="key" value="{{ .note.Key }}">
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<textarea class="layui-textarea" style="resize:none" placeholder="写点什么啊"></textarea>
|
||||
<textarea class="layui-textarea" name="content" style="resize:none" placeholder="写点什么啊"></textarea>
|
||||
</div>
|
||||
<div class="btnbox">
|
||||
<a href="details.html" id="sure">
|
||||
确定
|
||||
</a>
|
||||
<button lay-submit="" class="layui-btn" lay-filter="comment" >
|
||||
确定
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -57,5 +48,29 @@
|
|||
</div>
|
||||
</div>
|
||||
</script>
|
||||
<script>
|
||||
layui.use(['form',"jquery","layer"],function () {
|
||||
var form = layui.form,$= layui.jquery,layer = layui.layer;
|
||||
// 监听评论的form表单提交事件。
|
||||
form.on("submit(comment)",function (formData) {
|
||||
//我们假定评论新增路由的路径为 "/message/new/:key" key是文章的key
|
||||
$.post("/comment/new/"+formData.field.key,{content:formData.field.content},function (data) {
|
||||
if(data.code === 0){
|
||||
layer.msg("评论成功!");
|
||||
//评论成功就 跳转到文章详情页面
|
||||
setTimeout(function () {
|
||||
window.location.href = "/details/"+formData.field.key;
|
||||
},300)
|
||||
}else{
|
||||
layer.msg(data.msg);
|
||||
}
|
||||
},"json").error(function () {
|
||||
layer.msg("网络异常")
|
||||
});
|
||||
// 此处一定要return false,阻止浏览器默认事件,如果没有这段代码,form表单提交会直接刷新页面,走浏览器默认事件。
|
||||
return false;
|
||||
})
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -17,29 +17,21 @@
|
|||
<a name="comment"> </a>
|
||||
<div class="comt layui-clear">
|
||||
<a href="javascript:;" class="pull-left">评论</a>
|
||||
<a href="comment.html" class="pull-right">写评论</a>
|
||||
<a href="/comment/{{ .note.Key }}" class="pull-right">写评论</a>
|
||||
</div>
|
||||
<div id="LAY-msg-box">
|
||||
{{ range .comments }}
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="name">{{ .User.Name }}</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
<p class="info-intr">{{ .Content }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue