delete.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package actions
  2. import (
  3. "cold-logistics/common/dto"
  4. "cold-logistics/common/model"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  8. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  9. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  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 model.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. }