diff --git a/app/admin/apis/sysjob/sysjob.go b/app/admin/apis/sysjob/sysjob.go index 5c6b7ca2..72675173 100644 --- a/app/admin/apis/sysjob/sysjob.go +++ b/app/admin/apis/sysjob/sysjob.go @@ -25,14 +25,14 @@ func (e *SysJob) RemoveJobForService(c *gin.Context) { msgID := tools.GenerateMsgIDFromContext(c) db, err := e.GetOrm(c) if err != nil { - log.Errorf("msgID[%s] error:%#v", msgID, err) + log.Errorf("msgID[%s] error:%s", msgID, err) app.Error(c, 500, err, "") return } var v dto.GeneralDelDto err = c.BindUri(&v) if err != nil { - log.Errorf("msgID[%s] 参数验证错误, error:%#v", msgID, err) + log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err) app.Error(c, 422, err, "参数验证失败") return } @@ -52,14 +52,14 @@ func (e *SysJob) StartJobForService(c *gin.Context) { msgID := tools.GenerateMsgIDFromContext(c) db, err := e.GetOrm(c) if err != nil { - log.Errorf("msgID[%s] error:%#v", msgID, err) + log.Errorf("msgID[%s] error:%s", msgID, err) app.Error(c, 500, err, "") return } var v dto.GeneralGetDto err = c.BindUri(&v) if err != nil { - log.Errorf("msgID[%s] 参数验证错误, error:%#v", msgID, err) + log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err) app.Error(c, 422, err, "参数验证失败") return } diff --git a/app/admin/router/sysjob.go b/app/admin/router/sysjob.go index 75f670eb..85270907 100644 --- a/app/admin/router/sysjob.go +++ b/app/admin/router/sysjob.go @@ -24,7 +24,9 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew list := make([]models.SysJob, 0) return &list })) - r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.SysJobById))) + r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.SysJobById), func() interface{} { + return &dto.SysJobItem{} + })) r.POST("", actions.CreateAction(new(dto.SysJobControl))) r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.SysJobControl))) r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.SysJobById))) diff --git a/app/admin/service/dto/sysjob.go b/app/admin/service/dto/sysjob.go index 1335cdf2..d9ac1935 100644 --- a/app/admin/service/dto/sysjob.go +++ b/app/admin/service/dto/sysjob.go @@ -26,7 +26,7 @@ func (m *SysJobSearch) GetNeedSearch() interface{} { func (m *SysJobSearch) Bind(ctx *gin.Context) error { err := ctx.Bind(m) if err != nil { - log.Errorf("MsgID[%s] Bind error: %#v", err) + log.Errorf("MsgID[%s] Bind error: %s", err) } return err } @@ -91,3 +91,17 @@ func (s *SysJobById) Generate() dto.Control { func (s *SysJobById) GenerateM() (common.ActiveRecord, error) { return &models.SysJob{}, nil } + +type SysJobItem struct { + JobId int `json:"jobId"` + JobName string `json:"jobName" validate:"required"` // 名称 + JobGroup string `json:"jobGroup"` // 任务分组 + JobType int `json:"jobType"` // 任务类型 + CronExpression string `json:"cronExpression"` // cron表达式 + InvokeTarget string `json:"invokeTarget"` // 调用目标 + Args string `json:"args"` // 目标参数 + MisfirePolicy int `json:"misfirePolicy"` // 执行策略 + Concurrent int `json:"concurrent"` // 是否并发 + Status int `json:"status"` // 状态 + EntryId int `json:"entryId"` // job启动时返回的id +} diff --git a/app/admin/service/sysjob.go b/app/admin/service/sysjob.go index aa0d3112..2cf31ff5 100644 --- a/app/admin/service/sysjob.go +++ b/app/admin/service/sysjob.go @@ -23,7 +23,7 @@ func (e *SysJob) RemoveJob(c *dto.GeneralDelDto) error { data.JobId = c.Id err = e.Orm.Table(data.TableName()).First(&data).Error if err != nil { - log.Errorf("msgID[%s] db error:%#v", msgID, err) + log.Errorf("msgID[%s] db error:%s", msgID, err) return err } cn := jobs.Remove(data.EntryId) @@ -33,7 +33,7 @@ func (e *SysJob) RemoveJob(c *dto.GeneralDelDto) error { if res { err = e.Orm.Table(data.TableName()).Where("entry_id = ?", data.EntryId).Update("entry_id", 0).Error if err != nil { - log.Errorf("msgID[%s] db error:%#v", msgID, err) + log.Errorf("msgID[%s] db error:%s", msgID, err) } return err } @@ -51,7 +51,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error { msgID := e.MsgID err = e.Orm.Table(data.TableName()).First(&data, c.Id).Error if err != nil { - log.Errorf("msgID[%s] db error:%#v", msgID, err) + log.Errorf("msgID[%s] db error:%s", msgID, err) return err } if data.JobType == 1 { @@ -62,7 +62,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error { j.Name = data.JobName data.EntryId, err = jobs.AddJob(j) if err != nil { - log.Errorf("msgID[%s] jobs AddJob[HttpJob] error:%#v", msgID, err) + log.Errorf("msgID[%s] jobs AddJob[HttpJob] error:%s", msgID, err) } } else { var j = &jobs.ExecJob{} @@ -73,7 +73,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error { j.Args = data.Args data.EntryId, err = jobs.AddJob(j) if err != nil { - log.Errorf("msgID[%s] jobs AddJob[ExecJob] error:%#v", msgID, err) + log.Errorf("msgID[%s] jobs AddJob[ExecJob] error:%s", msgID, err) } } if err != nil { @@ -82,7 +82,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error { err = e.Orm.Table(data.TableName()).Where(c.Id).Updates(&data).Error if err != nil { - log.Errorf("msgID[%s] db error:%#v", msgID, err) + log.Errorf("msgID[%s] db error:%s", msgID, err) } return err } diff --git a/common/actions/create.go b/common/actions/create.go index 4b4a079e..89110626 100644 --- a/common/actions/create.go +++ b/common/actions/create.go @@ -5,7 +5,6 @@ import ( "github.com/gin-gonic/gin" - "go-admin/common/apis" "go-admin/common/dto" "go-admin/common/log" "go-admin/common/models" @@ -16,7 +15,7 @@ import ( // CreateAction 通用新增动作 func CreateAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -39,7 +38,7 @@ func CreateAction(control dto.Control) gin.HandlerFunc { object.SetCreateBy(tools.GetUserIdUint(c)) err = db.WithContext(c).Create(object).Error if err != nil { - log.Errorf("MsgID[%s] Create error: %#v", msgID, err) + log.Errorf("MsgID[%s] Create error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "创建失败") return } diff --git a/common/actions/delete.go b/common/actions/delete.go index 03aef405..27d0c0a0 100644 --- a/common/actions/delete.go +++ b/common/actions/delete.go @@ -5,7 +5,6 @@ import ( "github.com/gin-gonic/gin" - "go-admin/common/apis" "go-admin/common/dto" "go-admin/common/log" "go-admin/common/models" @@ -16,7 +15,7 @@ import ( // DeleteAction 通用删除动作 func DeleteAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -27,7 +26,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc { req := control.Generate() err = req.Bind(c) if err != nil { - log.Errorf("MsgID[%s] Bind error: %#v", msgID, err) + log.Errorf("MsgID[%s] Bind error: %s", msgID, err) app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") return } @@ -47,7 +46,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc { Permission(object.TableName(), p), ).Where(req.GetId()).Delete(object) if db.Error != nil { - log.Errorf("MsgID[%s] Delete error: %#v", msgID, err) + log.Errorf("MsgID[%s] Delete error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "删除失败") return } diff --git a/common/actions/index.go b/common/actions/index.go index a4589534..208e8a78 100644 --- a/common/actions/index.go +++ b/common/actions/index.go @@ -7,7 +7,6 @@ import ( "github.com/gin-gonic/gin" "gorm.io/gorm" - "go-admin/common/apis" "go-admin/common/dto" "go-admin/common/log" "go-admin/common/models" @@ -18,7 +17,7 @@ import ( // IndexAction 通用查询动作 func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -49,7 +48,7 @@ func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.H Find(list).Limit(-1).Offset(-1). Count(&count).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - log.Errorf("MsgID[%s] Index error: %#v", msgID, err) + log.Errorf("MsgID[%s] Index error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "查询失败") return } diff --git a/common/actions/permission.go b/common/actions/permission.go index 0cd63016..8a9365f2 100644 --- a/common/actions/permission.go +++ b/common/actions/permission.go @@ -7,7 +7,6 @@ import ( "github.com/gin-gonic/gin" "gorm.io/gorm" - "go-admin/common/apis" "go-admin/common/log" "go-admin/tools" "go-admin/tools/app" @@ -23,7 +22,7 @@ type dataPermission struct { func PermissionAction() gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -34,7 +33,7 @@ func PermissionAction() gin.HandlerFunc { if userId := tools.GetUserIdStr(c); userId != "" { p, err = newDataPermission(db, userId) if err != nil { - log.Errorf("MsgID[%s] PermissionAction error: %#v", msgID, err) + log.Errorf("MsgID[%s] PermissionAction error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "权限范围鉴定错误") c.Abort() return diff --git a/common/actions/update.go b/common/actions/update.go index 16c9c2d6..7c89e8b3 100644 --- a/common/actions/update.go +++ b/common/actions/update.go @@ -5,7 +5,6 @@ import ( "github.com/gin-gonic/gin" - "go-admin/common/apis" "go-admin/common/dto" "go-admin/common/log" "go-admin/common/models" @@ -16,7 +15,7 @@ import ( // UpdateAction 通用更新动作 func UpdateAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -45,7 +44,7 @@ func UpdateAction(control dto.Control) gin.HandlerFunc { Permission(object.TableName(), p), ).Where(req.GetId()).Updates(object) if db.Error != nil { - log.Errorf("MsgID[%s] Update error: %#v", msgID, err) + log.Errorf("MsgID[%s] Update error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "更新失败") return } diff --git a/common/actions/view.go b/common/actions/view.go index 6ada185b..e975ae7c 100644 --- a/common/actions/view.go +++ b/common/actions/view.go @@ -7,7 +7,6 @@ import ( "github.com/gin-gonic/gin" "gorm.io/gorm" - "go-admin/common/apis" "go-admin/common/dto" "go-admin/common/log" "go-admin/common/models" @@ -16,9 +15,9 @@ import ( ) // ViewAction 通用详情动作 -func ViewAction(control dto.Control, params ...interface{}) gin.HandlerFunc { +func ViewAction(control dto.Control, f func() interface{}) gin.HandlerFunc { return func(c *gin.Context) { - db, err := apis.GetOrm(c) + db, err := tools.GetOrm(c) if err != nil { log.Error(err) return @@ -39,23 +38,30 @@ func ViewAction(control dto.Control, params ...interface{}) gin.HandlerFunc { return } + var rsp interface{} + if f != nil { + rsp = f() + } else { + rsp, _ = req.GenerateM() + } + //数据权限检查 p := getPermissionFromContext(c) - err = db.WithContext(c).Scopes( + err = db.Model(object).WithContext(c).Scopes( Permission(object.TableName(), p), - ).Where(req.GetId()).First(object).Error + ).Where(req.GetId()).First(rsp).Error - if errors.Is(err, gorm.ErrRecordNotFound) { + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { app.Error(c, http.StatusNotFound, nil, "查看对象不存在或无权查看") return } if err != nil { - log.Errorf("MsgID[%s] Create error: %#v", msgID, err) + log.Errorf("MsgID[%s] View error: %s", msgID, err) app.Error(c, http.StatusInternalServerError, err, "查看失败") return } - app.OK(c, object, "查看成功") + app.OK(c, rsp, "查看成功") c.Next() } } diff --git a/common/apis/api.go b/common/apis/api.go index 3df5ad69..11d3577a 100644 --- a/common/apis/api.go +++ b/common/apis/api.go @@ -1,9 +1,6 @@ package apis import ( - "errors" - "fmt" - "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -14,21 +11,5 @@ type Api struct { } func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) { - return GetOrm(c) -} - -// GetOrm 获取orm连接 -func GetOrm(c *gin.Context) (*gorm.DB, error) { - msgID := tools.GenerateMsgIDFromContext(c) - idb, exist := c.Get("db") - if !exist { - return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID)) - } - switch idb.(type) { - case *gorm.DB: - //新增操作 - return idb.(*gorm.DB), nil - default: - return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID)) - } + return tools.GetOrm(c) } diff --git a/template/v2/routercheckrole.go.template b/template/v2/routercheckrole.go.template index b853e346..88451bd2 100644 --- a/template/v2/routercheckrole.go.template +++ b/template/v2/routercheckrole.go.template @@ -23,7 +23,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW list := make([]models.{{.ClassName}}, 0) return &list })) - r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById))) + r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById), nil)) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control))) r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById))) diff --git a/template/v2/routernocheckrole.go.template b/template/v2/routernocheckrole.go.template index 39de40c9..c647266e 100644 --- a/template/v2/routernocheckrole.go.template +++ b/template/v2/routernocheckrole.go.template @@ -22,7 +22,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) { list := make([]models.{{.ClassName}}, 0) return &list })) - r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById))) + r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById), nil)) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) r.PUT("/:id", actions.UpdateAction(new(dto.{{.ClassName}}Control))) r.DELETE("", actions.DeleteAction(new(dto.{{.ClassName}}ById))) diff --git a/tools/utils.go b/tools/utils.go index 7f085584..392eefbc 100644 --- a/tools/utils.go +++ b/tools/utils.go @@ -1,6 +1,9 @@ package tools import ( + "errors" + "fmt" + "gorm.io/gorm" "log" "runtime" "strconv" @@ -73,3 +76,19 @@ func GenerateMsgIDFromContext(c *gin.Context) string { msgID = cast.ToString(data) return msgID } + +// GetOrm 获取orm连接 +func GetOrm(c *gin.Context) (*gorm.DB, error) { + msgID := GenerateMsgIDFromContext(c) + idb, exist := c.Get("db") + if !exist { + return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID)) + } + switch idb.(type) { + case *gorm.DB: + //新增操作 + return idb.(*gorm.DB), nil + default: + return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID)) + } +}