码农笔录博客源码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

275 lines
6.6 KiB

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