base_api_add.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "gd_management/common.in/storage"
  11. "gd_management/common.in/utils"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. )
  15. /*
  16. Id int
  17. ApiType string
  18. Method string `json:"method" description:"方法,GET POST等"`
  19. Router string `json:"router" description:"路由"`
  20. RequestParam string `json:"request_param" description:"请求参数"`
  21. ResponseParam string `json:"response_param" description:"响应参数"`
  22. ProviderApiIds string `json:"provider_api_ids" description:"三方api列表 "`
  23. Enable byte
  24. ErrorCodeIds string
  25. */
  26. func paramPreProcess(baseinfo *apis.ManagementBaseApiInfo) {
  27. for _, v := range baseinfo.RequestParam {
  28. v.Selected = v.Required
  29. }
  30. for _, v := range baseinfo.ResponseParam {
  31. v.Selected = v.Required
  32. }
  33. }
  34. func addBaseApiReqToDBStruct(req *apis.ManagementAddBaseApiReq) (*apis.TGdApi, error) {
  35. paramPreProcess(&req.BaseApiInfo)
  36. reqParam, err := json.Marshal(req.BaseApiInfo.RequestParam)
  37. if err != nil {
  38. return nil, err
  39. }
  40. resParam, err := json.Marshal(req.BaseApiInfo.ResponseParam)
  41. if err != nil {
  42. return nil, err
  43. }
  44. errids := ""
  45. for _, id := range req.BaseApiInfo.ErrorCode {
  46. if errids == "" {
  47. errids = fmt.Sprintf("%d", id)
  48. } else {
  49. errids = errids + "," + fmt.Sprintf("%d", id)
  50. }
  51. }
  52. now := time.Now().Format("2006-01-02 15:04:05")
  53. ret := apis.TGdApi{}
  54. ret.ApiType = req.BaseApiInfo.Type
  55. ret.Method = strings.ToUpper(req.BaseApiInfo.Method)
  56. ret.Router = req.BaseApiInfo.Router
  57. ret.Enable = true
  58. ret.Name = req.BaseApiInfo.Name
  59. ret.ErrorCodeIds = errids
  60. ret.RequestParam = string(reqParam)
  61. ret.ResponseParam = string(resParam)
  62. ret.CreateTime = now
  63. ret.UpdateTime = now
  64. ret.Comment = req.BaseApiInfo.Comment
  65. ret.IsShow = true
  66. if strings.HasPrefix(ret.Router, "/") == false {
  67. ret.Router = fmt.Sprintf("/%s", ret.Router)
  68. }
  69. return &ret, nil
  70. }
  71. func addBaseApi(req *apis.ManagementAddBaseApiReq) (int64, error) {
  72. var id int64
  73. task := func(o orm.Ormer) error {
  74. dbstruct, err := addBaseApiReqToDBStruct(req)
  75. if err != nil {
  76. return errors.ArgsError
  77. }
  78. exist := o.QueryTable("t_gd_api").Filter("router", dbstruct.Router).Filter("method", dbstruct.Method).Exist()
  79. if exist {
  80. return errors.BaseApiExist
  81. }
  82. id, err = o.Insert(dbstruct)
  83. if err != nil {
  84. fmt.Printf("database err:%v\n", err)
  85. return errors.DataBaseError
  86. }
  87. err = utils.RedisSet("t_gd_api", dbstruct.Method+"-"+dbstruct.Router, dbstruct)
  88. if err != nil {
  89. }
  90. return nil
  91. }
  92. tasks := []storage.DbaTasker{}
  93. tasks = append(tasks, storage.GenerateDbaTask(task))
  94. storage.ExecTrans(tasks...)
  95. return id, nil
  96. }
  97. func ManagementAddBaseApi(ctx context.Context, req *apis.ManagementAddBaseApiReq, reply *apis.ManagementAddBaseApiReply) (err error) {
  98. id, err := addBaseApi(req)
  99. if err != nil {
  100. l.Error("func",
  101. zap.String("call", "ManagementAddBaseApi"),
  102. zap.String("args", utils.MarshalJsonString(req)),
  103. zap.String("error", err.Error()))
  104. }
  105. reply.ApiId = id
  106. l.Debug(utils.MarshalJsonString(req, reply))
  107. return
  108. }