view.go 1.5 KB

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