try_api.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package access_log
  2. import (
  3. "context"
  4. "gd_access_log/apis"
  5. "gd_access_log/common.in/utils"
  6. "gd_access_log/errors"
  7. "github.com/astaxie/beego/orm"
  8. "github.com/tidwall/gjson"
  9. "go.uber.org/zap"
  10. "strconv"
  11. "time"
  12. )
  13. func TryApiAccessLogAdd(ctx context.Context, req *apis.TryApiAccessLogReq, reply *apis.TryApiAccessLogReply) error {
  14. data := make([]apis.TryApiAccessLog, len(req.RequestParams))
  15. now := time.Now().Format("2006-01-02 15:04:05")
  16. for k := range req.ResponseParams {
  17. data[k].ApiId = req.ApiId
  18. data[k].ApiName = req.ApiName
  19. data[k].RequestParams = req.RequestParams[k]
  20. data[k].ResponseParams = req.ResponseParams[k]
  21. codeResult := gjson.GetBytes([]byte(req.ResponseParams[k]), req.Code)
  22. if codeResult.Type == 2 {
  23. data[k].Code = codeResult.Int()
  24. } else if codeResult.Type == 3 {
  25. code := codeResult.String()
  26. data[k].Code, _ = strconv.ParseInt(code, 10, 64)
  27. }
  28. msgResult := gjson.GetBytes([]byte(req.ResponseParams[k]), req.Msg)
  29. if req.Msg != "" && msgResult.Exists() {
  30. data[k].Msg = msgResult.String()
  31. } else {
  32. data[k].Msg = ""
  33. }
  34. data[k].CreateTime = now
  35. }
  36. if _, err := orm.NewOrm().InsertMulti(1, data); err != nil {
  37. l.Error("mysql",
  38. zap.String("sql", "Insert t_gd_try_api_access_log"),
  39. zap.String("fields", utils.MarshalJsonString(req)),
  40. zap.String("error", err.Error()))
  41. return errors.DataBaseError
  42. }
  43. return nil
  44. }