delete.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package actions
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. log "github.com/go-admin-team/go-admin-core/logger"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  8. "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  9. "git.cestong.com.cn/cecf/hx-admin-server/common/dto"
  10. "git.cestong.com.cn/cecf/hx-admin-server/common/models"
  11. )
  12. // DeleteAction 通用删除动作
  13. func DeleteAction(control dto.Control) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. db, err := pkg.GetOrm(c)
  16. if err != nil {
  17. log.Error(err)
  18. return
  19. }
  20. msgID := pkg.GenerateMsgIDFromContext(c)
  21. //删除操作
  22. req := control.Generate()
  23. err = req.Bind(c)
  24. if err != nil {
  25. log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
  26. response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
  27. return
  28. }
  29. var object models.ActiveRecord
  30. object, err = req.GenerateM()
  31. if err != nil {
  32. response.Error(c, 500, err, "模型生成失败")
  33. return
  34. }
  35. object.SetUpdateBy(user.GetUserId(c))
  36. //数据权限检查
  37. p := GetPermissionFromContext(c)
  38. db = db.WithContext(c).Scopes(
  39. Permission(object.TableName(), p),
  40. ).Where(req.GetId()).Delete(object)
  41. if db.Error != nil {
  42. log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
  43. response.Error(c, 500, err, "删除失败")
  44. return
  45. }
  46. if db.RowsAffected == 0 {
  47. response.Error(c, http.StatusForbidden, nil, "无权删除该数据")
  48. return
  49. }
  50. response.OK(c, object.GetId(), "删除成功")
  51. c.Next()
  52. }
  53. }