支持view自定义返回

This commit is contained in:
linwenxiang
2020-09-23 00:35:07 +08:00
parent 78bb246390
commit c0ac9a6397
12 changed files with 73 additions and 56 deletions
+2 -3
View File
@@ -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
}
+3 -4
View File
@@ -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
}
+2 -3
View File
@@ -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
}
+2 -3
View File
@@ -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
+2 -3
View File
@@ -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
}
+14 -8
View File
@@ -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()
}
}
+1 -20
View File
@@ -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)
}