码农笔录博客源码
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

186 linhas
4.3 KiB

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