码农笔录博客源码
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

203 righe
4.7 KiB

package controllers
import (
"beeblog/models"
"beeblog/service"
"github.com/astaxie/beego"
"strconv"
6 anni fa
"time"
)
type BlogController struct {
beego.Controller
}
6 anni fa
func (this *BlogController) EditPage() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/500", 302)
return
}
if blog.UserId != uid.(int64) {
this.Redirect("/403", 302)
return
}
this.Data["Blog"] = blog
this.TplName = "editblog.html"
}
func (this *BlogController) Save() {
uid := this.GetSession("userid")
6 anni fa
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
6 anni fa
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)}
6 anni fa
err := service.SaveBlog(blog, labels)
6 anni fa
if err == nil {
this.Data["json"] = models.ReurnData("",blog.Id)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
func (this *BlogController) Edit() {
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
id,_ := this.GetInt64("id")
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog,err :=service.GetBlog(id)
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()
err = service.EditBlog(blog, labels)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
func (this *BlogController) Get() {
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/500", 302)
return
}
6 anni fa
if uid := this.GetSession("userid"); uid != nil {
6 anni fa
if blog.UserId == uid.(int64) {
this.Data["IsAuthor"] = true
}
if flag, err := service.IsLike(id, uid.(int64)); err == nil {
this.Data["IsLike"] = flag
}
}
6 anni fa
if blogs, err := service.TopBlogByUser(blog.UserId); err == nil {
this.Data["Top"] = blogs
}
this.Data["Blog"] = blog
6 anni fa
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
6 anni fa
this.Data["UserId"] = this.GetSession("userid")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.TplName = "blog.html"
service.CountBrows(blog.UserId)
service.EditBlogBrows(id)
return
}
func (this *BlogController) Del() {
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)
blog, err := service.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
err = service.DelBlog(blog)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
6 anni fa
func (this *BlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
6 anni fa
this.Redirect("/login", 302)
return
}
6 anni fa
this.TplName = "newblog.html"
6 anni fa
}
6 anni fa
func (this *BlogController) BlogsPage() {
6 anni fa
cats, errcat := service.GetCats()
if errcat != nil {
6 anni fa
this.Redirect("/500", 302)
return
}
num, _ := this.GetInt("num")
size, _ := this.GetInt("size")
cat, _ := this.GetInt64("cat")
flag, _ := this.GetInt("flag")
if num <= 0 {
num = 1
}
6 anni fa
if size < 15 {
size = 15
}
if cat <= 0 {
cat = -1
}
pages, err := service.FindBlogs(num, size, cat, flag)
if err != nil {
6 anni fa
this.Redirect("/500", 302)
return
}
6 anni fa
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
6 anni fa
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = pages
this.Data["Cats"] = cats
this.Data["Cat"] = cat
this.Data["Flag"] = flag
6 anni fa
this.Data["IsBlog"] = true
this.TplName = "blogs.html"
}
6 anni fa