api层写法统一

This commit is contained in:
linwenxiang committed 2021-03-12 16:02:29 +08:00
1 parent fa30b8db95
commit 9bf9330410
40 files changed
+430 -316

No files matched your search

+6 -6
View File
@@ -4,11 +4,11 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/api"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"go-admin/common/apis"
"go-admin/common/dto"
"go-admin/common/models"
)
@@ -16,7 +16,7 @@ import (
// CreateAction 通用新增动作
func CreateAction(control dto.Control) gin.HandlerFunc {
return func(c *gin.Context) {
log := apis.GetRequestLogger(c)
log := api.GetRequestLogger(c)
db, err := pkg.GetOrm(c)
if err != nil {
log.Error(err)
@@ -27,23 +27,23 @@ func CreateAction(control dto.Control) gin.HandlerFunc {
req := control.Generate()
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object models.ActiveRecord
object, err = req.GenerateM()
if err != nil {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
response.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetCreateBy(user.GetUserId(c))
err = db.WithContext(c).Create(object).Error
if err != nil {
log.Errorf("Create error: %s", err)
app.Error(c, http.StatusInternalServerError, err, "创建失败")
response.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
app.OK(c, object.GetId(), "创建成功")
response.OK(c, object.GetId(), "创建成功")
c.Next()
}
}
+5 -5
View File
@@ -28,13 +28,13 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object models.ActiveRecord
object, err = req.GenerateM()
if err != nil {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
response.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
@@ -48,14 +48,14 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
).Where(req.GetId()).Delete(object)
if db.Error != nil {
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "删除失败")
response.Error(c, http.StatusInternalServerError, err, "删除失败")
return
}
if db.RowsAffected == 0 {
app.Error(c, http.StatusForbidden, nil, "无权删除该数据")
response.Error(c, http.StatusForbidden, nil, "无权删除该数据")
return
}
app.OK(c, object.GetId(), "删除成功")
response.OK(c, object.GetId(), "删除成功")
c.Next()
}
}
+3 -3
View File
@@ -32,7 +32,7 @@ func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.H
//查询列表
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
@@ -49,10 +49,10 @@ func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.H
Count(&count).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Errorf("MsgID[%s] Index error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "查询失败")
response.Error(c, http.StatusInternalServerError, err, "查询失败")
return
}
app.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
response.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
c.Next()
}
}
+1 -1
View File
@@ -34,7 +34,7 @@ func PermissionAction() gin.HandlerFunc {
p, err = newDataPermission(db, userId)
if err != nil {
log.Errorf("MsgID[%s] PermissionAction error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "权限范围鉴定错误")
response.Error(c, http.StatusInternalServerError, err, "权限范围鉴定错误")
c.Abort()
return
}
+5 -5
View File
@@ -27,13 +27,13 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
//更新操作
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object models.ActiveRecord
object, err = req.GenerateM()
if err != nil {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
response.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(user.GetUserId(c))
@@ -46,14 +46,14 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
).Where(req.GetId()).Updates(object)
if db.Error != nil {
log.Errorf("MsgID[%s] Update error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "更新失败")
response.Error(c, http.StatusInternalServerError, err, "更新失败")
return
}
if db.RowsAffected == 0 {
app.Error(c, http.StatusForbidden, nil, "无权更新该数据")
response.Error(c, http.StatusForbidden, nil, "无权更新该数据")
return
}
app.OK(c, object.GetId(), "更新成功")
response.OK(c, object.GetId(), "更新成功")
c.Next()
}
}
+6 -6
View File
@@ -2,6 +2,7 @@ package actions
import (
"errors"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"net/http"
"github.com/gin-gonic/gin"
@@ -9,7 +10,6 @@ import (
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"gorm.io/gorm"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"go-admin/common/dto"
"go-admin/common/models"
)
@@ -28,13 +28,13 @@ func ViewAction(control dto.Control, f func() interface{}) gin.HandlerFunc {
req := control.Generate()
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object models.ActiveRecord
object, err = req.GenerateM()
if err != nil {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
response.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
@@ -53,15 +53,15 @@ func ViewAction(control dto.Control, f func() interface{}) gin.HandlerFunc {
).Where(req.GetId()).First(rsp).Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
app.Error(c, http.StatusNotFound, nil, "查看对象不存在或无权查看")
response.Error(c, http.StatusNotFound, nil, "查看对象不存在或无权查看")
return
}
if err != nil {
log.Errorf("MsgID[%s] View error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "查看失败")
response.Error(c, http.StatusInternalServerError, err, "查看失败")
return
}
app.OK(c, rsp, "查看成功")
response.OK(c, rsp, "查看成功")
c.Next()
}
}
+3 -2
View File
@@ -2,10 +2,11 @@ package apis
import (
"encoding/json"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/api"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"github.com/go-admin-team/go-admin-core/sdk/pkg/logger"
"gorm.io/gorm"
@@ -17,7 +18,7 @@ type Api struct {
// GetLogger 获取上下文提供的日志
func (e *Api) GetLogger(c *gin.Context) *logger.Logger {
return GetRequestLogger(c)
return api.GetRequestLogger(c)
}
// GetOrm 获取Orm DB
+2 -3
View File
@@ -4,8 +4,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"go-admin/common/apis"
"github.com/go-admin-team/go-admin-core/sdk/api"
)
type ObjectById struct {
@@ -15,7 +14,7 @@ type ObjectById struct {
func (s *ObjectById) Bind(ctx *gin.Context) error {
var err error
log := apis.GetRequestLogger(ctx)
log := api.GetRequestLogger(ctx)
err = ctx.ShouldBindUri(s)
if err != nil {
log.Warnf("ShouldBindUri error: %s", err.Error())
+2 -2
View File
@@ -4,11 +4,11 @@ import (
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk"
"go-admin/common/apis"
"github.com/go-admin-team/go-admin-core/sdk/api"
)
func LoadPolicy(c *gin.Context) (*casbin.SyncedEnforcer, error) {
log := apis.GetRequestLogger(c)
log := api.GetRequestLogger(c)
if err := sdk.Runtime.GetCasbinKey(c.Request.Host).LoadPolicy(); err == nil {
return sdk.Runtime.GetCasbinKey(c.Request.Host), err
} else {
+3 -4
View File
@@ -5,16 +5,15 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk"
"github.com/go-admin-team/go-admin-core/sdk/api"
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"go-admin/common/apis"
)
//权限检查中间件
func AuthCheckRole() gin.HandlerFunc {
return func(c *gin.Context) {
log := apis.GetRequestLogger(c)
log := api.GetRequestLogger(c)
data, _ := c.Get(jwtauth.JwtPayloadKey)
v := data.(jwtauth.MapClaims)
e := sdk.Runtime.GetCasbinKey(c.Request.Host)
@@ -28,7 +27,7 @@ func AuthCheckRole() gin.HandlerFunc {
res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
if err != nil {
log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path)
app.Error(c, 500, err, "")
response.Error(c, 500, err, "")
return
}
}