码农笔录博客源码
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.

253 lines
6.1 KiB

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