Browse Source

个人中心博客管理&笔记管理

layui
yirenyishi 6 years ago
parent
commit
713b747eb8
  1. 33
      controllers/BlogController.go
  2. 40
      controllers/NoteController.go
  3. 4
      controllers/UserController.go
  4. BIN
      data/beeblog.db
  5. 1
      routers/BlogRouter.go
  6. 2
      routers/NoteRouter.go
  7. 6
      service/BlogService.go
  8. 36
      service/NoteService.go
  9. 2
      static/css/me.css
  10. 2
      views/T.me.tpl
  11. 27
      views/note.html
  12. 28
      views/ublogs.html
  13. 81
      views/unote.html

33
controllers/BlogController.go

@ -53,6 +53,39 @@ func (this *BlogController) Get() {
return return
} }
func (this *BlogController) 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)
blog, err := service.GetBlog(id)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
if blog.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(503, "")
this.ServeJSON()
return
}
blog.Delflag = 1
err = service.DelBlog(blog)
if err!=nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
func (this *BlogController) New() { func (this *BlogController) New() {
uid := this.GetSession("userid") uid := this.GetSession("userid")
if uid == nil { if uid == nil {

40
controllers/NoteController.go

@ -74,7 +74,26 @@ func (this *NoteController) SaveNoteColl() {
note := &models.NoteColl{Title: title, UserId: uid.(int64)} note := &models.NoteColl{Title: title, UserId: uid.(int64)}
err := service.SaveNoteColl(note) err := service.SaveNoteColl(note)
if err == nil { if err == nil {
this.Data["json"] = note this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) EditNoteColl() {
title := this.GetString("title")
id, _ := this.GetInt64("id")
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
err := service.EditNoteColl(title, id, uid.(int64))
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else { } else {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
} }
@ -104,6 +123,25 @@ func (this *NoteController) Get() {
this.ServeJSON() this.ServeJSON()
return return
} }
func (this *NoteController) DelNoteColl() {
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)
err := service.DelNoteColl(id, uid.(int64))
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
} else {
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
return
}
func (this *NoteController) Delete() { func (this *NoteController) Delete() {
uid := this.GetSession("userid") uid := this.GetSession("userid")
if uid == nil { if uid == nil {

4
controllers/UserController.go

@ -48,6 +48,8 @@ func (this *UserController) PersonBlog() {
return return
} }
} }
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = page this.Data["Page"] = page
this.Data["IsMeBlog"] = true this.Data["IsMeBlog"] = true
this.Data["Flag"] = 0 this.Data["Flag"] = 0
@ -85,6 +87,8 @@ func (this *UserController) PersonNote() {
return return
} }
} }
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Note"] = notColl this.Data["Note"] = notColl
this.Data["IsMeNote"] = true this.Data["IsMeNote"] = true
this.Data["User"] = user this.Data["User"] = user

BIN
data/beeblog.db

Binary file not shown.

1
routers/BlogRouter.go

@ -8,6 +8,7 @@ import (
func init() { func init() {
beego.Router("/blog/new", &controllers.BlogController{}, "post:Save") beego.Router("/blog/new", &controllers.BlogController{}, "post:Save")
beego.Router("/blog/:id([0-9]+)", &controllers.BlogController{}, "get:Get") 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") beego.Router("/blogs", &controllers.BlogController{}, "get:BlogsPage")
beego.Router("/blog/new", &controllers.BlogController{}, "get:New") beego.Router("/blog/new", &controllers.BlogController{}, "get:New")
beego.Router("/blog1", &controllers.BlogController{}, "get:Blog1") beego.Router("/blog1", &controllers.BlogController{}, "get:Blog1")

2
routers/NoteRouter.go

@ -9,7 +9,9 @@ func init() {
beego.Router("/note/:id([0-9]+)", &controllers.NoteController{}, "get:Get") beego.Router("/note/:id([0-9]+)", &controllers.NoteController{}, "get:Get")
beego.Router("/note", &controllers.NoteController{}, "get:Note") beego.Router("/note", &controllers.NoteController{}, "get:Note")
beego.Router("/notecoll/save", &controllers.NoteController{}, "post:SaveNoteColl") beego.Router("/notecoll/save", &controllers.NoteController{}, "post:SaveNoteColl")
beego.Router("/notecoll/edit", &controllers.NoteController{}, "post:EditNoteColl")
beego.Router("/note/save", &controllers.NoteController{}, "post:Save") beego.Router("/note/save", &controllers.NoteController{}, "post:Save")
beego.Router("/note/edit/:id([0-9]+)", &controllers.NoteController{}, "post:Edit") beego.Router("/note/edit/:id([0-9]+)", &controllers.NoteController{}, "post:Edit")
beego.Router("/note/del/:id([0-9]+)", &controllers.NoteController{}, "post:Delete") beego.Router("/note/del/:id([0-9]+)", &controllers.NoteController{}, "post:Delete")
beego.Router("/notecol/del/:id([0-9]+)", &controllers.NoteController{}, "post:DelNoteColl")
} }

6
service/BlogService.go

@ -64,6 +64,12 @@ func GetBlog(id int64) (*models.Blog, error) {
return blog, nil return blog, nil
} }
func DelBlog(blog *models.Blog) error {
o := orm.NewOrm()
_, err := o.Update(blog, "Delflag")
return err
}
func FindBlogs(num int, size int, cat int64, flag int) (*utils.Page, error) { func FindBlogs(num int, size int, cat int64, flag int) (*utils.Page, error) {
page, err := count(num, size, cat) page, err := count(num, size, cat)
if err != nil { if err != nil {

36
service/NoteService.go

@ -3,6 +3,8 @@ package service
import ( import (
"beeblog/models" "beeblog/models"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"errors"
"fmt"
) )
type NoteService struct { type NoteService struct {
@ -63,6 +65,21 @@ func SaveNoteColl(note *models.NoteColl) error {
return err return err
} }
func EditNoteColl(title string, id int64, uid int64) error {
o := orm.NewOrm()
noteColl := &models.NoteColl{Id: id}
if err := o.Read(noteColl); err != nil {
return err
}
if noteColl.UserId != uid {
return errors.New("403")
}
noteColl.Title = title
_, err := o.Update(noteColl, "Title")
return err
}
func GetNoteColl(uid int64) ([]*models.NoteColl, error) { func GetNoteColl(uid int64) ([]*models.NoteColl, error) {
var notes []*models.NoteColl var notes []*models.NoteColl
o := orm.NewOrm() o := orm.NewOrm()
@ -70,3 +87,22 @@ func GetNoteColl(uid int64) ([]*models.NoteColl, error) {
_, err := qs.Filter("UserId", uid).All(&notes) _, err := qs.Filter("UserId", uid).All(&notes)
return notes, err return notes, err
} }
func DelNoteColl(id int64, uid int64) error {
o := orm.NewOrm()
noteColl := &models.NoteColl{Id: id}
if err := o.Read(noteColl); err != nil {
fmt.Println(err)
return err
}
if uid != noteColl.UserId {
return errors.New("403")
}
_, err := o.QueryTable(models.Note{}).Filter("Pid", id).Delete()
fmt.Println(err)
if err == nil {
_, err = o.Delete(noteColl)
}
return err
}

2
static/css/me.css

@ -9,7 +9,7 @@
.me-menu p { .me-menu p {
cursor: pointer; cursor: pointer;
font-size: 18px; font-size: 15px;
color: #555; color: #555;
margin: 0; margin: 0;
height: 32px; height: 32px;

2
views/T.me.tpl

@ -27,7 +27,7 @@
</div> </div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/> <hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>
<p {{if .IsMeBlog}}class="active"{{end}}><span></span><a href="/me/blog">我的博客</a></p> <p {{if .IsMeBlog}}class="active"{{end}}><span></span><a href="/me/blog">我的博客</a></p>
<p {{if .IsMeNote}}class="active"{{end}}><span></span><a href="/me/note">我的笔记</a></p> <p {{if .IsMeNote}}class="active"{{end}}><span></span><a href="/me/note">笔记文件夹</a></p>
<p {{if .IsMeLike}}class="active"{{end}}><span></span><a href="/me/like">我的收藏</a></p> <p {{if .IsMeLike}}class="active"{{end}}><span></span><a href="/me/like">我的收藏</a></p>
<p {{if .IsMeInfo}}class="active"{{end}}><span></span><a href="/me/info">我的资料</a></p> <p {{if .IsMeInfo}}class="active"{{end}}><span></span><a href="/me/info">我的资料</a></p>
</div> </div>

27
views/note.html

@ -48,7 +48,7 @@
<nav> <nav>
<a href="/" target="_blank">首页</a> <a href="/" target="_blank">首页</a>
<a href="javascript:void(0)" id="newNote">新增笔记</a> <a href="javascript:void(0)" id="newNote">新增笔记</a>
<a href="javascript:void(0)" id="newNoteColl">管理文件夹</a> <a href="/me/note" id="newNoteColl">管理文件夹</a>
<a href="javascript:void(0)" id="delNote">删除笔记</a> <a href="javascript:void(0)" id="delNote">删除笔记</a>
<a href="javascript:void(0)">个人中心</a> <a href="javascript:void(0)">个人中心</a>
</nav> </nav>
@ -216,31 +216,6 @@
}); });
}) })
$("#newNoteColl").click(function () {
layer.prompt({title: '请输入文件夹名称'}, function (val, index) {
if (val.trim().length < 3) {
layer.msg('文件夹名称最低三个字哦', function () {
});
layer.close(index);
return
}
$.post('/notecoll/save', {
title: val.trim()
},
function (data) {
if (!data.Status) {
parent.layer.msg("保存成功", {icon: 6});
var child = '<li class="item-has-children">\n' +
' <a href="javascript:void(0)">' + data.Titled + '</a>\n' +
' <ul class="sub-menu" value="' + data.Id + '"></ul>\n' +
' </li> '
$(".cd-navigation").append(child)
layer.close(index);
}
}, 'json')
});
})
}) })
</script> </script>
</html> </html>

28
views/ublogs.html

@ -15,13 +15,16 @@
<li class="active">管理博客</li> <li class="active">管理博客</li>
</ol> </ol>
{{range .Page.List}} {{range .Page.List}}
<div>
<p><a href="/blog/{{.Id}}">{{.Title}}</a></p> <p><a href="/blog/{{.Id}}">{{.Title}}</a></p>
<div> <div>
<em>发布时间:{{.Ctime.Format "2006/01/02"}}</em> <em>发布时间:{{.Ctime.Format "2006/01/02"}}</em>
<em>浏览量:{{.Browses}}</em> <em>浏览量:{{.Browses}}</em>
<span style="width: 35px;margin-left: 30px;"><a href="" style="color: #F84822">删除</a></span> <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="" style="color: #F84822">编辑</a></span>
</div> </div>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/> <hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>
{{end}} {{end}}
<div style="text-align: center"> <div style="text-align: center">
@ -51,4 +54,27 @@
</div> </div>
</div> </div>
</body> </body>
<script>
function del(obj, id) {
layer.confirm('删除后将无法恢复', {
btn: ['确认', '手滑了'] //按钮
}, function () {
$.post('/blog/del/' + id,
function (data) {
if (data.Status == 0) {
$(obj).parent().parent().parent().remove()
layer.msg("删除成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}, function () {
});
}
</script>
</html> </html>

81
views/unote.html

@ -13,6 +13,7 @@
<li>个人中心</li> <li>个人中心</li>
<li>笔记文件夹</li> <li>笔记文件夹</li>
<li class="active">管理文件夹</li> <li class="active">管理文件夹</li>
<button type="button" class="btn btn-primary" style="margin-left: 30px" id="newBtn">新增</button>
</ol> </ol>
</div> </div>
@ -20,8 +21,10 @@
<div class="me-blog-list"> <div class="me-blog-list">
<div style="display: flex"> <div style="display: flex">
<p style="flex:1">{{.Title}}<span class="badge" style="margin-left: 3px">{{.Count}}</span></p> <p style="flex:1">{{.Title}}<span class="badge" style="margin-left: 3px">{{.Count}}</span></p>
<span style="width: 35px;"><a href="" style="color: #F84822">删除</a></span> <span style="width: 35px;"><a href="javascript:void(0)" style="color: #F84822"
<span style="width: 35px;margin-left: 10px;"><a href="" style="color: #F84822">编辑</a></span> onclick="del(this,{{.Id}})">删除</a></span>
<span style="width: 42px;margin-left: 10px;"><a href="javascript:void(0)" style="color: #F84822"
onclick="rename(this,{{.Id}},{{.Title}})">重命名</a></span>
</div> </div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/> <hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>
</div> </div>
@ -34,4 +37,78 @@
</div> </div>
</div> </div>
</body> </body>
<script>
function rename(obj, id, name) {
layer.prompt({title: '请输入文件夹名称',value: name}, function (val, index) {
if (val.trim().length < 3) {
layer.msg('文件夹名称最低三个字哦', function () {
});
layer.close(index);
return
}
$.post('/notecoll/edit', {
title: val.trim(),
id: id
},
function (data) {
if (data.Status == 0) {
layer.close(index);
layer.msg("修改成功", {icon: 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});
}
}, 'json')
});
}
function del(obj, id) {
layer.confirm('文件夹下的所有笔记都将会删除', {
btn: ['确认', '手滑了'] //按钮
}, function () {
$.post('/notecol/del/' + id,
function (data) {
if (data.Status == 0) {
$(obj).parent().parent().remove()
layer.msg("删除成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}, function () {
});
}
$(function () {
$("#newBtn").click(function () {
layer.prompt({title: '请输入文件夹名称'}, function (val, index) {
if (val.trim().length < 3) {
layer.msg('文件夹名称最低三个字哦', function () {
});
layer.close(index);
return
}
$.post('/notecoll/save', {
title: val.trim()
},
function (data) {
if (data.Status == 0) {
layer.close(index);
parent.layer.msg("保存成功", {icon: 6});
window.location.href=window.location.href
}
}, 'json')
});
})
})
</script>
</html> </html>
Loading…
Cancel
Save