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

186 lines
4.3 KiB

před 6 roky
package mcontrollers
import (
"beeblog/models"
"beeblog/service"
před 3 roky
beego "github.com/beego/beego/v2/server/web"
před 6 roky
"strconv"
"time"
)
type MBlogController struct {
beego.Controller
}
před 6 roky
func (this *MBlogController) Get() {
před 6 roky
blogService := service.BlogService{}
userService := service.UserService{}
před 6 roky
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
před 6 roky
blog, err := blogService.GetBlog(id)
před 6 roky
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
před 3 roky
this.Data["json"] = models.ReurnData("", blog)
před 6 roky
this.ServeJSON()
před 6 roky
userService.CountBrows(blog.UserId)
blogService.EditBlogBrows(id)
před 6 roky
return
}
před 6 roky
func (this *MBlogController) BlogsPage() {
před 6 roky
blogService := service.BlogService{}
před 6 roky
num, _ := this.GetInt("num")
size, _ := this.GetInt("size")
cat, _ := this.GetInt64("cat")
flag, _ := this.GetInt("flag")
if num <= 0 {
num = 1
}
if size < 15 {
size = 15
}
if cat <= 0 {
cat = -1
}
před 6 roky
pages, err := blogService.FindBlogs(num, size, cat, flag)
před 6 roky
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
před 3 roky
this.Data["json"] = models.ReurnData("", pages)
před 6 roky
this.ServeJSON()
return
}
func (this *MBlogController) EditPage() {
před 6 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 6 roky
blog, err := blogService.GetBlog(id)
před 6 roky
if err != nil {
this.Redirect("/404", 302)
return
}
if blog.UserId != uid.(int64) {
this.Redirect("/403", 302)
return
}
this.Data["Blog"] = blog
this.TplName = "editblog.html"
}
func (this *MBlogController) Save() {
před 6 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
}
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
před 6 roky
err := blogService.SaveBlog(blog, labels)
před 6 roky
if err == nil {
před 3 roky
this.Data["json"] = models.ReurnData("", blog.Id)
před 6 roky
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
před 6 roky
userService.CountBlog(uid.(int64))
před 6 roky
return
}
func (this *MBlogController) Edit() {
před 6 roky
userService := service.UserService{}
blogService := service.BlogService{}
před 6 roky
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
před 3 roky
id, _ := this.GetInt64("id")
před 6 roky
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
před 3 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
blog.CategoryId = catoryId
blog.Utime = time.Now()
před 6 roky
err = blogService.EditBlog(blog, labels)
před 6 roky
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
před 6 roky
userService.CountBlog(uid.(int64))
před 6 roky
return
}
func (this *MBlogController) Del() {
před 6 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
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
před 6 roky
blog, err := blogService.GetBlog(id)
před 6 roky
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 6 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 6 roky
userService.CountBlog(uid.(int64))
před 6 roky
return
před 6 roky
}
func (this *MBlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
this.TplName = "newblog.html"
před 3 roky
}