operation_log.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package v1
  2. import (
  3. "context"
  4. "cp-organization-management-gateway/errors"
  5. param_v1 "cp-organization-management-gateway/param/v1"
  6. "cp-organization-management-gateway/pb"
  7. "cp-organization-management-gateway/pb/v1"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jaryhe/gopkgs/tasker/httptasker"
  10. "github.com/jaryhe/gopkgs/util"
  11. "net/http"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. "go.uber.org/zap"
  15. )
  16. //
  17. // @Summary 获取日志列表
  18. // @Description 获取日志列表
  19. // @Tags 操作日志
  20. // @Accept json
  21. // @Produce json
  22. // @Param token header string true "token"
  23. // @Param page query int64 false " "
  24. // @Param page_size query int64 false " "
  25. // @Success 200 {object} v1.ManagementLogListResponse
  26. // @Failure 500 {object} base.HTTPError
  27. // @Router /api/v1/log/list [get]
  28. func (c *Controller) ManagementLogList(ctx *gin.Context) {
  29. // 解析参数
  30. req := &param_v1.ManagementLogListRequest{}
  31. parseParamTask := func() error {
  32. err := util.ShouldBind(ctx, &req.Header, nil, &req.ManagementLogListQuery, nil)
  33. if err != nil {
  34. logger.Error("func",
  35. zap.String("call", "util.ShouldBind"),
  36. zap.String("error", err.Error()))
  37. return errors.ParamsError
  38. }
  39. return nil
  40. }
  41. // 业务处理
  42. handleServiceTask := func() error {
  43. // 响应数据
  44. resp := param_v1.ManagementLogListResponse{}
  45. rpcReq := &v1.ManagementLogListRequest{
  46. Page:req.Page,
  47. PageSize:req.PageSize,
  48. }
  49. rpcRsp, err := pb.Organization.ManagementLogList(ctx, rpcReq)
  50. if err != nil {
  51. s, _ := json.MarshalToString(req)
  52. logger.Error("func",
  53. zap.String("call", "pb.Organization.ManagementLogList"),
  54. zap.String("params", s),
  55. zap.String("error", err.Error()))
  56. return errors.ErrorTransForm(err)
  57. }
  58. if rpcRsp.List == nil {
  59. rpcRsp.List = make([]*v1.ManagementLogItem, 0)
  60. }
  61. resp.Data = *rpcRsp
  62. ctx.JSON(http.StatusOK, resp)
  63. return nil
  64. }
  65. // 执行任务
  66. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  67. }
  68. type OperationLogRequest struct {
  69. Module string
  70. Action string
  71. UserName string
  72. Uid int64
  73. Origin interface{}
  74. Target interface{}
  75. OrganizationCode string
  76. }
  77. func OperationLogAdd(req *OperationLogRequest) {
  78. mreq := v1.ManagementLogAddRequest{
  79. Module:req.Module,
  80. Action:req.Action,
  81. Username:req.UserName,
  82. Uid:req.Uid,
  83. CreatedAt:time.Now().Unix(),
  84. OrganizationCode:req.OrganizationCode,
  85. }
  86. if req.Origin != nil {
  87. origin, _ := json.Marshal(req.Origin)
  88. mreq.Origin = string(origin)
  89. }
  90. if req.Target != nil {
  91. target, _ := json.Marshal(req.Target)
  92. mreq.Target = string(target)
  93. }
  94. _, err := pb.Organization.ManagementLogAdd(context.Background(), &mreq)
  95. if err != nil {
  96. s, _ := json.Marshal(mreq)
  97. logger.Error("func",
  98. zap.String("call", "pb.Organization.ManagementLogAdd"),
  99. zap.String("params", string(s)),
  100. zap.String("error", err.Error()))
  101. }
  102. }