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

104 lines
2.3 KiB

пре 6 година
package controllers
import (
"beeblog/models"
"beeblog/service"
пре 6 година
"github.com/astaxie/beego"
пре 6 година
"strconv"
пре 6 година
"fmt"
пре 6 година
)
type BlogController struct {
beego.Controller
}
func (this *BlogController) Save() {
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")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
пре 6 година
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
пре 6 година
err := service.SaveBlog(blog, labels)
пре 6 година
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
пре 6 година
} else {
пре 6 година
this.Data["json"] = models.ReurnError(500, "保存失败")
пре 6 година
}
this.ServeJSON()
пре 6 година
service.CountBlog(uid.(int64))
пре 6 година
return
пре 6 година
}
func (this *BlogController) Get() {
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
пре 6 година
if err != nil {
пре 6 година
this.Redirect("/500",302)
пре 6 година
return
пре 6 година
}
пре 6 година
this.Data["Blog"] = blog
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
пре 6 година
this.TplName = "blog.html"
пре 6 година
service.CountBrows(blog.UserId)
service.EditBlogBrows(id)
return
пре 6 година
}
пре 6 година
func (this *BlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
пре 6 година
this.Redirect("login.html", 302)
return
}
пре 6 година
this.TplName = "newblog.html"
пре 6 година
}
пре 6 година
func (this *BlogController) Blog1() {
this.TplName = "blog1.html"
}
пре 6 година
func (this *BlogController) BlogsPage() {
пре 6 година
cats, errcat := service.GetCats()
if errcat != nil {
пре 6 година
this.Redirect("/500", 302)
return
}
пре 6 година
num, _ := this.GetInt("num")
size, _ := this.GetInt("size")
cat, _ := this.GetInt64("cat")
flag, _ := this.GetInt("flag")
if num <= 0 {
num = 1
}
if size < 5 {
size = 5
}
if cat <= 0 {
cat = -1
}
fmt.Println("nelson page", num, size, cat)
pages, err := service.FindBlogs(num, size, cat, flag)
if err != nil {
пре 6 година
this.Redirect("/500", 302)
пре 6 година
return
}
пре 6 година
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 година
}