view.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package actions
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  6. "github.com/gin-gonic/gin"
  7. log "github.com/go-admin-team/go-admin-core/logger"
  8. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  9. "gorm.io/gorm"
  10. "git.cestong.com.cn/cecf/hx-admin-server/common/dto"
  11. "git.cestong.com.cn/cecf/hx-admin-server/common/models"
  12. )
  13. // ViewAction 通用详情动作
  14. func ViewAction(control dto.Control, f func() interface{}) gin.HandlerFunc {
  15. return func(c *gin.Context) {
  16. db, err := pkg.GetOrm(c)
  17. if err != nil {
  18. log.Error(err)
  19. return
  20. }
  21. msgID := pkg.GenerateMsgIDFromContext(c)
  22. //查看详情
  23. req := control.Generate()
  24. err = req.Bind(c)
  25. if err != nil {
  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. var rsp interface{}
  36. if f != nil {
  37. rsp = f()
  38. } else {
  39. rsp, _ = req.GenerateM()
  40. }
  41. //数据权限检查
  42. p := GetPermissionFromContext(c)
  43. err = db.Model(object).WithContext(c).Scopes(
  44. Permission(object.TableName(), p),
  45. ).Where(req.GetId()).First(rsp).Error
  46. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  47. response.Error(c, http.StatusNotFound, nil, "查看对象不存在或无权查看")
  48. return
  49. }
  50. if err != nil {
  51. log.Errorf("MsgID[%s] View error: %s", msgID, err)
  52. response.Error(c, 500, err, "查看失败")
  53. return
  54. }
  55. response.OK(c, rsp, "查询成功")
  56. c.Next()
  57. }
  58. }