callback.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package access_log
  4. import (
  5. "context"
  6. "gd_access_log/apis"
  7. "gd_access_log/errors"
  8. "gd_access_log/impl/dbmodel"
  9. "fmt"
  10. "time"
  11. "gd_access_log/common.in/utils"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. )
  15. func LogCallbackIn(ctx context.Context, req *apis.LogCallbackInReq, reply *apis.LogCallbackInReply) (err error) {
  16. // 参数检查
  17. if req.ServiceType == 0 || req.SourceName == "" {
  18. return errors.ArgsError
  19. }
  20. // 模型数据填充
  21. cr := dbmodel.CallbackIn{
  22. ServiceType: req.ServiceType,
  23. SourceName: req.SourceName,
  24. SourceObjectId: req.SourceObjectId,
  25. RequestCode: req.RequestCode,
  26. RequestParams: req.RequestParams,
  27. ResponseParams: req.ResponseParams,
  28. Elapsed: req.Elapsed,
  29. AddTime: time.Now().Unix(),
  30. }
  31. // 创建一条记录
  32. if _, err := cr.Create(orm.NewOrm()); err != nil {
  33. l.Error("mysql",
  34. zap.String("sql", fmt.Sprintf("INSERT INTO %s", cr.TableName())),
  35. zap.String("fields", utils.MarshalJsonString(cr)),
  36. zap.String("error", err.Error()))
  37. return err
  38. }
  39. return nil
  40. }
  41. func LogCallbackOut(ctx context.Context, req *apis.LogCallbackOutReq, reply *apis.LogCallbackOutReply) (err error) {
  42. // 参数检查
  43. if req.ServiceType == 0 || req.MerchantId == 0 {
  44. return errors.ArgsError
  45. }
  46. // 模型数据填充
  47. cs := dbmodel.CallbackOut{
  48. ServiceType: req.ServiceType,
  49. MerchantId: req.MerchantId,
  50. ObjectId: req.ObjectId,
  51. RequestCode: req.RequestCode,
  52. RequestParams: req.RequestParams,
  53. ResponseParams: req.ResponseParams,
  54. Elapsed: req.Elapsed,
  55. AddTime: time.Now().Unix(),
  56. IsReuse: req.IsReuse,
  57. Search: req.Search,
  58. }
  59. // 创建一条记录
  60. if _, err := cs.Create(orm.NewOrm()); err != nil {
  61. l.Error("mysql",
  62. zap.String("sql", fmt.Sprintf("INSERT INTO %s", cs.TableName())),
  63. zap.String("fields", utils.MarshalJsonString(cs)),
  64. zap.String("error", err.Error()))
  65. return err
  66. }
  67. return nil
  68. }