码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

253 rindas
6.1 KiB

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