base_api_get_by_router.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package base_api
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/errors"
  8. "encoding/json"
  9. "github.com/astaxie/beego/orm"
  10. )
  11. func getParam(param []apis.ManagementBaseApiParam,respMap map[string]string){
  12. for _,v := range param{
  13. if v.Mean != ""{
  14. respMap[v.Name] = v.Mean
  15. if v.In != ""{
  16. respMap[v.In] = v.Mean
  17. }
  18. }
  19. if len(v.Child) > 0 {
  20. getParam(v.Child,respMap)
  21. }
  22. }
  23. }
  24. func ManagementGetBaseApiByRouter(ctx context.Context, req *apis.ManagementGetBaseApiByRouterReq, reply *apis.ManagementGetBaseApiByRouterReply) error {
  25. var requestParam string
  26. var responseParam string
  27. apiId :=req.ApiId
  28. merchantId := req.MerchantId
  29. if apiId <=0 {
  30. err := orm.NewOrm().Raw("select id from t_gd_api where router = ? and method = ?", req.Router, req.Method).QueryRow(&apiId)
  31. if err != nil {
  32. if err != orm.ErrNoRows {
  33. return errors.DataBaseError
  34. } else {
  35. return errors.ServiceError
  36. }
  37. }
  38. }
  39. if merchantId <= 0 {
  40. err := orm.NewOrm().Raw("select id from t_gd_merchants where app_key = ?", req.AppKey).QueryRow(&merchantId)
  41. if err != nil {
  42. if err != orm.ErrNoRows {
  43. return errors.DataBaseError
  44. } else {
  45. return errors.ServiceError
  46. }
  47. }
  48. }
  49. err := orm.NewOrm().Raw("select a.request_param, a.response_param from t_gd_merchant_child_data_api as a inner join t_gd_merchant_data_api as b on a.merchant_data_api_id=b.id where a.api_id=? and b.merchant_id=?",apiId,merchantId).QueryRow(&requestParam,&responseParam)
  50. if err != nil {
  51. if err != orm.ErrNoRows {
  52. return errors.DataBaseError
  53. } else {
  54. return errors.ServiceError
  55. }
  56. }
  57. var RequestParam []apis.ManagementBaseApiParam
  58. var ResponseParam []apis.ManagementBaseApiParam
  59. json.Unmarshal([]byte(requestParam), &RequestParam)
  60. json.Unmarshal([]byte(responseParam), &ResponseParam)
  61. reply.Data = make(map[string]string)
  62. getParam(RequestParam,reply.Data)
  63. getParam(ResponseParam,reply.Data)
  64. reply.Data["origin"]= "原始响应数据"
  65. reply.Data["code"]= "错误码"
  66. reply.Data["msg"]= "错误消息"
  67. return nil
  68. }