ソースを参照

评论完成

layui
yirenyishi 6年前
コミット
7e474bf7f7
  1. 83
      controllers/CommentController.go
  2. 1
      models/Blog.go
  3. 14
      models/Comment.go
  4. 3
      models/DataInit.go
  5. 1
      models/User.go
  6. 11
      routers/CommentRouter.go
  7. 13
      service/BlogService.go
  8. 80
      service/CommentService.go
  9. 7
      service/UserService.go
  10. 7
      static/css/blog.css
  11. 5
      static/css/common.css
  12. 1
      views/T.footer.tpl
  13. 118
      views/blog.html
  14. 2
      views/blogs.html

83
controllers/CommentController.go

@ -0,0 +1,83 @@
package controllers
import (
"github.com/astaxie/beego"
"beeblog/models"
"beeblog/service"
"strconv"
)
type CommentController struct {
beego.Controller
}
func (this *CommentController) Save() {
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
blogId, berr := this.GetInt64("blog")
if blogId == 0 || berr != nil {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
commVal := this.GetString("commval")
blog, err := service.ReadBlog(blogId)
if err != nil {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
comm := &models.Comment{BlogId: blogId, CuserId: uid.(int64), BuserId: blog.UserId, ComVal: commVal}
if pid, _ := this.GetInt64("pid"); pid != 0 {
parent := &models.Comment{Id: pid}
if err := service.ReadComment(parent); err == nil {
comm.BuserId = parent.CuserId
}
comm.Pid = pid
}
err = service.SaveComment(comm)
if err == nil {
this.Data["json"] = models.ReurnData("", comm)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountComments(uid.(int64))
return
}
func (this *CommentController) Del() {
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
comm := &models.Comment{Id: id}
err := service.ReadComment(comm)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
if comm.CuserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
err = service.DelComment(id)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountComments(uid.(int64))
return
}

1
models/Blog.go

@ -25,4 +25,5 @@ type Blog struct {
HeadImg string `orm:"-"`
CateName string `orm:"-"`
Lables []*NLabel `orm:"-"`
Comms []*Comment `orm:"-"`
}

14
models/Comment.go

@ -4,10 +4,14 @@ import "time"
type Comment struct {
Id int64
CuserId int64
BuserId int64
BlogId int64
Ctime time.Time `orm:"datetime"`
Pid int64
CuserId int64 `orm:"default(0)"`
BuserId int64 `orm:"default(0)"`
BlogId int64 `orm:"default(0)"`
Ctime time.Time `orm:"auto_now_add;type(datetime)"`
Pid int64 `orm:"default(0)"`
ComVal string `orm:"type(text)"`
Childs []*Comment `orm:"-"`
CUser *User `orm:"-"`
BUser *User `orm:"-"`
}

3
models/DataInit.go

@ -20,8 +20,9 @@ func RegistDB() {
// os.Create(_DB_NAME)
//}
//orm.RegisterModel(new(Attachment),new(Topic))
orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category),new(Like))
//orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
//orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category),new(Like),new(Comment))
orm.RegisterDataBase("default", "mysql", "root:booszy@tcp(127.0.0.1:3306)/beeblog?charset=utf8&loc=Local", 30)
}

1
models/User.go

@ -15,6 +15,7 @@ type User struct {
QQ string
HomeUrl string
Sex int `orm:"default(1)"`
Status int `orm:"default(0)"`
DescInfo string
Ctime time.Time `orm:"auto_now_add;type(datetime)"`

11
routers/CommentRouter.go

@ -0,0 +1,11 @@
package routers
import (
"github.com/astaxie/beego"
"beeblog/controllers"
)
func init() {
beego.Router("/comms/save", &controllers.CommentController{}, "post:Save")
beego.Router("/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del")
}

13
service/BlogService.go

@ -47,6 +47,15 @@ func TopBlogByUser(uid int64) ([]*models.Blog, error) {
return blogs, nil
}
func ReadBlog(id int64) (*models.Blog, error) {
o := orm.NewOrm()
blog := &models.Blog{Id: id}
if err := o.Read(blog);err != nil {
return nil, err
}
return blog, nil
}
func GetBlog(id int64) (*models.Blog, error) {
o := orm.NewOrm()
blog := &models.Blog{Id: id}
@ -65,6 +74,10 @@ func GetBlog(id int64) (*models.Blog, error) {
if err == nil {
blog.Lables = labels
}
comms , berr:= FindCommentByBlog(id)
if berr == nil{
blog.Comms = comms
}
return blog, nil
}

80
service/CommentService.go

@ -0,0 +1,80 @@
package service
import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"fmt"
)
func FindCommentByBlog(bid int64) ([]*models.Comment, error) {
var comms []*models.Comment
o := orm.NewOrm()
_, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).OrderBy("-Ctime").All(&comms)
if err != nil {
return nil, err
}
if len(comms) > 0 {
for i := 0; i < len(comms); i++ {
var childs []*models.Comment
_, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs)
if childerrr == nil {
if len(childs) > 0{
comms[i].Childs = childs
for j:=0 ; j<len(childs);j++{
cuser := &models.User{Id: childs[j].CuserId}
o.Read(cuser)
childs[j].CUser = cuser
buser := &models.User{Id: childs[j].BuserId}
o.Read(buser)
childs[j].BUser = buser
}
}
}
cuser := &models.User{Id: comms[i].CuserId}
o.Read(cuser)
comms[i].CUser = cuser
}
}
return comms, nil
}
func SaveComment(comment *models.Comment) error {
o := orm.NewOrm()
id, err := o.Insert(comment)
if err == nil {
comment.Id = id
cuser := &models.User{Id: comment.CuserId}
o.Read(cuser)
comment.CUser = cuser
if comment.BuserId !=0 {
buser := &models.User{Id: comment.BuserId}
o.Read(buser)
comment.BUser = buser
}
return nil
}
return err
}
func ReadComment(comment *models.Comment) error {
return orm.NewOrm().Read(comment)
}
func DelComment(id int64) error {
comm := &models.Comment{Id: id}
o := orm.NewOrm()
err := o.Read(comm)
if err != nil {
return err
}
if comm.Pid != 0 {
if _, err := o.QueryTable(models.Comment{}).Filter("Pid", id).Delete(); err != nil {
fmt.Println(err)
}
}
if _, err := o.Delete(comm); err != nil {
return err
}
return nil
}

7
service/UserService.go

@ -54,7 +54,10 @@ func CountBrows(uid int64) {
return
}
func CountComments(uid int64) {
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
return
}
func CountLike(uid int64) {
o := orm.NewOrm()
@ -63,6 +66,6 @@ func CountLike(uid int64) {
return
}
func EditUser(user *models.User) (int64, error){
func EditUser(user *models.User) (int64, error) {
return orm.NewOrm().Update(user)
}

7
static/css/blog.css

@ -18,4 +18,11 @@
width: 260px;
height: 100%;
background-color: #f9f9f9;
}
.comms-root{
margin: 50px 4% 0 300px;
}
.comms-root a{
text-decoration: none;
}

5
static/css/common.css

@ -19,7 +19,6 @@ body {
}
.root-container {
position: absolute;
height: 100%;
width: 100%;
max-width: 100%;
@ -71,8 +70,8 @@ body {
.wxcode{
position: absolute;
right: 0;
top: -5px;
right: 11px;
top: 11px;
}
.login-footer .wxcode{

1
views/T.footer.tpl

@ -1,5 +1,6 @@
{{define "footer"}}
<div class="footer-root">
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 0"/>
<div class="footer-friend" style="margin-top: 15px">
<a href="https://blog.csdn.net/yp090416" target="_blank" style="padding: 0 5px">码农笔录-CSDN</a>
<a href="https://www.jianshu.com/u/6baf4cdc7ce1" target="_blank" style="padding: 0 5px">码农笔录-简书</a>

118
views/blog.html

@ -7,8 +7,9 @@
{{template "nav" .}}
<div class="blog-user">
<div style="height: 50px;line-height: 50px">
<a href=""><img src="/static/img/2.png" alt="头像" class="img-circle"></a>
<a href="" style="margin-left: 15px;font-size: 18px;text-decoration: none">{{.Blog.User.NickName}}</a>
<a href="/u/{{.Blog.User.Id}}"><img src="/static/img/2.png" alt="头像" class="img-circle"></a>
<a href="/u/{{.Blog.User.Id}}"
style="margin-left: 15px;font-size: 18px;text-decoration: none">{{.Blog.User.NickName}}</a>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>
<div style="display: flex;height: 30px;line-height: 30px;">
@ -35,7 +36,9 @@
{{range .Top}}
<div>
<a href=""><p style="max-width:245px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{.Title}}</p></a>
<a href=""><p
style="max-width:245px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{.Title}}</p>
</a>
</div>
{{end}}
</div>
@ -47,7 +50,7 @@
{{else}}
<span><img src="/static/img/like.png" alt="" onclick="like({{.Blog.Id}})"></span>
{{end}}
<span><a href="">{{.Blog.User.NickName}}</a></span>
<span><a href="/u/{{.Blog.User.Id}}">{{.Blog.User.NickName}}</a></span>
<span><em>时间:</em>{{.Blog.Ctime.Format "2006-01-02"}}</span>
<span><em>阅读数:</em>{{.Blog.Browses}}</span>
<div class="tag-group" style="display: inline-block">
@ -59,12 +62,115 @@
<div style="width: 100%">
{{str2html .Blog.BlogHtml}}
</div>
{{template "footer"}}
</div>
<div class="comms-root">
<textarea class="form-control" rows="3" id="commval"></textarea>
<div style="text-align: end">
<button type="button" class="btn btn-info btn-bordered" id="commBtn">发表评论</button>
</div>
<div class="comm-container">
{{range .Blog.Comms}}
<div style="font-size: 14px;margin-top: 10px" class="pcomm-{{.Id}}">
<span><a href="/u/{{.CUser.Id}}">{{.CUser.NickName}}</a>:</span>
<span>{{.ComVal}}</span>
<span onclick="recomm({{.Id}},0,this)" style="font-size: 10px"><a
href="javascript:void(0)">回复</a></span>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 5px"/>
<div class="child-container">
{{range .Childs}}
<div style="margin-left: 18px" class="ccomm-{{.Id}}">
<span>
<a href="/u/{{.CUser.Id}}">{{.CUser.NickName}}</a> 回复
<a href="/u/{{.BUser.Id}}">{{.BUser.NickName}}</a>:
</span>
<span>{{.ComVal}}</span>
<span onclick="recomm({{.Id}},1)" style="font-size: 10px"><a
href="javascript:void(0)">回复</a></span>
</div>
{{end}}
</div>
</div>
{{end}}
</div>
<div style="margin-top: 50px">
{{template "footer"}}
</div>
</div>
</div>
</div>
</body>
<script>
$(function () {
$("#commBtn").click(function () {
var commval = $("#commval").val()
if (!commval || commval.trim().length < 3) {
layer.msg('评论最少三个字', function () {
});
return
}
$.post('/comms/save', {
blog: {{.Blog.Id}},
commval: commval
},
function (data) {
if (data.Status == 0) {
var res = ' <div style="font-size: 14px;margin-top: 10px" class="pcomm-' + data.Data.Id + '">\n' +
' <span><a href="/u/' + data.Data.CuserId + '">' + data.Data.CUser.NickName + '</a>:</span>\n' +
' <span>' + commval + '</span>\n' +
'<span onclick="recomm(' + data.Data.Id + ',0)" style="font-size: 10px"><a href="javascript:void(0)">回复</a></span>' +
' <hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 5px"/>\n' +
' <div class="child-container"></div> </div>'
$(".comm-container").append(res)
$("#commval").val("")
layer.msg("保存成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
layer.msg("保存失败", {icon: 5});
window.location.href = "/"
}
}, 'json')
})
})
function recomm(pid, flag) {
layer.prompt({title: '请输入评论内容', value: name, formType: 2}, function (val, index) {
if (val.trim().length < 3) {
layer.msg('评论最少三个字', function () {
});
layer.close(index);
return
}
$.post('/comms/save', {blog: {{.Blog.Id}},commval: val,pid: pid},
function (data) {
if (data.Status == 0) {
var res = '<div style="margin-left: 18px" class="ccomm-' + data.Data.Id + '">\n' +
' <span>\n' +
' <a href="/u/' + data.Data.CUser.Id + '">' + data.Data.CUser.NickName + '</a> 回复\n' +
' <a href="/u/' + data.Data.BUser.Id + '">' + data.Data.BUser.NickName + '</a>:\n' +
' </span>\n' +
' <span>' + val + '</span>\n' +
' <span onclick="recomm(' + data.Data.Id + ',1)" style="font-size: 10px"><a href="javascript:void(0)">回复</a></span>\n' +
' </div>'
if (flag == 0) {
$(".pcomm-" + pid + " .child-container").append(res)
} else {
$($(".ccomm-" + pid)[0]).parent().append(res)
}
$("#commval").val("")
layer.close(index);
layer.msg("保存成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
layer.msg("保存失败", {icon: 5});
window.location.href = "/"
}
}, 'json')
});
}
function like(id) {
$.get('/like/' + id,
function (data) {
@ -72,8 +178,6 @@
window.location.href = window.location.href
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}

2
views/blogs.html

@ -87,7 +87,7 @@
</nav>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE"/>
{{template "footer"}}
</div>
</div>

読み込み中…
キャンセル
保存