소스 검색

修改博客

layui
yirenyishi 6 년 전
부모
커밋
ec2f3bbe03
  1. 56
      controllers/BlogController.go
  2. 5
      controllers/PageController.go
  3. 4
      models/User.go
  4. 2
      routers/BlogRouter.go
  5. 37
      service/BlogService.go
  6. 6
      service/UserService.go
  7. 10
      views/403.html
  8. 114
      views/editblog.html
  9. 1
      views/newblog.html
  10. 10
      views/ublogs.html
  11. 9
      views/ulike.html

56
controllers/BlogController.go

@ -5,12 +5,34 @@ import (
"beeblog/service"
"github.com/astaxie/beego"
"strconv"
"time"
)
type BlogController struct {
beego.Controller
}
func (this *BlogController) EditPage() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/500", 302)
return
}
if blog.UserId != uid.(int64) {
this.Redirect("/403", 302)
return
}
this.Data["Blog"] = blog
this.TplName = "editblog.html"
}
func (this *BlogController) Save() {
uid := this.GetSession("userid")
if uid == nil {
@ -25,6 +47,40 @@ func (this *BlogController) Save() {
labels := this.GetStrings("labels[]")
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
err := service.SaveBlog(blog, labels)
if err == nil {
this.Data["json"] = models.ReurnData("",blog.Id)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
func (this *BlogController) Edit() {
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
id,_ := this.GetInt64("id")
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog,err :=service.GetBlog(id)
if err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
this.ServeJSON()
return
}
blog.Title = title
blog.BlogHtml = blogHtml
blog.CategoryId = catoryId
blog.Utime = time.Now()
err = service.EditBlog(blog, labels)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {

5
controllers/PageController.go

@ -52,3 +52,8 @@ func (this *PageController) PageNotFound() {
func (this *PageController) ServerError() {
this.TplName = "500.html"
}
// @router /403 [get]
func (this *PageController) ServerDemined() {
this.TplName = "403.html"
}

4
models/User.go

@ -23,3 +23,7 @@ type User struct {
BlogComment int `orm:"default(0)"`
BlogLike int `orm:"default(0)"`
}
func (u *User) TableName() string {
return "auth_user"
}

2
routers/BlogRouter.go

@ -7,6 +7,8 @@ import (
func init() {
beego.Router("/blog/new", &controllers.BlogController{}, "post:Save")
beego.Router("/blog/edit", &controllers.BlogController{}, "post:Edit")
beego.Router("/blog/edit/:id([0-9]+)", &controllers.BlogController{}, "get:EditPage")
beego.Router("/blog/:id([0-9]+)", &controllers.BlogController{}, "get:Get")
beego.Router("/blog/del/:id([0-9]+)", &controllers.BlogController{}, "post:Del")
beego.Router("/blogs", &controllers.BlogController{}, "get:BlogsPage")

37
service/BlogService.go

@ -6,7 +6,6 @@ import (
"beeblog/utils"
)
func count(num int, size int, cat int64) (*utils.Page, error) {
o := orm.NewOrm()
qs := o.QueryTable(&models.Blog{})
@ -44,7 +43,7 @@ func EditBlogBrows(id int64) {
func TopBlogByUser(uid int64) ([]*models.Blog, error) {
o := orm.NewOrm()
var blogs []*models.Blog
o.QueryTable(models.Blog{}).Filter("UserId",uid).Limit(12,0).OrderBy("-Browses").All(&blogs)
o.QueryTable(models.Blog{}).Filter("UserId", uid).Limit(12, 0).OrderBy("-Browses").All(&blogs)
return blogs, nil
}
@ -141,6 +140,30 @@ func SaveBlog(blog *models.Blog, strs []string) error {
return nil
}
func EditBlog(blog *models.Blog, strs []string) error {
o := orm.NewOrm()
o.Begin()
id, eror := o.Update(blog)
if eror != nil {
o.Rollback()
return eror
} else {
o.QueryTable(models.NLabel{}).Filter("BlogId",blog.Id).Delete()
if strs != nil && len(strs) > 0 {
nlabels := make([]*models.NLabel, len(strs))
for i := 0; i < len(strs); i++ {
nlabels[i] = &models.NLabel{Title: strs[i], BlogId: id, UserId: blog.UserId}
}
if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil {
o.Rollback()
return err
}
}
o.Commit()
}
return nil
}
func MeBlogs(num int, size int, flag int, uid int64) (*utils.Page, error) {
page, err := countByUser(num, size, uid)
if err != nil {
@ -164,3 +187,13 @@ func MeBlogs(num int, size int, flag int, uid int64) (*utils.Page, error) {
page.List = blogs
return page, nil
}
func GetNLabel(id int64) ([]*models.NLabel, error) {
var labels []*models.NLabel
o := orm.NewOrm()
_, err := o.QueryTable(&models.NLabel{}).Filter("BlogId", id).All(&labels)
if err != nil {
return labels, err
}
return labels, nil
}

6
service/UserService.go

@ -44,13 +44,13 @@ func SaveUser(user *models.User) error {
func CountBlog(uid int64) {
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `user` SET `blog_count` = (SELECT count(id) FROM blog where delflag = 0 and user_id =?1 ) WHERE `id` = ?2 ", uid, uid).QueryRow(&browses)
o.Raw("UPDATE `auth_user` SET `blog_count` = (SELECT count(id) FROM blog where delflag = 0 and user_id =? ) WHERE `id` = ? ", uid, uid).QueryRow(&browses)
return
}
func CountBrows(uid int64) {
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?1) WHERE `id` = ?2 ", uid, uid).QueryRow(&browses) //获取总条数
o.Raw("UPDATE `auth_user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
return
}
func CountComments(uid int64) {
@ -59,7 +59,7 @@ func CountComments(uid int64) {
func CountLike(uid int64) {
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `user` SET `blog_like` = (select count(id) from like where user_id = ?1) WHERE `id` = ?2 ", uid, uid).QueryRow(&browses)
o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) from like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses)
return
}

10
views/403.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>404</h1>
</body>
</html>

114
views/editblog.html

@ -0,0 +1,114 @@
{{template "header"}}
<title>博客 - 个人随笔</title>
<link type="text/css" rel="styleSheet" href="/static/css/newblog.css"/>
<script src="//unpkg.com/wangeditor/release/wangEditor.min.js"></script>
</head>
<body>
<div class="root-container">
<div class="blog-title">
<input type="text" id="blog-title" placeholder="请输入文章标题" value="{{.Blog.Title}}">
<button type="button" id="newBtn" class="btn btn-danger" style="border-radius: 12px">发布文章</button>
</div>
<div id="editor" class="editor">
</div>
<div id="myModal" tabindex="-1" role="dialog" data-hasfoot="false" class="sui-modal hide fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close">×</button>
<h4 id="myModalLabel" class="modal-title">Modal title</h4>
</div>
<div class="modal-body">我是内容body</div>
<div class="modal-footer">
<button type="button" data-ok="modal" class="sui-btn btn-primary btn-large">可自定ok</button>
<button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large">可自定dismiss</button>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
var tobj = {}
var storage = window.localStorage
var E = window.wangEditor
var editor = new E('#editor')
editor.customConfig.uploadImgServer = '/upload'
// 或者 var editor = new E( document.getElementById('editor') )
editor.create()
document.onkeydown = keyDown;
function keyDown(e) {
var currKey = 0, e = e || event || window.event;
currKey = e.keyCode || e.which || e.charCode;
if (currKey == 83 && (e.ctrlKey || e.metaKey)) {
layer.msg("ctrl+s")
saveBlog()
return
}
}
function saveBlog() {
var title = $("#blog-title").val();
if (title.trim().length < 3) {
layer.msg('文章标题最少三个字', function () {
});
return
}
tobj.title = $("#blog-title").val();
if (editor.txt.text().trim().length == 0) {
layer.msg('文章内容不能为空哦', function () {
});
return
}
tobj.blogHtml = editor.txt.html();
storage.setItem("blogHtml", tobj.blogHtml);
storage.setItem("blogTitle", tobj.title);
layer.open({
type: 2,
title: '发布文章',
shadeClose: true,
shade: 0.8,
area: ['420px', '280px'],
content: '/iframe/blog.html?id={{.Blog.Id}}' //iframe的url
});
}
$(function () {
editor.txt.html({{.Blog.BlogHtml}});
tobj.id = {{.Blog.Id}};
tobj.catory = {{.Blog.CategoryId}};
tobj.labels = []
console.log({{.Blog.Lables}})
{{range .Blog.Lables}}
tobj.labels.push({{.Title}})
{{end}}
var height = document.documentElement.clientHeight
$(".w-e-text-container").height(height - 50 - 40 - 1);
$("#newBtn").click(function () {
saveBlog()
})
})
function saveBlogCallback(callback) {
$.post('/blog/edit', tobj,
function (data) {
if (data.Status == 0) {
storage.removeItem("blogHtml");
storage.removeItem("blogTitle");
layer.msg("保存成功", {icon: 6});
callback(true)
window.location.href = "/blog/{{.Blog.Id}}"
} else if (data.Status == 401) {
window.location.href = "login"
} else if (data.Status == 500) {
layer.msg("保存失败", {icon: 6});
callback(false)
}
}, 'json')
}
</script>
</html>

1
views/newblog.html

@ -94,6 +94,7 @@
storage.removeItem("blogTitle");
layer.msg("保存成功", {icon: 6});
callback(true)
window.location.href = "/blog/"+ data.Data
} else if (data.Status == 401) {
window.location.href = "login"
} else if (data.Status == 500) {

10
views/ublogs.html

@ -11,8 +11,8 @@
<div class="me-blog-list">
<ol class="breadcrumb">
<li>个人中心</li>
<li>博客列表</li>
<li class="active">管理博客</li>
<li>我的博客</li>
<li class="active">博客列表</li>
</ol>
{{range .Page.List}}
<div>
@ -22,7 +22,7 @@
<em>浏览量:{{.Browses}}</em>
<span style="width: 35px;margin-left: 30px;"><a href="javascript:void(0)" style="color: #F84822"
onclick="del(this,{{.Id}})">删除</a></span>
<span style="width: 35px;margin-left: 10px;"><a href="" style="color: #F84822">编辑</a></span>
<span style="width: 35px;margin-left: 10px;"><a href="/blog/edit/{{.Id}}" target="_blank" style="color: #F84822">编辑</a></span>
</div>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>
@ -49,7 +49,9 @@
</nav>
</div>
</div>
{{template "footer"}}
<div class="me-footer">
{{template "footer"}}
</div>
</div>
</div>
</div>

9
views/ulike.html

@ -9,6 +9,11 @@
<div class="me-blog-root">
<div class="me-blog-list">
<ol class="breadcrumb">
<li>个人中心</li>
<li>我的收藏</li>
<li class="active">收藏列表</li>
</ol>
{{range .Page.List}}
<div>
<p><a href="/blog/{{.BlogId}}">{{.Blog.Title}}</a></p>
@ -43,7 +48,9 @@
</nav>
</div>
</div>
{{template "footer"}}
<div class="me-footer">
{{template "footer"}}
</div>
</div>
</div>
</div>

불러오는 중...
취소
저장