修改action和模版,使用log打印日志,修复job删除问题,已经所有删除问题

This commit is contained in:
linwenxiang committed 2020-09-22 22:04:29 +08:00
1 parent f3e0e4dbc6
commit 8460b3f385
17 files changed
+319 -314

No files matched your search

+45 -39
View File
@@ -1,55 +1,61 @@
package actions
import (
"errors"
dto2 "go-admin/common/dto"
"go-admin/common/models"
"go-admin/tools/app"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/common/apis"
"go-admin/common/dto"
"go-admin/common/log"
"go-admin/common/models"
"go-admin/tools"
"go-admin/tools/app"
)
// DeleteAction 通用删除动作
func DeleteAction(control dto2.Control) gin.HandlerFunc {
func DeleteAction(control dto.Control) gin.HandlerFunc {
return func(c *gin.Context) {
var err error
idb, exist := c.Get("db")
if !exist {
err = errors.New("db connect not exist")
tools.HasError(err, "", 500)
db, err := apis.GetOrm(c)
if err != nil {
log.Error(err)
return
}
switch idb.(type) {
case *gorm.DB:
//删除操作
db := idb.(*gorm.DB)
req := control.Generate()
err = req.Bind(c)
tools.HasError(err, "参数验证失败", 422)
var object models.ActiveRecord
object, err = req.GenerateM()
tools.HasError(err, "模型生成失败", 500)
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := getPermissionFromContext(c)
db = db.WithContext(c).Scopes(
Permission(object.TableName(), p),
).Where(req.GetId()).Delete(object)
tools.HasError(db.Error, "删除失败", 500)
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
tools.HasError(err, "", 403)
}
app.OK(c, object.GetId(), "删除成功")
c.Next()
default:
err = errors.New("db connect not exist")
tools.HasError(err, "", 500)
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := control.Generate()
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %#v", msgID, err)
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object models.ActiveRecord
object, err = req.GenerateM()
if err != nil {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := getPermissionFromContext(c)
db = db.WithContext(c).Scopes(
Permission(object.TableName(), p),
).Where(req.GetId()).Delete(object)
if db.Error != nil {
log.Errorf("MsgID[%s] Delete error: %#v", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "删除失败")
return
}
if db.RowsAffected == 0 {
app.Error(c, http.StatusForbidden, nil, "无权删除该数据")
return
}
app.OK(c, object.GetId(), "删除成功")
c.Next()
}
}