From ccfbb662ff41db90c912f3f78f7f687ea0d4b625 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sat, 26 Sep 2020 12:30:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E7=AB=A0=E5=88=86?= =?UTF-8?q?=E7=BB=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/blog.go | 55 +++++++++++++++++++++++++++++++++++---- controller/middle/auth.go | 27 ++++++++++--------- db/db.go | 2 +- main.go | 15 ++++++----- model/blog.go | 38 ++++++++++++++++++++++++--- mq/mq.go | 3 --- 6 files changed, 109 insertions(+), 31 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index d1cc003..e05c6b0 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -339,11 +339,8 @@ func ArticlesTypes(c *gin.Context) { defer func() { c.JSON(200, resp) }() - type DocType struct { - Id int64 `sql:"id"` - TypeName string `sql:"type_name"` - } - docTypes := []DocType{} + + docTypes := []model.ArticleType{} e := db.GetMysqlClient().Query2("select * from doc_type", &docTypes) if nil != e { @@ -398,6 +395,54 @@ func UpdateMemo(c *gin.Context) { resp.Status = 0 } +func GetDocTypeGroup(c *gin.Context) { + type DocType struct { + Id int32 `json:"id"` + } + rsp := RespBase{"ERR", -1, nil} + defer func() { + c.JSON(200, rsp) + }() + + var id DocType + e := c.BindJSON(&id) + if nil != e { + log.Print(e.Error) + return + } + group, e := model.GetTypeGroup(id.Id) + if nil != e { + log.Print(e.Error) + return + } + rsp.Data = group + rsp.Status = 0 + rsp.Msg = "OK" +} +func GetDoGroupcType(c *gin.Context) { + type DocType struct { + Id int32 `json:"id"` + } + rsp := RespBase{"ERR", -1, nil} + defer func() { + c.JSON(200, rsp) + }() + + var id DocType + e := c.BindJSON(&id) + if nil != e { + log.Print(e.Error) + return + } + group, e := model.GetGroupTypes(id.Id) + if nil != e { + log.Print(e.Error) + return + } + rsp.Data = group + rsp.Status = 0 + rsp.Msg = "OK" +} func GetDocGroup(c *gin.Context) { rsp := RespBase{"ERR", -1, nil} defer func() { diff --git a/controller/middle/auth.go b/controller/middle/auth.go index fda3816..56b722b 100644 --- a/controller/middle/auth.go +++ b/controller/middle/auth.go @@ -5,18 +5,19 @@ import ( "background/controller" "background/model" "encoding/json" - "github.com/gin-gonic/gin" "log" + + "github.com/gin-gonic/gin" ) -func AuthMiddle(c *gin.Context) { +func AuthMiddle(c *gin.Context) { token := c.Query("token") user := c.Query("userid") - if user == "" || token == ""{ + if user == "" || token == "" { log.Print("error user not existed or token missing") log.Print(c.Request.URL.String()) - c.JSON(200,controller.RespBase{ - "auth err",20,nil, + c.JSON(200, controller.RespBase{ + "auth err", 20, nil, }) c.Abort() return @@ -24,20 +25,20 @@ func AuthMiddle(c *gin.Context) { if config.RedisOne().Exists(token).Val() { users := model.Users{} userInfo := config.RedisOne().Get(token).Val() - e := json.Unmarshal([]byte(userInfo),&users) - if nil != e{ - c.JSON(200,controller.RespBase{ - "auth err",10,nil, + e := json.Unmarshal([]byte(userInfo), &users) + if nil != e { + c.JSON(200, controller.RespBase{ + "auth err", 10, nil, }) c.Abort() return } - }else { - c.JSON(200,controller.RespBase{ - "expired or no login",210,nil, + } else { + c.JSON(200, controller.RespBase{ + "expired or no login", 210, nil, }) c.Abort() return } c.Next() -} \ No newline at end of file +} diff --git a/db/db.go b/db/db.go index 1c01367..d0f1bcc 100644 --- a/db/db.go +++ b/db/db.go @@ -2,6 +2,7 @@ package db import ( + "background/logs" "database/sql" "errors" "fmt" @@ -9,7 +10,6 @@ import ( "strconv" "sync" "time" - "background/logs" ) // 数据容器抽象对象定义 diff --git a/main.go b/main.go index 31ba787..6832502 100644 --- a/main.go +++ b/main.go @@ -159,12 +159,15 @@ func main() { api.GET("/filelist", fileController.FileList) // 文件列表 api.GET("/fileType", fileController.FileType) // 文件类型 - api.PUT("/memo", controller.CreateMemo) // 备忘录新建 - api.POST("/memo", controller.UpdateMemo) // 备忘录更新 - api.POST("/memos", controller.GetMemos) // 备忘录批量 - api.POST("/delmemo", controller.DeleteMemos) //删除备忘录 - api.GET("/memo", controller.GetMemo) // 单独读取 - api.GET("doc_groups", controller.GetDocGroup) // 获取所有的文章分组 + api.PUT("/memo", controller.CreateMemo) // 备忘录新建 + api.POST("/memo", controller.UpdateMemo) // 备忘录更新 + api.POST("/memos", controller.GetMemos) // 备忘录批量 + api.POST("/delmemo", controller.DeleteMemos) //删除备忘录 + api.GET("/memo", controller.GetMemo) // 单独读取 + api.GET("doc_groups", controller.GetDocGroup) // 获取所有的文章分组 + api.POST("type_group", controller.GetDocTypeGroup) // 获取类所在的组 + api.POST("group_type", controller.GetDoGroupcType) // 获取类所在的组 + api.GET("templates", controller.GetDocTemplate) // 获取所有文章的模板 api.GET("doc_versions", nil) // 获取文章的某个版本 diff --git a/model/blog.go b/model/blog.go index c95bc14..dbd9d12 100644 --- a/model/blog.go +++ b/model/blog.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/pkg/errors" "qiniupkg.com/x/log.v7" ) @@ -33,16 +34,17 @@ type ArticleType struct { func GetArticlesType() []ArticleType { ret := []ArticleType{} sql := fmt.Sprintf("select * from doc_type") - e := db.GetBlogMysql().Query2(sql, &ret) + e := db.GetMysqlClient().Query2(sql, &ret) + log.Print(ret) + for k, _ := range ret { group := []DocGroup{} sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group) - db.GetBlogMysql().Query2(sql, &group) + db.GetMysqlClient().Query2(sql, &group) if len(group) > 0 { ret[k].GroupName = group[0].Name } } - log.Print(ret) if nil != e { logs.Error(e.Error()) return nil @@ -143,3 +145,33 @@ func GetAllGroup() ([]DocGroup, error) { } return ret, nil } + +func GetTypeGroup(id int32) (DocGroup, error) { + ret := []DocGroup{} + sql := fmt.Sprintf("select * from doc_group where doc_group = ?", id) + e := db.GetMysqlClient().Query2(sql, &ret) + if nil != e { + logs.Error(e.Error()) + return DocGroup{}, e + } + if len(ret) > 0 { + return ret[0], nil + } else { + return DocGroup{}, errors.New("no existed") + } +} +func GetGroupTypes(id int32) ([]ArticleType, error) { + ret := []ArticleType{} + sql := fmt.Sprintf("select * from doc_type where doc_type.group = %d", id) + log.Print(sql) + e := db.GetMysqlClient().Query2(sql, &ret) + if nil != e { + logs.Error(e.Error()) + return nil, e + } + if len(ret) > 0 { + return ret, nil + } else { + return nil, errors.New("no existed") + } +} diff --git a/mq/mq.go b/mq/mq.go index e737415..f4247d8 100644 --- a/mq/mq.go +++ b/mq/mq.go @@ -1,7 +1,4 @@ package mq type mq struct { - } - -