yirenyishi há 6 anos
ascendente
cometimento
347052da05
  1. 8
      controllers/BlogController.go
  2. 4
      controllers/PageController.go
  3. 16
      controllers/UserController.go
  4. 8
      main.go
  5. 191
      mcontrollers/MBlogController.go
  6. 3
      models/Result.go
  7. 10
      routers/MBlogRouter.go
  8. 4
      routers/router.go

8
controllers/BlogController.go

@ -22,7 +22,7 @@ func (this *BlogController) EditPage() {
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
if blog.UserId != uid.(int64) {
@ -96,7 +96,7 @@ func (this *BlogController) Get() {
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
if uid := this.GetSession("userid"); uid != nil {
@ -167,7 +167,7 @@ func (this *BlogController) New() {
func (this *BlogController) BlogsPage() {
cats, errcat := service.GetCats()
if errcat != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
num, _ := this.GetInt("num")
@ -185,7 +185,7 @@ func (this *BlogController) BlogsPage() {
}
pages, err := service.FindBlogs(num, size, cat, flag)
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
this.Data["UserId"] = this.GetSession("userid")

4
controllers/PageController.go

@ -14,7 +14,7 @@ type PageController struct {
func (this *PageController) Blog() {
cats, err := service.GetCats()
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
this.Data["Cats"] = cats
@ -64,7 +64,7 @@ func (this *PageController) PageNotFound() {
this.TplName = "404.html"
}
// @router /500 [get]
// @router /404 [get]
func (this *PageController) ServerError() {
this.TplName = "500.html"
}

16
controllers/UserController.go

@ -27,7 +27,7 @@ func (this *UserController) UserInfo() {
id, _ := strconv.ParseInt(idStr, 10, 64)
user, err := service.GetUser(id)
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
size := 15
@ -62,12 +62,12 @@ func (this *UserController) PersonBlog() {
flag, _ := this.GetInt("flag")
page, err := service.MeBlogs(num, size, flag, uid.(int64))
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
this.Data["UserId"] = this.GetSession("userid")
@ -99,13 +99,13 @@ func (this *UserController) PersonNote() {
notColl = make([]*models.NoteColl, 0)
}
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
}
@ -132,13 +132,13 @@ func (this *UserController) PersonLike() {
}
page, err := service.MeLikes(num, size, uid.(int64))
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
}
@ -160,7 +160,7 @@ func (this *UserController) PersonInfo() {
}
user, err := service.GetUser(uid.(int64))
if err != nil {
this.Redirect("/500", 302)
this.Redirect("/404", 302)
return
}
this.Data["IsLogin"] = this.GetSession("nickname") != nil

8
main.go

@ -6,6 +6,7 @@ import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"beeblog/filter"
"github.com/astaxie/beego/plugins/cors"
)
func init() {
@ -19,6 +20,13 @@ func main() {
beego.AddFuncMap("NAdd",NAdd)
beego.SetLevel(beego.LevelInformational)
beego.SetLogger("file", `{"filename":"/opt/logs/aiprose.log"}`)
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
}))
beego.Run()
}

191
mcontrollers/MBlogController.go

@ -0,0 +1,191 @@
package mcontrollers
import (
"beeblog/models"
"beeblog/service"
"github.com/astaxie/beego"
"strconv"
"time"
)
type MBlogController struct {
beego.Controller
}
func (this *MBlogController) BlogsPage() {
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
}
pages, err := service.FindBlogs(num, size, cat, flag)
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnData("",pages)
this.ServeJSON()
return
}
func (this *MBlogController) 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("/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() {
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)}
err := service.SaveBlog(blog, labels)
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 *MBlogController) 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 *MBlogController) Get() {
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err != nil {
this.Redirect("/404", 302)
return
}
if uid := this.GetSession("userid"); uid != nil {
if blog.UserId == uid.(int64) {
this.Data["IsAuthor"] = true
}
if flag, err := service.IsLike(id, uid.(int64)); err == nil {
this.Data["IsLike"] = flag
}
}
if blogs, err := service.TopBlogByUser(blog.UserId); err == nil {
this.Data["Top"] = blogs
}
this.Data["Blog"] = blog
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
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 *MBlogController) 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)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
func (this *MBlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
this.TplName = "newblog.html"
}

3
models/Result.go

@ -4,6 +4,9 @@ type Error struct {
Status int
Msg string
}
func ReurnServerError(status int) *Error {
return &Error{Status: status, Msg: "服务器异常"}
}
func ReurnError(status int, msg string) *Error {
if msg == "" {

10
routers/MBlogRouter.go

@ -0,0 +1,10 @@
package routers
import (
"beeblog/mcontrollers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/api/blogs", &mcontrollers.MBlogController{}, "get:BlogsPage")
}

4
routers/router.go

@ -7,8 +7,8 @@ import (
func init() {
beego.Router("/", &controllers.IndexController{})
beego.Router("/500", &controllers.PageController{},"get:PageNotFound")
beego.Router("/404", &controllers.PageController{},"get:PageNotFound")
beego.Router("/500", &controllers.PageController{},"get:ServerError")
beego.Router("/map", &controllers.MapController{})
beego.Router("/us", &controllers.PageController{},"get:UsPage")
beego.Router("/iframe/note", &controllers.PageController{},"get:IframeNote")
@ -16,5 +16,5 @@ func init() {
beego.Router("/iframe/blog", &controllers.PageController{},"get:Blog")
beego.Router("/file/upload", &controllers.FileController{}, "post:Upload")
beego.Router("/himg/upload", &controllers.FileController{}, "post:HeadImgUpload")
beego.Include(&controllers.PageController{})
//beego.Include(&controllers.PageController{})
}

Carregando…
Cancelar
Guardar