Browse Source

升级beego V2

master
燕鹏 4 years ago
parent
commit
170d97145d
  1. 4
      controllers/BlogController.go
  2. 2
      controllers/CatController.go
  3. 6
      controllers/CommentController.go
  4. 31
      controllers/FileController.go
  5. 2
      controllers/IndexController.go
  6. 14
      controllers/LikeController.go
  7. 2
      controllers/MapController.go
  8. 2
      controllers/NoteController.go
  9. 5
      controllers/PageController.go
  10. 2
      controllers/UPageController.go
  11. 12
      controllers/UserController.go
  12. 8
      filter/Security.go
  13. 6
      go.mod
  14. 14
      main.go
  15. 17
      mcontrollers/MBlogController.go
  16. 29
      models/DataInit.go
  17. 2
      routers/BlogRouter.go
  18. 2
      routers/CatRouter.go
  19. 4
      routers/CommentRouter.go
  20. 2
      routers/LikeRouter.go
  21. 2
      routers/MBlogRouter.go
  22. 2
      routers/NoteRouter.go
  23. 2
      routers/UPageRouter.go
  24. 2
      routers/UserRouter.go
  25. 10
      routers/router.go
  26. 20
      service/BlogService.go
  27. 2
      service/CategoryService.go
  28. 10
      service/CommentService.go
  29. 4
      service/LikeService.go
  30. 2
      service/NoteService.go
  31. 11
      service/UserService.go
  32. 2
      utils/EsUtil.go

4
controllers/BlogController.go

@ -4,7 +4,7 @@ import (
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
"beeblog/utils" "beeblog/utils"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
"strconv" "strconv"
"time" "time"
) )
@ -51,7 +51,7 @@ func (this *BlogController) Save() {
catory := this.GetString("catory") catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64) catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]") labels := this.GetStrings("labels[]")
blog := &models.Blog{Title: title, BlogHtml: blogHtml, BlogValue: blogValue,CategoryId: catoryId, UserId: uid.(int64), BlogDesc: blogDesc} blog := &models.Blog{Title: title, BlogHtml: blogHtml, BlogValue: blogValue, CategoryId: catoryId, UserId: uid.(int64), BlogDesc: blogDesc}
err := blogService.SaveBlog(blog, labels) err := blogService.SaveBlog(blog, labels)
if err == nil { if err == nil {
this.Data["json"] = models.ReurnData("", blog.Id) this.Data["json"] = models.ReurnData("", blog.Id)

2
controllers/CatController.go

@ -3,7 +3,7 @@ package controllers
import ( import (
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
type CatController struct { type CatController struct {

6
controllers/CommentController.go

@ -1,9 +1,9 @@
package controllers package controllers
import ( import (
"github.com/astaxie/beego"
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv" "strconv"
) )
@ -49,7 +49,7 @@ func (this *CommentController) Save() {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
} }
this.ServeJSON() this.ServeJSON()
userService.CountComments(uid.(int64),blogId) userService.CountComments(uid.(int64), blogId)
return return
} }
@ -83,6 +83,6 @@ func (this *CommentController) Del() {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
} }
this.ServeJSON() this.ServeJSON()
userService.CountComments(uid.(int64),id) userService.CountComments(uid.(int64), id)
return return
} }

31
controllers/FileController.go

@ -1,15 +1,16 @@
package controllers package controllers
import ( import (
"github.com/astaxie/beego"
"path"
"time"
"os"
"fmt"
"crypto/md5"
"math/rand"
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
"crypto/md5"
"fmt"
"github.com/beego/beego/v2/core/config"
beego "github.com/beego/beego/v2/server/web"
"math/rand"
"os"
"path"
"time"
) )
type FileController struct { type FileController struct {
@ -45,7 +46,8 @@ func (this *FileController) Upload() {
} }
//创建目录 //创建目录
urlDir := time.Now().Format("2006/01/02/") urlDir := time.Now().Format("2006/01/02/")
uploadDir := beego.AppConfig.String("uploadDir") + urlDir uploadDir, _ := config.String("uploadDir")
uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777) err := os.MkdirAll(uploadDir, 777)
if err != nil { if err != nil {
this.Data["json"] = models.ReurnError(500, "") this.Data["json"] = models.ReurnError(500, "")
@ -55,7 +57,7 @@ func (this *FileController) Upload() {
//构造文件名称 //构造文件名称
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000) randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
hashName := md5.Sum([]byte( time.Now().Format("2006_01_02_15_04_05_") + randNum )) hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
fileName := fmt.Sprintf("%x", hashName) + ext fileName := fmt.Sprintf("%x", hashName) + ext
fpath := uploadDir + fileName fpath := uploadDir + fileName
urlDir += fileName urlDir += fileName
@ -67,7 +69,8 @@ func (this *FileController) Upload() {
this.ServeJSON() this.ServeJSON()
return return
} }
urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir imgUrlPrefix, _ := config.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir
this.Data["json"] = models.ReurnData("", urlDir) this.Data["json"] = models.ReurnData("", urlDir)
this.ServeJSON() this.ServeJSON()
return return
@ -106,7 +109,8 @@ func (this *FileController) HeadImgUpload() {
} }
//创建目录 //创建目录
urlDir := time.Now().Format("2006/01/02/") urlDir := time.Now().Format("2006/01/02/")
uploadDir := beego.AppConfig.String("uploadDir") + urlDir uploadDir, _ := config.String("uploadDir")
uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777) err := os.MkdirAll(uploadDir, 777)
if err != nil { if err != nil {
this.Data["json"] = models.ReurnError(500, "") this.Data["json"] = models.ReurnError(500, "")
@ -116,7 +120,7 @@ func (this *FileController) HeadImgUpload() {
//构造文件名称 //构造文件名称
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000) randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
hashName := md5.Sum([]byte( time.Now().Format("2006_01_02_15_04_05_") + randNum )) hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
fileName := fmt.Sprintf("%x", hashName) + ext fileName := fmt.Sprintf("%x", hashName) + ext
fpath := uploadDir + fileName fpath := uploadDir + fileName
urlDir += fileName urlDir += fileName
@ -128,7 +132,8 @@ func (this *FileController) HeadImgUpload() {
this.ServeJSON() this.ServeJSON()
return return
} }
urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir imgUrlPrefix, _ := config.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir
user := &models.User{Id: uid.(int64), Headimg: urlDir} user := &models.User{Id: uid.(int64), Headimg: urlDir}
userSerivce := service.UserService{} userSerivce := service.UserService{}
if _, err = userSerivce.EditHeadImg(user); err != nil { if _, err = userSerivce.EditHeadImg(user); err != nil {

2
controllers/IndexController.go

@ -1,8 +1,8 @@
package controllers package controllers
import ( import (
"github.com/astaxie/beego"
"beeblog/service" "beeblog/service"
beego "github.com/beego/beego/v2/server/web"
) )
type IndexController struct { type IndexController struct {

14
controllers/LikeController.go

@ -1,10 +1,10 @@
package controllers package controllers
import ( import (
"strconv"
"beeblog/service"
"github.com/astaxie/beego"
"beeblog/models" "beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
) )
type LikeController struct { type LikeController struct {
@ -25,11 +25,11 @@ func (this *LikeController) Save() {
like := &models.Like{BlogId: id, UserId: uid.(int64)} like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := likeService.SaveLike(like); err != nil { if _, err := likeService.SaveLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
}else{ } else {
this.Data["json"] = models.ReurnSuccess("") this.Data["json"] = models.ReurnSuccess("")
} }
this.ServeJSON() this.ServeJSON()
userService.CountLike(uid.(int64),id) userService.CountLike(uid.(int64), id)
return return
} }
@ -47,10 +47,10 @@ func (this *LikeController) Delete() {
like := &models.Like{BlogId: id, UserId: uid.(int64)} like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := likeService.DelLike(like); err != nil { if _, err := likeService.DelLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
}else{ } else {
this.Data["json"] = models.ReurnSuccess("") this.Data["json"] = models.ReurnSuccess("")
} }
this.ServeJSON() this.ServeJSON()
userService.CountLike(uid.(int64),id) userService.CountLike(uid.(int64), id)
return return
} }

2
controllers/MapController.go

@ -1,6 +1,6 @@
package controllers package controllers
import "github.com/astaxie/beego" import beego "github.com/beego/beego/v2/server/web"
type MapController struct { type MapController struct {
beego.Controller beego.Controller

2
controllers/NoteController.go

@ -3,7 +3,7 @@ package controllers
import ( import (
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
"strconv" "strconv"
) )

5
controllers/PageController.go

@ -1,16 +1,15 @@
package controllers package controllers
import ( import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models" "beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
) )
type PageController struct { type PageController struct {
beego.Controller beego.Controller
} }
func (this *PageController) IframeUser() { func (this *PageController) IframeUser() {
userService := service.UserService{} userService := service.UserService{}
uid := this.GetSession("userid") uid := this.GetSession("userid")

2
controllers/UPageController.go

@ -1,6 +1,6 @@
package controllers package controllers
import "github.com/astaxie/beego" import beego "github.com/beego/beego/v2/server/web"
type UPageController struct { type UPageController struct {
beego.Controller beego.Controller

12
controllers/UserController.go

@ -1,14 +1,14 @@
package controllers package controllers
import ( import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models" "beeblog/models"
"strings" "beeblog/service"
"crypto/md5" "crypto/md5"
"time"
"strconv"
"encoding/hex" "encoding/hex"
beego "github.com/beego/beego/v2/server/web"
"strconv"
"strings"
"time"
) )
type UserController struct { type UserController struct {
@ -295,7 +295,7 @@ func (this *UserController) Regist() {
return return
} }
h := md5.New() h := md5.New()
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + beego.AppConfig.String("host"))) h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + "aiprose:nelson"))
salt := hex.EncodeToString(h.Sum(nil)) salt := hex.EncodeToString(h.Sum(nil))
h = md5.New() h = md5.New()
h.Write([]byte(userpwd + salt)) h.Write([]byte(userpwd + salt))

8
filter/Security.go

@ -1,10 +1,10 @@
package filter package filter
import ( import (
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"beeblog/models" "beeblog/models"
"encoding/json" "encoding/json"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web/context"
) )
var LogFilter = func(ctx *context.Context) { var LogFilter = func(ctx *context.Context) {
@ -13,9 +13,9 @@ var LogFilter = func(ctx *context.Context) {
var FilterAdmin = func(ctx *context.Context) { var FilterAdmin = func(ctx *context.Context) {
if ctx.Input.Session("userid") != nil { if ctx.Input.Session("userid") != nil {
ctx.Output.SetStatus(200) ctx.Output.SetStatus(200)
ctx.Output.Header("Access-Control-Allow-Origin","*") ctx.Output.Header("Access-Control-Allow-Origin", "*")
result := models.ReurnError(401, "") result := models.ReurnError(401, "")
if b,err := json.Marshal(result); err != nil { if b, err := json.Marshal(result); err != nil {
ctx.Output.Body(b) ctx.Output.Body(b)
} }
return return

6
go.mod

@ -1,10 +1,10 @@
module beeblog module beeblog
require ( require (
github.com/astaxie/beego v1.12.0 github.com/beego/bee/v2 v2.0.2 // indirect
github.com/beego/beego/v2 v2.0.1
github.com/fortytw2/leaktest v1.3.0 // indirect github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/go-sql-driver/mysql v1.4.1 github.com/go-sql-driver/mysql v1.5.0
github.com/google/go-cmp v0.3.1 // indirect
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
github.com/olivere/elastic v6.2.22+incompatible github.com/olivere/elastic v6.2.22+incompatible
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect

14
main.go

@ -4,10 +4,10 @@ import (
"beeblog/filter" "beeblog/filter"
"beeblog/models" "beeblog/models"
_ "beeblog/routers" _ "beeblog/routers"
"github.com/astaxie/beego" "github.com/beego/beego/v2/client/orm"
"github.com/astaxie/beego/logs" "github.com/beego/beego/v2/core/logs"
"github.com/astaxie/beego/orm" beego "github.com/beego/beego/v2/server/web"
"github.com/astaxie/beego/plugins/cors" "github.com/beego/beego/v2/server/web/filter/cors"
) )
func init() { func init() {
@ -17,8 +17,8 @@ func init() {
} }
func main() { func main() {
orm.Debug = false orm.Debug = false
orm.RunSyncdb("default", false, true)
beego.AddFuncMap("NAdd",NAdd) beego.AddFuncMap("NAdd", NAdd)
logs.SetLogger(logs.AdapterFile, `{"filename":"/opt/logs/aiprose.log","level":1}`) logs.SetLogger(logs.AdapterFile, `{"filename":"/opt/logs/aiprose.log","level":1}`)
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{ beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
@ -31,6 +31,6 @@ func main() {
beego.Run() beego.Run()
} }
func NAdd(n1 int, n2 int) int{ func NAdd(n1 int, n2 int) int {
return n1 + n2 return n1 + n2
} }

17
mcontrollers/MBlogController.go

@ -3,7 +3,7 @@ package mcontrollers
import ( import (
"beeblog/models" "beeblog/models"
"beeblog/service" "beeblog/service"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
"strconv" "strconv"
"time" "time"
) )
@ -12,7 +12,6 @@ type MBlogController struct {
beego.Controller beego.Controller
} }
func (this *MBlogController) Get() { func (this *MBlogController) Get() {
blogService := service.BlogService{} blogService := service.BlogService{}
userService := service.UserService{} userService := service.UserService{}
@ -24,14 +23,13 @@ func (this *MBlogController) Get() {
this.ServeJSON() this.ServeJSON()
return return
} }
this.Data["json"] = models.ReurnData("",blog) this.Data["json"] = models.ReurnData("", blog)
this.ServeJSON() this.ServeJSON()
userService.CountBrows(blog.UserId) userService.CountBrows(blog.UserId)
blogService.EditBlogBrows(id) blogService.EditBlogBrows(id)
return return
} }
func (this *MBlogController) BlogsPage() { func (this *MBlogController) BlogsPage() {
blogService := service.BlogService{} blogService := service.BlogService{}
num, _ := this.GetInt("num") num, _ := this.GetInt("num")
@ -53,12 +51,11 @@ func (this *MBlogController) BlogsPage() {
this.ServeJSON() this.ServeJSON()
return return
} }
this.Data["json"] = models.ReurnData("",pages) this.Data["json"] = models.ReurnData("", pages)
this.ServeJSON() this.ServeJSON()
return return
} }
func (this *MBlogController) EditPage() { func (this *MBlogController) EditPage() {
blogService := service.BlogService{} blogService := service.BlogService{}
uid := this.GetSession("userid") uid := this.GetSession("userid")
@ -98,7 +95,7 @@ func (this *MBlogController) Save() {
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)} blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
err := blogService.SaveBlog(blog, labels) err := blogService.SaveBlog(blog, labels)
if err == nil { if err == nil {
this.Data["json"] = models.ReurnData("",blog.Id) this.Data["json"] = models.ReurnData("", blog.Id)
} else { } else {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
} }
@ -116,13 +113,13 @@ func (this *MBlogController) Edit() {
this.ServeJSON() this.ServeJSON()
return return
} }
id,_ := this.GetInt64("id") id, _ := this.GetInt64("id")
title := this.GetString("title") title := this.GetString("title")
blogHtml := this.GetString("blogHtml") blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory") catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64) catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]") labels := this.GetStrings("labels[]")
blog,err :=blogService.GetBlog(id) blog, err := blogService.GetBlog(id)
if err != nil { if err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败") this.Data["json"] = models.ReurnError(500, "保存失败")
this.ServeJSON() this.ServeJSON()
@ -185,4 +182,4 @@ func (this *MBlogController) New() {
return return
} }
this.TplName = "newblog.html" this.TplName = "newblog.html"
} }

29
models/DataInit.go

@ -1,29 +1,18 @@
package models package models
import ( import (
"github.com/astaxie/beego" "fmt"
//"github.com/Unknwon/com" "github.com/beego/beego/v2/client/orm"
//"os" "github.com/beego/beego/v2/core/config"
//"path"
"github.com/astaxie/beego/orm"
//_ "github.com/mattn/go-sqlite3"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
const (
_DB_NAME = "data/beeblog.db"
_SQLITE3_DRIVER = "sqlite3"
)
func RegistDB() { func RegistDB() {
//if !com.IsExist(_DB_NAME){ dbUrl, _ := config.String("dburl")
// os.MkdirAll(path.Dir(_DB_NAME),os.ModePerm) fmt.Println(dbUrl)
// os.Create(_DB_NAME) orm.RegisterDriver("mysql", orm.DRMySQL)
//} orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local")
//orm.RegisterModel(new(Attachment),new(Topic))
//orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
//orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment)) orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment))
orm.RegisterDataBase("default", "mysql", "root:"+beego.AppConfig.String("dburl")+"/beeblog?charset=utf8&loc=Local", 30)
//orm.RunSyncdb("default", false, true)
} }

2
routers/BlogRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

2
routers/CatRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

4
routers/CommentRouter.go

@ -1,11 +1,11 @@
package routers package routers
import ( import (
"github.com/astaxie/beego"
"beeblog/controllers" "beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {
beego.Router("/api/comms/save", &controllers.CommentController{}, "post:Save") beego.Router("/api/comms/save", &controllers.CommentController{}, "post:Save")
beego.Router("/api/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del") beego.Router("/api/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del")
} }

2
routers/LikeRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

2
routers/MBlogRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/mcontrollers" "beeblog/mcontrollers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

2
routers/NoteRouter.go

@ -1,8 +1,8 @@
package routers package routers
import ( import (
"github.com/astaxie/beego"
"beeblog/controllers" "beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

2
routers/UPageRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

2
routers/UserRouter.go

@ -2,7 +2,7 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {

10
routers/router.go

@ -2,16 +2,16 @@ package routers
import ( import (
"beeblog/controllers" "beeblog/controllers"
"github.com/astaxie/beego" beego "github.com/beego/beego/v2/server/web"
) )
func init() { func init() {
beego.Router("/", &controllers.IndexController{}) beego.Router("/", &controllers.IndexController{})
beego.Router("/404", &controllers.PageController{},"get:PageNotFound") beego.Router("/404", &controllers.PageController{}, "get:PageNotFound")
beego.Router("/500", &controllers.PageController{},"get:ServerError") beego.Router("/500", &controllers.PageController{}, "get:ServerError")
beego.Router("/map", &controllers.MapController{}) beego.Router("/map", &controllers.MapController{})
beego.Router("/us", &controllers.PageController{},"get:UsPage") beego.Router("/us", &controllers.PageController{}, "get:UsPage")
beego.Router("/iframe/user", &controllers.PageController{},"get:IframeUser") beego.Router("/iframe/user", &controllers.PageController{}, "get:IframeUser")
beego.Router("/file/upload", &controllers.FileController{}, "post:Upload") beego.Router("/file/upload", &controllers.FileController{}, "post:Upload")
beego.Router("/himg/upload", &controllers.FileController{}, "post:HeadImgUpload") beego.Router("/himg/upload", &controllers.FileController{}, "post:HeadImgUpload")
//beego.Include(&controllers.PageController{}) //beego.Include(&controllers.PageController{})

20
service/BlogService.go

@ -1,9 +1,9 @@
package service package service
import ( import (
"github.com/astaxie/beego/orm"
"beeblog/models" "beeblog/models"
"beeblog/utils" "beeblog/utils"
"github.com/beego/beego/v2/client/orm"
) )
type BlogService struct { type BlogService struct {
@ -133,7 +133,6 @@ func (this *BlogService) FindBlogs(num int, size int, cat int64, flag int) (*uti
return page, nil return page, nil
} }
func (this *BlogService) AllBlogs() ([]*models.Blog, error) { func (this *BlogService) AllBlogs() ([]*models.Blog, error) {
var blogs []*models.Blog var blogs []*models.Blog
@ -147,13 +146,12 @@ func (this *BlogService) AllBlogs() ([]*models.Blog, error) {
return blogs, nil return blogs, nil
} }
func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error { func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error {
o := orm.NewOrm() o := orm.NewOrm()
o.Begin() tx, _ := o.Begin()
id, eror := o.Insert(blog) id, eror := o.Insert(blog)
if eror != nil { if eror != nil {
o.Rollback() tx.Rollback()
return eror return eror
} else { } else {
blog.Id = id blog.Id = id
@ -163,21 +161,21 @@ func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error {
nlabels[i] = &models.NLabel{Title: strs[i], BlogId: id, UserId: blog.UserId} nlabels[i] = &models.NLabel{Title: strs[i], BlogId: id, UserId: blog.UserId}
} }
if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil { if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil {
o.Rollback() tx.Rollback()
return err return err
} }
} }
o.Commit() tx.Commit()
} }
return nil return nil
} }
func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error { func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error {
o := orm.NewOrm() o := orm.NewOrm()
o.Begin() tx, _ := o.Begin()
_, eror := o.Update(blog) _, eror := o.Update(blog)
if eror != nil { if eror != nil {
o.Rollback() tx.Rollback()
return eror return eror
} else { } else {
o.QueryTable(models.NLabel{}).Filter("BlogId", blog.Id).Delete() o.QueryTable(models.NLabel{}).Filter("BlogId", blog.Id).Delete()
@ -187,11 +185,11 @@ func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error {
nlabels[i] = &models.NLabel{Title: strs[i], BlogId: blog.Id, UserId: blog.UserId} nlabels[i] = &models.NLabel{Title: strs[i], BlogId: blog.Id, UserId: blog.UserId}
} }
if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil { if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil {
o.Rollback() tx.Rollback()
return err return err
} }
} }
o.Commit() tx.Commit()
} }
return nil return nil
} }

2
service/CategoryService.go

@ -1,8 +1,8 @@
package service package service
import ( import (
"github.com/astaxie/beego/orm"
"beeblog/models" "beeblog/models"
"github.com/beego/beego/v2/client/orm"
) )
type CategoryService struct { type CategoryService struct {

10
service/CommentService.go

@ -2,8 +2,8 @@ package service
import ( import (
"beeblog/models" "beeblog/models"
"github.com/astaxie/beego/orm"
"fmt" "fmt"
"github.com/beego/beego/v2/client/orm"
) )
type CommentService struct { type CommentService struct {
@ -12,7 +12,7 @@ type CommentService struct {
func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, error) { func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, error) {
var comms []*models.Comment var comms []*models.Comment
o := orm.NewOrm() o := orm.NewOrm()
_, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).Filter("BlogId",bid).OrderBy("-Ctime").All(&comms) _, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).Filter("BlogId", bid).OrderBy("-Ctime").All(&comms)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -21,9 +21,9 @@ func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, err
var childs []*models.Comment var childs []*models.Comment
_, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs) _, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs)
if childerrr == nil { if childerrr == nil {
if len(childs) > 0{ if len(childs) > 0 {
comms[i].Childs = childs comms[i].Childs = childs
for j:=0 ; j<len(childs);j++{ for j := 0; j < len(childs); j++ {
cuser := &models.User{Id: childs[j].CuserId} cuser := &models.User{Id: childs[j].CuserId}
o.Read(cuser) o.Read(cuser)
childs[j].CUser = cuser childs[j].CUser = cuser
@ -49,7 +49,7 @@ func (this *CommentService) SaveComment(comment *models.Comment) error {
cuser := &models.User{Id: comment.CuserId} cuser := &models.User{Id: comment.CuserId}
o.Read(cuser) o.Read(cuser)
comment.CUser = cuser comment.CUser = cuser
if comment.BuserId !=0 { if comment.BuserId != 0 {
buser := &models.User{Id: comment.BuserId} buser := &models.User{Id: comment.BuserId}
o.Read(buser) o.Read(buser)
comment.BUser = buser comment.BUser = buser

4
service/LikeService.go

@ -2,9 +2,9 @@ package service
import ( import (
"beeblog/models" "beeblog/models"
"github.com/astaxie/beego/orm"
"fmt"
"beeblog/utils" "beeblog/utils"
"fmt"
"github.com/beego/beego/v2/client/orm"
) )
type LikeService struct { type LikeService struct {

2
service/NoteService.go

@ -2,8 +2,8 @@ package service
import ( import (
"beeblog/models" "beeblog/models"
"github.com/astaxie/beego/orm"
"errors" "errors"
"github.com/beego/beego/v2/client/orm"
) )
type NoteService struct { type NoteService struct {

11
service/UserService.go

@ -1,8 +1,8 @@
package service package service
import ( import (
"github.com/astaxie/beego/orm"
"beeblog/models" "beeblog/models"
"github.com/beego/beego/v2/client/orm"
) )
type UserService struct { type UserService struct {
@ -36,7 +36,6 @@ func (this *UserService) SaveUser(user *models.User) error {
return eror return eror
} else { } else {
user.Id = id user.Id = id
o.Commit()
} }
return nil return nil
} }
@ -53,14 +52,14 @@ func (this *UserService) CountBrows(uid int64) {
o.Raw("UPDATE `auth_user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数 o.Raw("UPDATE `auth_user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
return return
} }
func (this *UserService) CountComments(uid int64,bid int64) { func (this *UserService) CountComments(uid int64, bid int64) {
o := orm.NewOrm() o := orm.NewOrm()
browses := 0 browses := 0
o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) num from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数 o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) num from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
o.Raw("UPDATE `blog` SET `comments` = (select count(id) num from comment where blog_id = ?) WHERE `id` = ? ", bid, bid).QueryRow(&browses) //获取总条数 o.Raw("UPDATE `blog` SET `comments` = (select count(id) num from comment where blog_id = ?) WHERE `id` = ? ", bid, bid).QueryRow(&browses) //获取总条数
return return
} }
func (this *UserService) CountLike(uid int64,bid int64) { func (this *UserService) CountLike(uid int64, bid int64) {
o := orm.NewOrm() o := orm.NewOrm()
browses := 0 browses := 0
o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) num from tb_like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses) o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) num from tb_like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses)
@ -73,5 +72,5 @@ func (this *UserService) EditUser(user *models.User) (int64, error) {
} }
func (this *UserService) EditHeadImg(user *models.User) (int64, error) { func (this *UserService) EditHeadImg(user *models.User) (int64, error) {
return orm.NewOrm().Update(user,"Headimg") return orm.NewOrm().Update(user, "Headimg")
} }

2
utils/EsUtil.go

@ -13,7 +13,7 @@ import (
) )
var client *elastic.Client var client *elastic.Client
var host = "http://47.98.109.5:8209/" var host = "http://49.235.160.131:6002/"
const mapping = ` const mapping = `
{ {

Loading…
Cancel
Save