支持view自定义返回
This commit is contained in:
1 parent
78bb246390
commit
c0ac9a6397
12 files changed
+73
-56
No files matched your search
@@ -25,14 +25,14 @@ func (e *SysJob) RemoveJobForService(c *gin.Context) {
|
|||||||
msgID := tools.GenerateMsgIDFromContext(c)
|
msgID := tools.GenerateMsgIDFromContext(c)
|
||||||
db, err := e.GetOrm(c)
|
db, err := e.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] error:%#v", msgID, err)
|
log.Errorf("msgID[%s] error:%s", msgID, err)
|
||||||
app.Error(c, 500, err, "")
|
app.Error(c, 500, err, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var v dto.GeneralDelDto
|
var v dto.GeneralDelDto
|
||||||
err = c.BindUri(&v)
|
err = c.BindUri(&v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] 参数验证错误, error:%#v", msgID, err)
|
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
|
||||||
app.Error(c, 422, err, "参数验证失败")
|
app.Error(c, 422, err, "参数验证失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -52,14 +52,14 @@ func (e *SysJob) StartJobForService(c *gin.Context) {
|
|||||||
msgID := tools.GenerateMsgIDFromContext(c)
|
msgID := tools.GenerateMsgIDFromContext(c)
|
||||||
db, err := e.GetOrm(c)
|
db, err := e.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] error:%#v", msgID, err)
|
log.Errorf("msgID[%s] error:%s", msgID, err)
|
||||||
app.Error(c, 500, err, "")
|
app.Error(c, 500, err, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var v dto.GeneralGetDto
|
var v dto.GeneralGetDto
|
||||||
err = c.BindUri(&v)
|
err = c.BindUri(&v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] 参数验证错误, error:%#v", msgID, err)
|
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
|
||||||
app.Error(c, 422, err, "参数验证失败")
|
app.Error(c, 422, err, "参数验证失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew
|
|||||||
list := make([]models.SysJob, 0)
|
list := make([]models.SysJob, 0)
|
||||||
return &list
|
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.POST("", actions.CreateAction(new(dto.SysJobControl)))
|
||||||
r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.SysJobControl)))
|
r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.SysJobControl)))
|
||||||
r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.SysJobById)))
|
r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.SysJobById)))
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func (m *SysJobSearch) GetNeedSearch() interface{} {
|
|||||||
func (m *SysJobSearch) Bind(ctx *gin.Context) error {
|
func (m *SysJobSearch) Bind(ctx *gin.Context) error {
|
||||||
err := ctx.Bind(m)
|
err := ctx.Bind(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("MsgID[%s] Bind error: %#v", err)
|
log.Errorf("MsgID[%s] Bind error: %s", err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -91,3 +91,17 @@ func (s *SysJobById) Generate() dto.Control {
|
|||||||
func (s *SysJobById) GenerateM() (common.ActiveRecord, error) {
|
func (s *SysJobById) GenerateM() (common.ActiveRecord, error) {
|
||||||
return &models.SysJob{}, nil
|
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
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@ func (e *SysJob) RemoveJob(c *dto.GeneralDelDto) error {
|
|||||||
data.JobId = c.Id
|
data.JobId = c.Id
|
||||||
err = e.Orm.Table(data.TableName()).First(&data).Error
|
err = e.Orm.Table(data.TableName()).First(&data).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] db error:%#v", msgID, err)
|
log.Errorf("msgID[%s] db error:%s", msgID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cn := jobs.Remove(data.EntryId)
|
cn := jobs.Remove(data.EntryId)
|
||||||
@@ -33,7 +33,7 @@ func (e *SysJob) RemoveJob(c *dto.GeneralDelDto) error {
|
|||||||
if res {
|
if res {
|
||||||
err = e.Orm.Table(data.TableName()).Where("entry_id = ?", data.EntryId).Update("entry_id", 0).Error
|
err = e.Orm.Table(data.TableName()).Where("entry_id = ?", data.EntryId).Update("entry_id", 0).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] db error:%#v", msgID, err)
|
log.Errorf("msgID[%s] db error:%s", msgID, err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error {
|
|||||||
msgID := e.MsgID
|
msgID := e.MsgID
|
||||||
err = e.Orm.Table(data.TableName()).First(&data, c.Id).Error
|
err = e.Orm.Table(data.TableName()).First(&data, c.Id).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] db error:%#v", msgID, err)
|
log.Errorf("msgID[%s] db error:%s", msgID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if data.JobType == 1 {
|
if data.JobType == 1 {
|
||||||
@@ -62,7 +62,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error {
|
|||||||
j.Name = data.JobName
|
j.Name = data.JobName
|
||||||
data.EntryId, err = jobs.AddJob(j)
|
data.EntryId, err = jobs.AddJob(j)
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
var j = &jobs.ExecJob{}
|
var j = &jobs.ExecJob{}
|
||||||
@@ -73,7 +73,7 @@ func (e *SysJob) StartJob(c *dto.GeneralGetDto) error {
|
|||||||
j.Args = data.Args
|
j.Args = data.Args
|
||||||
data.EntryId, err = jobs.AddJob(j)
|
data.EntryId, err = jobs.AddJob(j)
|
||||||
if err != nil {
|
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 {
|
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
|
err = e.Orm.Table(data.TableName()).Where(c.Id).Updates(&data).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("msgID[%s] db error:%#v", msgID, err)
|
log.Errorf("msgID[%s] db error:%s", msgID, err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/dto"
|
"go-admin/common/dto"
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/common/models"
|
"go-admin/common/models"
|
||||||
@@ -16,7 +15,7 @@ import (
|
|||||||
// CreateAction 通用新增动作
|
// CreateAction 通用新增动作
|
||||||
func CreateAction(control dto.Control) gin.HandlerFunc {
|
func CreateAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -39,7 +38,7 @@ func CreateAction(control dto.Control) gin.HandlerFunc {
|
|||||||
object.SetCreateBy(tools.GetUserIdUint(c))
|
object.SetCreateBy(tools.GetUserIdUint(c))
|
||||||
err = db.WithContext(c).Create(object).Error
|
err = db.WithContext(c).Create(object).Error
|
||||||
if err != nil {
|
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, "创建失败")
|
app.Error(c, http.StatusInternalServerError, err, "创建失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/dto"
|
"go-admin/common/dto"
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/common/models"
|
"go-admin/common/models"
|
||||||
@@ -16,7 +15,7 @@ import (
|
|||||||
// DeleteAction 通用删除动作
|
// DeleteAction 通用删除动作
|
||||||
func DeleteAction(control dto.Control) gin.HandlerFunc {
|
func DeleteAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -27,7 +26,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
|
|||||||
req := control.Generate()
|
req := control.Generate()
|
||||||
err = req.Bind(c)
|
err = req.Bind(c)
|
||||||
if err != nil {
|
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, "参数验证失败")
|
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -47,7 +46,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
|
|||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Where(req.GetId()).Delete(object)
|
).Where(req.GetId()).Delete(object)
|
||||||
if db.Error != nil {
|
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, "删除失败")
|
app.Error(c, http.StatusInternalServerError, err, "删除失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/dto"
|
"go-admin/common/dto"
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/common/models"
|
"go-admin/common/models"
|
||||||
@@ -18,7 +17,7 @@ import (
|
|||||||
// IndexAction 通用查询动作
|
// IndexAction 通用查询动作
|
||||||
func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.HandlerFunc {
|
func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -49,7 +48,7 @@ func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.H
|
|||||||
Find(list).Limit(-1).Offset(-1).
|
Find(list).Limit(-1).Offset(-1).
|
||||||
Count(&count).Error
|
Count(&count).Error
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
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, "查询失败")
|
app.Error(c, http.StatusInternalServerError, err, "查询失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
"go-admin/tools/app"
|
"go-admin/tools/app"
|
||||||
@@ -23,7 +22,7 @@ type dataPermission struct {
|
|||||||
|
|
||||||
func PermissionAction() gin.HandlerFunc {
|
func PermissionAction() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -34,7 +33,7 @@ func PermissionAction() gin.HandlerFunc {
|
|||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
if userId := tools.GetUserIdStr(c); userId != "" {
|
||||||
p, err = newDataPermission(db, userId)
|
p, err = newDataPermission(db, userId)
|
||||||
if err != nil {
|
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, "权限范围鉴定错误")
|
app.Error(c, http.StatusInternalServerError, err, "权限范围鉴定错误")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/dto"
|
"go-admin/common/dto"
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/common/models"
|
"go-admin/common/models"
|
||||||
@@ -16,7 +15,7 @@ import (
|
|||||||
// UpdateAction 通用更新动作
|
// UpdateAction 通用更新动作
|
||||||
func UpdateAction(control dto.Control) gin.HandlerFunc {
|
func UpdateAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -45,7 +44,7 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
|
|||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Where(req.GetId()).Updates(object)
|
).Where(req.GetId()).Updates(object)
|
||||||
if db.Error != nil {
|
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, "更新失败")
|
app.Error(c, http.StatusInternalServerError, err, "更新失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-8
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/common/apis"
|
|
||||||
"go-admin/common/dto"
|
"go-admin/common/dto"
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/common/models"
|
"go-admin/common/models"
|
||||||
@@ -16,9 +15,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ViewAction 通用详情动作
|
// 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) {
|
return func(c *gin.Context) {
|
||||||
db, err := apis.GetOrm(c)
|
db, err := tools.GetOrm(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
@@ -39,23 +38,30 @@ func ViewAction(control dto.Control, params ...interface{}) gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rsp interface{}
|
||||||
|
if f != nil {
|
||||||
|
rsp = f()
|
||||||
|
} else {
|
||||||
|
rsp, _ = req.GenerateM()
|
||||||
|
}
|
||||||
|
|
||||||
//数据权限检查
|
//数据权限检查
|
||||||
p := getPermissionFromContext(c)
|
p := getPermissionFromContext(c)
|
||||||
|
|
||||||
err = db.WithContext(c).Scopes(
|
err = db.Model(object).WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
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, "查看对象不存在或无权查看")
|
app.Error(c, http.StatusNotFound, nil, "查看对象不存在或无权查看")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
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, "查看失败")
|
app.Error(c, http.StatusInternalServerError, err, "查看失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.OK(c, object, "查看成功")
|
app.OK(c, rsp, "查看成功")
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-20
@@ -1,9 +1,6 @@
|
|||||||
package apis
|
package apis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
@@ -14,21 +11,5 @@ type Api struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) {
|
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) {
|
||||||
return GetOrm(c)
|
return tools.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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -73,3 +76,19 @@ func GenerateMsgIDFromContext(c *gin.Context) string {
|
|||||||
msgID = cast.ToString(data)
|
msgID = cast.ToString(data)
|
||||||
return msgID
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user