123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package access_log
- import (
- "context"
- "gd_access_log/apis"
- "gd_access_log/common.in/utils"
- "gd_access_log/errors"
- "github.com/astaxie/beego/orm"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- "strconv"
- "time"
- )
- func TryApiAccessLogAdd(ctx context.Context, req *apis.TryApiAccessLogReq, reply *apis.TryApiAccessLogReply) error {
- data := make([]apis.TryApiAccessLog, len(req.RequestParams))
- now := time.Now().Format("2006-01-02 15:04:05")
- for k := range req.ResponseParams {
- data[k].ApiId = req.ApiId
- data[k].ApiName = req.ApiName
- data[k].RequestParams = req.RequestParams[k]
- data[k].ResponseParams = req.ResponseParams[k]
- codeResult := gjson.GetBytes([]byte(req.ResponseParams[k]), req.Code)
- if codeResult.Type == 2 {
- data[k].Code = codeResult.Int()
- } else if codeResult.Type == 3 {
- code := codeResult.String()
- data[k].Code, _ = strconv.ParseInt(code, 10, 64)
- }
- msgResult := gjson.GetBytes([]byte(req.ResponseParams[k]), req.Msg)
- if req.Msg != "" && msgResult.Exists() {
- data[k].Msg = msgResult.String()
- } else {
- data[k].Msg = ""
- }
- data[k].CreateTime = now
- }
- if _, err := orm.NewOrm().InsertMulti(1, data); err != nil {
- l.Error("mysql",
- zap.String("sql", "Insert t_gd_try_api_access_log"),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
|