Browse Source

fix:删除收藏,笔记等代码

master
燕鹏 3 years ago
parent
commit
6170c13894
  1. 9
      controllers/BlogController.go
  2. 88
      controllers/CommentController.go
  3. 8
      controllers/IndexController.go
  4. 56
      controllers/LikeController.go
  5. 236
      controllers/NoteController.go
  6. 17
      models/Blog.go
  7. 17
      models/Comment.go
  8. 2
      models/DataInit.go
  9. 16
      models/Like.go
  10. 16
      models/Note.go
  11. 13
      models/NoteColl.go
  12. 11
      routers/CommentRouter.go
  13. 11
      routers/LikeRouter.go
  14. 18
      routers/NoteRouter.go
  15. 7
      service/BlogService.go
  16. 83
      service/CommentService.go
  17. 71
      service/LikeService.go
  18. 105
      service/NoteService.go
  19. 34
      utils/EsUtil.go
  20. 10
      views/T.me.tpl
  21. 2
      views/blog.html
  22. 16
      views/index.html
  23. 378
      views/note.html

9
controllers/BlogController.go

@ -134,7 +134,6 @@ func (this *BlogController) Edit() {
func (this *BlogController) Get() { func (this *BlogController) Get() {
blogService := service.BlogService{} blogService := service.BlogService{}
likeService := service.LikeService{}
userService := service.UserService{} userService := service.UserService{}
idStr := this.Ctx.Input.Param(":id") idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64) id, _ := strconv.ParseInt(idStr, 10, 64)
@ -149,12 +148,6 @@ func (this *BlogController) Get() {
if blog.UserId == uid.(int64) { if blog.UserId == uid.(int64) {
this.Data["IsAuthor"] = true this.Data["IsAuthor"] = true
} }
if flag, err := likeService.IsLike(id, uid.(int64)); err == nil {
this.Data["IsLike"] = flag
}
}
if blogs, err := blogService.TopBlogByUser(blog.UserId); err == nil {
this.Data["Top"] = blogs
} }
this.Data["Blog"] = blog this.Data["Blog"] = blog
@ -174,7 +167,6 @@ func (this *BlogController) Get() {
func (this *BlogController) Del() { func (this *BlogController) Del() {
blogService := service.BlogService{} blogService := service.BlogService{}
userService := service.UserService{} userService := service.UserService{}
likeService := service.LikeService{}
uid := this.GetSession("userid") uid := this.GetSession("userid")
if uid == nil { if uid == nil {
this.Data["json"] = models.ReurnError(401, "") this.Data["json"] = models.ReurnError(401, "")
@ -205,7 +197,6 @@ func (this *BlogController) Del() {
this.Data["json"] = models.ReurnSuccess("") this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON() this.ServeJSON()
userService.CountBlog(uid.(int64)) userService.CountBlog(uid.(int64))
likeService.DelLikeByBlog(id)
return return
} }

88
controllers/CommentController.go

@ -1,88 +0,0 @@
package controllers
import (
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)
type CommentController struct {
beego.Controller
}
func (this *CommentController) Save() {
commentService := service.CommentService{}
blogService := service.BlogService{}
userService := service.UserService{}
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 := blogService.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 := commentService.ReadComment(parent); err == nil {
comm.BuserId = parent.CuserId
}
comm.Pid = pid
}
err = commentService.SaveComment(comm)
if err == nil {
this.Data["json"] = models.ReurnData("", comm)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
userService.CountComments(uid.(int64), blogId)
return
}
func (this *CommentController) Del() {
commentService := service.CommentService{}
userService := service.UserService{}
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 := commentService.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 = commentService.DelComment(id)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
userService.CountComments(uid.(int64), id)
return
}

8
controllers/IndexController.go

@ -11,14 +11,10 @@ type IndexController struct {
func (this *IndexController) Get() { func (this *IndexController) Get() {
blogService := service.BlogService{} blogService := service.BlogService{}
timeBlog, _ := blogService.IndexBlogs(12, 0) timeBlog, _ := blogService.IndexBlogs(20, 0)
this.Data["TimeBlog"] = timeBlog this.Data["TimeBlog"] = timeBlog
browsBlog, _ := blogService.IndexBlogs(12, 1) browsBlog, _ := blogService.IndexBlogs(20, 1)
this.Data["BrowsBlog"] = browsBlog this.Data["BrowsBlog"] = browsBlog
likeBlog, _ := blogService.IndexBlogs(12, 2)
this.Data["LikeBlog"] = likeBlog
commentBlog, _ := blogService.IndexBlogs(12, 3)
this.Data["CommBlog"] = commentBlog
this.Data["UserId"] = this.GetSession("userid") this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg") this.Data["HeadImg"] = this.GetSession("headimg")

56
controllers/LikeController.go

@ -1,56 +0,0 @@
package controllers
import (
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)
type LikeController struct {
beego.Controller
}
func (this *LikeController) Save() {
likeService := service.LikeService{}
userService := service.UserService{}
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := likeService.SaveLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
} else {
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
userService.CountLike(uid.(int64), id)
return
}
func (this *LikeController) Delete() {
likeService := service.LikeService{}
userService := service.UserService{}
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := likeService.DelLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
} else {
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
userService.CountLike(uid.(int64), id)
return
}

236
controllers/NoteController.go

@ -1,236 +0,0 @@
package controllers
import (
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)
type NoteController struct {
beego.Controller
}
func (this *NoteController) Save() {
noteService := service.NoteService{}
pid, _ := this.GetInt64("pid")
title := this.GetString("title")
uid := this.GetSession("userid")
noteHtml := this.GetString("noteHtml")
noteVal := this.GetString("noteVal")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "保存失败")
this.ServeJSON()
return
}
note := &models.Note{Title: title, Pid: pid, UserId: uid.(int64), NoteVal: noteVal, NoteHtml: noteHtml}
err := noteService.SaveNote(note)
if err == nil {
this.Data["json"] = note
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) Edit() {
noteService := service.NoteService{}
idStr := this.Ctx.Input.Param(":id")
noteHtml := this.GetString("noteHtml")
noteVal := this.GetString("noteVal")
id, _ := strconv.ParseInt(idStr, 10, 64)
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
note := &models.Note{Id: id}
err1 := noteService.GetNote(note)
if err1 != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
this.ServeJSON()
return
}
if uid != note.UserId {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
note.NoteHtml = noteHtml
note.NoteVal = noteVal
err := noteService.EditNote(note)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) SaveNoteColl() {
noteService := service.NoteService{}
title := this.GetString("title")
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
note := &models.NoteColl{Title: title, UserId: uid.(int64)}
err := noteService.SaveNoteColl(note)
if err == nil {
this.Data["json"] = models.ReurnData("", note)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) EditNoteColl() {
noteService := service.NoteService{}
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 := noteService.EditNoteColl(title, id, uid.(int64))
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) Get() {
noteService := service.NoteService{}
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)
note := &models.Note{Id: id}
err := noteService.GetNote(note)
if err == nil {
this.Data["json"] = note
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
this.ServeJSON()
return
}
func (this *NoteController) DelNoteColl() {
noteService := service.NoteService{}
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 := noteService.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() {
noteService := service.NoteService{}
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)
note := &models.Note{Id: id}
err := noteService.GetNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
return
}
err = noteService.DelNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
return
}
func (this *NoteController) Note() {
noteService := service.NoteService{}
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
noteColls, err := noteService.GetNoteColl(uid.(int64))
if err == nil {
if len(noteColls) > 0 {
for i := 0; i < len(noteColls); i++ {
notes, err1 := noteService.GetNoteByPid(noteColls[i].Id)
if err1 == nil {
noteColls[i].Notes = notes
}
}
}
} else {
noteColls = make([]*models.NoteColl, 0)
}
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["NoteColls"] = noteColls
this.TplName = "note.html"
}
func (this *NoteController) NoteApi() {
noteService := service.NoteService{}
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
noteColls, err := noteService.GetNoteColl(uid.(int64))
if err == nil {
if len(noteColls) > 0 {
for i := 0; i < len(noteColls); i++ {
notes, err1 := noteService.GetNoteByPid(noteColls[i].Id)
if err1 == nil {
noteColls[i].Notes = notes
}
}
}
} else {
noteColls = make([]*models.NoteColl, 0)
}
this.Data["json"] = noteColls
this.ServeJSON()
}

17
models/Blog.go

@ -8,14 +8,12 @@ type Blog struct {
Id int64 Id int64
UserId int64 UserId int64
Title string Title string
BlogValue string `orm:"type(text)"` BlogValue string `orm:"type(text)"`
BlogHtml string `orm:"type(text)"` BlogHtml string `orm:"type(text)"`
BlogDesc string BlogDesc string
Ctime time.Time `orm:"auto_now_add;type(datetime)"` Ctime time.Time `orm:"auto_now_add;type(datetime)"`
Utime time.Time `orm:"auto_now_add;type(datetime)"` Utime time.Time `orm:"auto_now_add;type(datetime)"`
Browses int64 `orm:"default(0)"` Browses int64 `orm:"default(0)"`
Likes int64 `orm:"default(0)"`
Comments int64 `orm:"default(0)"`
Top int `orm:"default(0)"` Top int `orm:"default(0)"`
Hot int `orm:"default(0)"` Hot int `orm:"default(0)"`
Ttime time.Time `orm:"null;type(date)"` Ttime time.Time `orm:"null;type(date)"`
@ -23,10 +21,9 @@ type Blog struct {
Delflag int `orm:"default(0)"` Delflag int `orm:"default(0)"`
CategoryId int64 CategoryId int64
User *User `orm:"-"` User *User `orm:"-"`
UserName string `orm:"-"` UserName string `orm:"-"`
HeadImg string `orm:"-"` HeadImg string `orm:"-"`
CateName string `orm:"-"` CateName string `orm:"-"`
Lables []*NLabel `orm:"-"` Lables []*NLabel `orm:"-"`
Comms []*Comment `orm:"-"`
} }

17
models/Comment.go

@ -1,17 +0,0 @@
package models
import "time"
type Comment struct {
Id 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:"-"`
}

2
models/DataInit.go

@ -12,7 +12,7 @@ func RegistDB() {
fmt.Println(dbUrl) fmt.Println(dbUrl)
orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local") orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local")
orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment)) orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Category))
orm.RunSyncdb("default", false, true) orm.RunSyncdb("default", false, true)
} }

16
models/Like.go

@ -1,16 +0,0 @@
package models
import "time"
type Like struct {
Id int64
UserId int64
BlogId int64
Ltime time.Time `orm:"auto_now_add;type(datetime)"`
Blog *Blog `orm:"-"`
}
func (u *Like) TableName() string {
return "tb_like"
}

16
models/Note.go

@ -1,16 +0,0 @@
package models
import "time"
/**
笔记
*/
type Note struct {
Id int64
UserId int64
Title string
NoteHtml string `orm:"type(text)"`
NoteVal string `orm:"type(text)"`
Pid int64
Utime time.Time `orm:"auto_now_add;type(datetime)"`
}

13
models/NoteColl.go

@ -1,13 +0,0 @@
package models
/**
文章
*/
type NoteColl struct {
Id int64
UserId int64
Title string
Notes []*Note `orm:"-"`
Count int64 `orm:"-"`
}

11
routers/CommentRouter.go

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

11
routers/LikeRouter.go

@ -1,11 +0,0 @@
package routers
import (
"beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
beego.Router("/api/like/:id([0-9]+)", &controllers.LikeController{}, "get:Save")
beego.Router("/api/unlike/:id([0-9]+)", &controllers.LikeController{}, "get:Delete")
}

18
routers/NoteRouter.go

@ -1,18 +0,0 @@
package routers
import (
"beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
beego.Router("/api/note/:id([0-9]+)", &controllers.NoteController{}, "get:Get")
beego.Router("/note", &controllers.NoteController{}, "get:Note")
beego.Router("/api/note", &controllers.NoteController{}, "get:NoteApi")
beego.Router("/api/notecoll/save", &controllers.NoteController{}, "post:SaveNoteColl")
beego.Router("/api/notecoll/edit", &controllers.NoteController{}, "post:EditNoteColl")
beego.Router("/api/note/save", &controllers.NoteController{}, "post:Save")
beego.Router("/api/note/edit/:id([0-9]+)", &controllers.NoteController{}, "post:Edit")
beego.Router("/api/note/del/:id([0-9]+)", &controllers.NoteController{}, "post:Delete")
beego.Router("/api/notecol/del/:id([0-9]+)", &controllers.NoteController{}, "post:DelNoteColl")
}

7
service/BlogService.go

@ -60,7 +60,6 @@ func (this *BlogService) ReadBlog(id int64) (*models.Blog, error) {
} }
func (this *BlogService) GetBlog(id int64) (*models.Blog, error) { func (this *BlogService) GetBlog(id int64) (*models.Blog, error) {
commentService := CommentService{}
o := orm.NewOrm() o := orm.NewOrm()
blog := &models.Blog{Id: id} blog := &models.Blog{Id: id}
err := o.Read(blog) err := o.Read(blog)
@ -78,9 +77,9 @@ func (this *BlogService) GetBlog(id int64) (*models.Blog, error) {
if err == nil { if err == nil {
blog.Lables = labels blog.Lables = labels
} }
comms, berr := commentService.FindCommentByBlog(id) category := &models.Category{Id: blog.CategoryId}
if berr == nil { if err := o.Read(category); err == nil {
blog.Comms = comms blog.CateName = category.Title
} }
return blog, nil return blog, nil
} }

83
service/CommentService.go

@ -1,83 +0,0 @@
package service
import (
"beeblog/models"
"fmt"
"github.com/beego/beego/v2/client/orm"
)
type CommentService struct {
}
func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, error) {
var comms []*models.Comment
o := orm.NewOrm()
_, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).Filter("BlogId", bid).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 (this *CommentService) 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 (this *CommentService) ReadComment(comment *models.Comment) error {
return orm.NewOrm().Read(comment)
}
func (this *CommentService) 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
}

71
service/LikeService.go

@ -1,71 +0,0 @@
package service
import (
"beeblog/models"
"beeblog/utils"
"fmt"
"github.com/beego/beego/v2/client/orm"
)
type LikeService struct {
}
func (this *LikeService) SaveLike(like *models.Like) (int64, error) {
return orm.NewOrm().Insert(like)
}
func (this *LikeService) DelLike(like *models.Like) (int64, error) {
return orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", like.BlogId).Filter("UserId", like.UserId).Delete()
}
func (this *LikeService) IsLike(bid int64, uid int64) (bool, error) {
totalCount, err := orm.NewOrm().QueryTable(&models.Like{}).Filter("BlogId", bid).Filter("UserId", uid).Count()
if err == nil {
fmt.Println(totalCount, "like count")
if totalCount > 0 {
return true, nil
} else {
return false, nil
}
} else {
fmt.Println(err)
return false, err
}
}
func (this *LikeService) MeLikes(num int, size int, uid int64) (*utils.Page, error) {
page, err := this.countLike(num, size, uid)
if err != nil {
return nil, err
}
var likes []*models.Like
o := orm.NewOrm()
qs := o.QueryTable(&models.Like{})
qs = qs.Filter("UserId", uid)
qs = qs.Limit(size, (page.PageNo-1)*size)
if _, err = qs.All(&likes); err != nil {
return nil, err
}
if len(likes) > 0 {
for i := 0; i < len(likes); i++ {
blog := &models.Blog{Id: likes[i].BlogId}
if err := o.Read(blog); err == nil {
likes[i].Blog = blog
}
}
}
page.List = likes
return page, nil
}
func (this *LikeService) countLike(num int, size int, uid int64) (*utils.Page, error) {
o := orm.NewOrm()
qs := o.QueryTable(&models.Like{})
totalCount, err := qs.Filter("UserId", uid).Count()
if err != nil {
return nil, err
}
return utils.PageUtil(totalCount, num, size), nil
}
func (likeService LikeService) DelLikeByBlog(id int64) {
orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", id).Delete()
}

105
service/NoteService.go

@ -1,105 +0,0 @@
package service
import (
"beeblog/models"
"errors"
"github.com/beego/beego/v2/client/orm"
)
type NoteService struct {
}
func (this *NoteService) EditNote(note *models.Note) error {
o := orm.NewOrm()
id, err := o.Update(note)
if err == nil {
note.Id = id
}
return err
}
func (this *NoteService) SaveNote(note *models.Note) error {
o := orm.NewOrm()
id, err := o.Insert(note)
if err == nil {
note.Id = id
}
return err
}
func (this *NoteService) GetNote(note *models.Note) error {
o := orm.NewOrm()
return o.Read(note)
}
func (this *NoteService) DelNote(note *models.Note) error {
o := orm.NewOrm()
_, err := o.Delete(note)
return err
}
func (this *NoteService) GetNoteByPid(pid int64) ([]*models.Note, error) {
var notes []*models.Note
o := orm.NewOrm()
qs := o.QueryTable(models.Note{})
_, err := qs.Filter("Pid", pid).All(&notes)
return notes, err
}
func (this *NoteService) CountNote(pid int64) (int64, error) {
o := orm.NewOrm()
totalCount, err := o.QueryTable(&models.Note{}).Filter("Pid", pid).Count()
if err != nil {
return 0, err
}
return totalCount, nil
}
func (this *NoteService) SaveNoteColl(note *models.NoteColl) error {
o := orm.NewOrm()
id, err := o.Insert(note)
if err == nil {
note.Id = id
}
return err
}
func (this *NoteService) 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 (this *NoteService) GetNoteColl(uid int64) ([]*models.NoteColl, error) {
var notes []*models.NoteColl
o := orm.NewOrm()
qs := o.QueryTable(models.NoteColl{})
_, err := qs.Filter("UserId", uid).All(&notes)
return notes, err
}
func (this *NoteService) DelNoteColl(id int64, uid int64) error {
o := orm.NewOrm()
noteColl := &models.NoteColl{Id: id}
if err := o.Read(noteColl); err != nil {
return err
}
if uid != noteColl.UserId {
return errors.New("403")
}
_, err := o.QueryTable(models.Note{}).Filter("Pid", id).Delete()
if err == nil {
_, err = o.Delete(noteColl)
}
return err
}

34
utils/EsUtil.go

@ -4,13 +4,9 @@ import (
"beeblog/models" "beeblog/models"
"context" "context"
"fmt" "fmt"
beego "github.com/beego/beego/v2/server/web"
"github.com/olivere/elastic" "github.com/olivere/elastic"
"log"
"os"
"reflect" "reflect"
"strconv" "strconv"
"time"
) )
var client *elastic.Client var client *elastic.Client
@ -48,21 +44,21 @@ const mapping = `
} }
}` }`
func init() { //func init() {
host, _ := beego.AppConfig.String("eshost") // host, _ := beego.AppConfig.String("eshost")
var err error // var err error
client, err = elastic.NewClient( // client, err = elastic.NewClient(
elastic.SetURL(host), // elastic.SetURL(host),
elastic.SetSniff(false), // elastic.SetSniff(false),
elastic.SetHealthcheckInterval(10*time.Second), // elastic.SetHealthcheckInterval(10*time.Second),
elastic.SetGzip(true), // elastic.SetGzip(true),
elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)), // elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags))) // elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
//
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
} //}
func Index() { func Index() {
ctx := context.Background() ctx := context.Background()

10
views/T.me.tpl

@ -15,16 +15,6 @@
<span>{{.User.BlogBrowes}}</span> <span>{{.User.BlogBrowes}}</span>
</div> </div>
</div> </div>
<div style="display: flex;height: 30px;line-height: 30px;">
<div style="display: inline-block;width: 100px">
<span>评论: </span>
<span>{{.User.BlogComment}}</span>
</div>
<div style="display: inline-block;flex: 1">
<span>喜欢: </span>
<span>{{.User.BlogLike}}</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;"/>
<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 .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>

2
views/blog.html

@ -19,7 +19,7 @@
</div> </div>
<div class="author-info"> <div class="author-info">
<a href="/u/{{.Blog.User.Id}}">{{.Blog.User.NickName}}</a><br> <a href="/u/{{.Blog.User.Id}}">{{.Blog.User.NickName}}</a><br>
<span>{{.Blog.Ctime.Format "2006-01-02"}}</span><span>阅读 {{.Blog.Browses}}</span> <span>{{.Blog.Ctime.Format "2006-01-02"}}</span><span class="catelog-name"> {{.Blog.CateName}} </span><span>阅读 {{.Blog.Browses}}</span>
</div> </div>
<div class="blog-label"> <div class="blog-label">
{{range .Blog.Lables}} {{range .Blog.Lables}}

16
views/index.html

@ -25,22 +25,6 @@
<p><a href="https://www.aiprose.com/blogs">查看更多...</a></p> <p><a href="https://www.aiprose.com/blogs">查看更多...</a></p>
</div> </div>
</div> </div>
<div style="display: flex">
<div style="flex: 1">
<h5>收藏排行</h5>
{{range .LikeBlog}}
<p><a href="/blog/{{.Id}}" target="_blank">{{.Title}}</a></p>
{{end}}
<p><a href="https://www.aiprose.com/blogs">查看更多...</a></p>
</div>
<div style="flex: 1">
<h5>人气最旺</h5>
{{range .CommBlog}}
<p><a href="/blog/{{.Id}}" target="_blank">{{.Title}}</a></p>
{{end}}
<p><a href="https://www.aiprose.com/blogs">查看更多...</a></p>
</div>
</div>
</div> </div>
<div class="footer-container"> <div class="footer-container">
{{template "footer"}} {{template "footer"}}

378
views/note.html

@ -1,378 +0,0 @@
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description"
content="码农随笔,个人随笔是一个面向IT技术人员,提供个人平时工作总结和在线记录学习笔记,个人技术博客,在线云笔记,码农笔录,最新的技术博客,www.aiprose.com">
<meta name="keywords" content="码农随笔,个人随笔,博客,个人博客,个人笔记,技术博客,免费云笔记,云笔记,随笔,IT博客,谷歌地图,码农笔录,aiprose">
<title>我的笔记 - 码农随笔</title>
<script type="text/javascript" src="/static/js/vue.min.js"></script>
<link rel="stylesheet" href="/static/css/iview.css">
<script src="/static/js/iview.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.4.0/jquery.min.js"></script>
<link rel="stylesheet" href="/static/editor/css/index.css">
<script src="/static/editor/mavon-editor.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<style>
html, body {
height: 100%;
min-height: 100%;
}
main {
margin-top: 0 !important;
margin-bottom: 0 !important;
background-color: #f5f4e9;
}
#vue-app {
height: 100%;
}
.v-note-wrapper {
height: 100%;
z-index: 99 !important;
}
.v-note-wrapper .v-note-op {
padding-left: 40px !important;
}
.ivu-menu-png {
z-index: 1000;
position: fixed;
left: 10px;
top: 5px;
}
.ivu-drawer-body {
scrollbar-width: none; /* firefox */
-ms-overflow-style: none; /* IE 10+ */
overflow-x: hidden;
overflow-y: auto;
}
::-webkit-scrollbar {
display: none; /* Chrome Safari */
}
</style>
</head>
<body>
<div id="vue-app">
<div class="ivu-menu-png" @click="drawer = true">
<img src="/static/img/imenu.png" alt="" width="30" height="30">
</div>
<Modal
v-model="modal1"
title="笔记信息"
:mask-closable="false"
@on-ok="okHandler"
@on-cancel="cancelHandler">
<i-form :label-width="100">
<form-item label="文件夹名称">
<i-select v-model="currentNote.Pid">
<i-option v-for="item in noteCols" :value="item.id" :key="item.id">${ item.title }</i-option>
</i-select>
</form-item>
<form-item label="笔记名称">
<i-input type="text" v-model="currentNote.Title" placeholder="请输入笔记名称"/>
</form-item>
</i-form>
</Modal>
<Modal
v-model="modal2"
title="新增文件夹"
:mask-closable="false"
@on-ok="okColHandler">
<i-form :label-width="100">
<form-item label="文件夹名称">
<i-input type="text" v-model="colTitle" placeholder="请输入文件夹名称"/>
</form-item>
</i-form>
</Modal>
<mavon-editor :externalLink="externalLink" :toolbars="toolbars" v-model="meval" @imgAdd="$imgAdd" @save="saveHandler"
@change="changeHandler" ref=md></mavon-editor>
<Drawer title="目录选择" placement="left" :closable="false" v-model="drawer" width="400">
<i-button type="primary" ghost long @click="newNoteColHandler">新建文件夹</i-button>
<i-button type="success" ghost long style="margin-top: 5px" @click="newNoteHandler">新建笔记</i-button>
<i-menu theme="light" accordion="true" width="380" @on-select="noteSelectHandler">
<Submenu :name="item.Id" v-for="(item,key) in notes" :key="key">
<template slot="title">
<i class="ivu-icon ivu-icon-ios-paper"></i>
${item.Title}
</template>
<menu-item :name="note.Id" v-for="(note,nkey) in item.Notes" :key="nkey">${note.Title}
<Icon type="md-trash" style="float: right" @click="delHandler(note.Id)"/>
</menu-item>
</Submenu>
</i-menu>
</Drawer>
</div>
</body>
<script>
var app = new Vue({
delimiters: ['${', '}'],
el: '#vue-app',
components: {
'mavon-editor': MavonEditor.mavonEditor
},
data: {
modal2: false,
modal1: false,
drawer: false,
colTitle: '',
externalLink: {
// markdown_css: function() {
// // 这是你的markdown css文件路径
// return '/markdown/github-markdown.min.css';
// },
katex_css: function () {
// 这是你的katex配色方案路径路径
return '/katex/katex.min.css';
},
katex_js: function () {
// 这是你的katex.js路径
return '/katex/katex.min.js';
},
},
toolbars: {
bold: true, // 粗体
italic: true, // 斜体
header: true, // 标题
underline: true, // 下划线
strikethrough: true, // 中划线
mark: true, // 标记
superscript: true, // 上角标
subscript: true, // 下角标
quote: true, // 引用
ol: true, // 有序列表
ul: true, // 无序列表
link: true, // 链接
imagelink: true, // 图片链接
code: true, // code
table: true, // 表格
//fullscreen: true, // 全屏编辑
readmodel: true, // 沉浸式阅读
htmlcode: true, // 展示html源码
help: true, // 帮助
/* 1.3.5 */
undo: true, // 上一步
//redo: true, // 下一步
trash: true, // 清空
save: true, // 保存(触发events中的save事件)
/* 1.4.2 */
navigation: true, // 导航目录
/* 2.1.8 */
alignleft: true, // 左对齐
aligncenter: true, // 居中
alignright: true, // 右对齐
/* 2.2.1 */
subfield: true, // 单双栏模式
preview: true, // 预览
menu: true, // 预览
},
notes: [],
currentNote: {},
meval: '',
noteCols: []
},
mounted: function () {
var _this = this
var noteCahche = window.localStorage.getItem("note")
if (noteCahche) {
this.currentNote = JSON.parse(noteCahche)
this.meval = this.currentNote.NoteVal
}
this.getData()
// $vm.$emit('imgAdd', pos, $file);
this.$refs.md.$on("imgAdd",(pos, $file)=>{
_this.$imgAdd(pos, $file)
})
},
methods: {
$imgAdd(pos, $file){
debugger
var _this = this
// 第一步.将图片上传到服务器.
var formdata = new FormData();
formdata.append('image', $file);
axios({
url: '/file/upload',
method: 'post',
data: formdata,
headers: { 'Content-Type': 'multipart/form-data' },
}).then((resp) => {
_this.$refs.md.$img2Url(pos, resp.data.Data);
})
},
delHandler (id){
var _this = this
if(!id){
return
}
$.post('/api/note/del/' + id,
function (data) {
if (data.Status == 0) {
this.currentNote = {}
this.meval = ''
_this.getData()
_this.$Notice.success({
title: '删除成功',
desc: ''
})
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
this.$Notice.warning({
title: '暂无权限',
desc: ''
})
} else {
_this.$Notice.error({
title: '删除失败',
desc: ''
})
}
}, 'json')
},
newNoteColHandler(){
this.modal2 = true
},
getData(){
var _this = this
$.get("/api/note", function (data, status) {
_this.notes = data
data.forEach((el) => {
_this.noteCols.push({title: el.Title, id: el.Id})
})
})
},
okColHandler() {
var _this = this
if(!this.colTitle){
return
}
$.post('/api/notecoll/save', {
title: this.colTitle
},
function (data) {
if (data.Status == 500) {
_this.$Notice.error({
title: '保存失败',
desc: ''
})
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
_this.getData()
_this.$Notice.success({
title: '保存成功',
desc: ''
})
}
}, 'json')
},
changeHandler: function (value, render) {
this.currentNote.NoteVal = value
this.currentNote.NoteHtml = render
var jsonStr = JSON.stringify(this.currentNote);
window.localStorage.setItem("note", jsonStr)
},
newNoteHandler() {
this.currentNote = {}
this.meval = ''
},
okHandler() {
var _this = this
if (!this.currentNote.Pid) {
this.$Notice.warning({
title: '请选择文件夹后重新保存',
desc: ''
})
return
}
if (!this.currentNote.Title) {
this.$Notice.warning({
title: '请输入笔记名称后重新保存',
desc: ''
})
return
}
$.post('/api/note/save/', {
pid: this.currentNote.Pid,
title: this.currentNote.Title,
noteHtml: this.currentNote.NoteHtml,
noteVal: this.currentNote.NoteVal
},
function (data) {
if (data.Status == 500) {
_this.$Notice.error({
title: '保存失败',
desc: ''
})
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
_this.currentNote.Id = data.Id
_this.$Notice.success({
title: '保存成功',
desc: ''
})
}
}, 'json')
},
cancelHandler() {
this.$Notice.warning({
title: '笔记没有被保存!!!',
desc: ''
})
},
noteSelectHandler(id) {
var _this = this
this.notes.forEach((item) => {
item.Notes.forEach((note) => {
if (id == note.Id) {
_this.currentNote = note
_this.meval = note.NoteVal ? note.NoteVal : note.NoteHtml
}
})
})
},
saveHandler(value, render) {
this.currentNote.NoteVal = value
this.currentNote.NoteHtml = render
if (!this.currentNote.Id) {
this.modal1 = true
} else {
this.editHandler(value, render)
}
},
editHandler(value, render) {
var _this = this
$.post('/api/note/edit/' + this.currentNote.Id, {
noteHtml: render,
noteVal: value
},
function (data) {
if (data.Status == 0) {
_this.$Notice.success({
title: '保存成功',
desc: ''
})
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
_this.$Notice.error({
title: '保存失败',
desc: ''
})
}
}, 'json')
}
}
})
</script>
</html>
Loading…
Cancel
Save