码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
6.2 KiB

package controllers
import (
"beeblog/models"
"beeblog/service"
"beeblog/utils"
6 years ago
"github.com/astaxie/beego"
"strconv"
6 years ago
"time"
)
type BlogController struct {
beego.Controller
}
6 years ago
func (this *BlogController) EditPage() {
6 years ago
blogService := service.BlogService{}
6 years ago
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 years ago
blog, err := blogService.GetBlog(id)
6 years ago
if err != nil {
6 years ago
this.Redirect("/404", 302)
6 years ago
return
}
if blog.UserId != uid.(int64) {
this.Redirect("/403", 302)
return
}
this.Data["Blog"] = blog
this.TplName = "editblog.html"
}
func (this *BlogController) Save() {
6 years ago
blogService := service.BlogService{}
userService := service.UserService{}
uid := this.GetSession("userid")
6 years ago
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
6 years ago
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
6 years ago
blogDesc := this.GetString("blogDesc")
6 years ago
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
6 years ago
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64), BlogDesc: blogDesc}
6 years ago
err := blogService.SaveBlog(blog, labels)
6 years ago
if err == nil {
6 years ago
this.Data["json"] = models.ReurnData("", blog.Id)
6 years ago
blog.User.Salt = ""
blog.User.UserPwd = ""
utils.ESSave(blog)
6 years ago
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 years ago
userService.CountBlog(uid.(int64))
6 years ago
return
}
func (this *BlogController) Edit() {
6 years ago
blogService := service.BlogService{}
userService := service.UserService{}
6 years ago
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
6 years ago
id, _ := this.GetInt64("id")
6 years ago
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
6 years ago
blogDesc := this.GetString("blogDesc")
6 years ago
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
6 years ago
blog, err := blogService.GetBlog(id)
6 years ago
if err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
this.ServeJSON()
return
}
blog.Title = title
blog.BlogHtml = blogHtml
6 years ago
blog.BlogDesc = blogDesc
6 years ago
blog.CategoryId = catoryId
blog.Utime = time.Now()
6 years ago
err = blogService.EditBlog(blog, labels)
6 years ago
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
6 years ago
blog.User.Salt = ""
blog.User.UserPwd = ""
utils.ESSave(blog)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 years ago
userService.CountBlog(uid.(int64))
6 years ago
return
}
func (this *BlogController) Get() {
6 years ago
blogService := service.BlogService{}
likeService := service.LikeService{}
userService := service.UserService{}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 years ago
blog, err := blogService.GetBlog(id)
blog.User.Salt = ""
blog.User.UserPwd = ""
if err != nil {
6 years ago
this.Redirect("/404", 302)
return
}
6 years ago
if uid := this.GetSession("userid"); uid != nil {
6 years ago
if blog.UserId == uid.(int64) {
this.Data["IsAuthor"] = true
}
6 years ago
if flag, err := likeService.IsLike(id, uid.(int64)); err == nil {
6 years ago
this.Data["IsLike"] = flag
}
}
6 years ago
if blogs, err := blogService.TopBlogByUser(blog.UserId); err == nil {
6 years ago
this.Data["Top"] = blogs
}
5 years ago
this.Data["Blog"] = blog
6 years ago
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
6 years ago
this.Data["IsDDesc"] = true
this.TplName = "blog.html"
if this.Ctx.Input.GetData("refresh") != true {
6 years ago
userService.CountBrows(blog.UserId)
blogService.EditBlogBrows(id)
}
return
}
func (this *BlogController) Del() {
6 years ago
blogService := service.BlogService{}
userService := service.UserService{}
6 years ago
likeService := service.LikeService{}
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
idStr := this.Ctx.Input.Param(":id")
6 years ago
utils.ESDelete(idStr)
id, _ := strconv.ParseInt(idStr, 10, 64)
6 years ago
blog, err := blogService.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
6 years ago
err = blogService.DelBlog(blog)
6 years ago
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
6 years ago
userService.CountBlog(uid.(int64))
6 years ago
likeService.DelLikeByBlog(id)
return
}
6 years ago
func (this *BlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
6 years ago
this.Redirect("/login", 302)
return
}
6 years ago
this.TplName = "newblog.html"
6 years ago
}
6 years ago
func (this *BlogController) BlogsPage() {
6 years ago
catService := service.CategoryService{}
blogService := service.BlogService{}
cats, errcat := catService.GetCats()
if errcat != nil {
6 years ago
this.Redirect("/404", 302)
return
}
num, _ := this.GetInt("num")
size, _ := this.GetInt("size")
cat, _ := this.GetInt64("cat")
flag, _ := this.GetInt("flag")
if num <= 0 {
num = 1
}
6 years ago
if size < 15 {
size = 15
}
if cat <= 0 {
cat = -1
}
6 years ago
pages, err := blogService.FindBlogs(num, size, cat, flag)
if err != nil {
6 years ago
this.Redirect("/404", 302)
return
}
6 years ago
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
6 years ago
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = pages
this.Data["Cats"] = cats
this.Data["Cat"] = cat
this.Data["Flag"] = flag
6 years ago
this.Data["IsBlog"] = true
this.TplName = "blogs.html"
}
6 years ago
6 years ago
func (this *BlogController) Search() {
blog, _ := utils.Search("elk")
6 years ago
this.Data["json"] = models.ReurnData("", blog)
6 years ago
this.ServeJSON()
}
5 years ago
func (this *BlogController) SyncBlog() {
utils.Index()
blogService := service.BlogService{}
list, err := blogService.AllBlogs()
var i = 0
if err != nil {
this.Data["json"] = models.ReurnError(500,"");
this.ServeJSON()
}
for index := range list {
utils.ESSave(list[index])
i++
}
this.Data["json"] = models.ReurnData("",i)
this.ServeJSON()
}