center.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package thirdparty_impl
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "gd_service/consts"
  8. "gd_service/impl/thirdparty_impl/adm"
  9. "gd_service/impl/thirdparty_impl/cdbd"
  10. "gd_service/impl/thirdparty_impl/dy"
  11. "gd_service/impl/thirdparty_impl/dybd"
  12. "gd_service/impl/thirdparty_impl/zr"
  13. //"fmt"
  14. "gd_service/apis"
  15. "gd_service/errors"
  16. dutils "gd_service/utils"
  17. "strconv"
  18. "time"
  19. "github.com/afex/hystrix-go/hystrix"
  20. //"gd_service/common.in/jsonrpc2"
  21. //"go.uber.org/zap"
  22. )
  23. type ThirdpartyFunc struct {
  24. LocalFuc func(req *apis.ThirdpartRequest) (reply string, err error)
  25. Function func(req *apis.ThirdpartRequest) (reply string, err error)
  26. ExistLocalFunc bool
  27. ReuseFlag bool
  28. SearchKey string
  29. }
  30. var ThirdpartyFuncMap = map[string]ThirdpartyFunc{
  31. dutils.ADMA01: {nil, adm.A01, false, true,"plate_no"},
  32. dutils.ADMA02: {nil, adm.A02, false, true,"vin"},
  33. dutils.ADMA04: {nil, adm.A04, false, true,"plate_no"},
  34. dutils.ADMA05: {nil, adm.A05, false, true,"vin"},
  35. dutils.ADMIS01: {nil, adm.IS01, false, true,"vin"},
  36. dutils.ADMU01: {nil, adm.U01, false, true,""},
  37. dutils.ADMV01: {nil, adm.V01, false, true,"plate_no"},
  38. dutils.ADMV02: {nil, adm.V02, false, true,"id_card"},
  39. dutils.ADMF01: {nil, adm.F01, false, true,""},
  40. dutils.ADMX01: {nil, adm.X01, false, true,"degree_id"},
  41. dutils.ADMX02: {nil, adm.X02, false, true,"education_id"},
  42. dutils.ADMX03: {nil, adm.X03, false, true,"id_card"},
  43. dutils.CDBDTWOELEMENTVERIFY: {cdbd.TwoElementVerifyLocal, cdbd.TwoElementVerify, true, false,"id_card"},
  44. dutils.DYTWOELEMENTVERIFY: {dy.TwoElementVerifyLocal, dy.TwoElementVerify, true, false,"id_card"},
  45. dutils.ZRTWOELEMENTVERIFY: {zr.TwoElementVerifyGuangdongLocal, zr.TwoElementVerifyGuangdong, true, false,"plateNo"},
  46. dutils.ZRTWOELEMENTVERIFYNATIONAL: {zr.TwoElementVerifyNationalLocal, zr.TwoElementVerifyNational, true, false,"plateNum"},
  47. dutils.DYXW: {dybd.DybdX001Local, dybd.DybdX001, true, false,consts.DyBdX001P1},
  48. dutils.DYXL: {dybd.DybdX002Local, dybd.DybdX002, true, false,consts.DyBdX002P1},
  49. dutils.DYXLALL: {dybd.DybdX003Local, dybd.DybdX003, true, false,consts.DyBdX003P1},
  50. }
  51. func CheckProviderApiIsAvaliable(code string, providerMap map[string]apis.MerchantProviderLimitInfo, rangeNo int) (apis.MerchantProviderLimitInfo, error) {
  52. v, ok := providerMap[code+strconv.Itoa(rangeNo)]
  53. if ok {
  54. ret, _ := dutils.CheckProviderAvailable(v)
  55. if ret == false {
  56. return v, errors.ProviderUnavailable
  57. }
  58. } else {
  59. return v, errors.ProviderApiNotFound
  60. }
  61. return v, nil
  62. }
  63. func CallThirdparty(req *apis.ThirdpartRequest) (reply string, err error) {
  64. defer func() {
  65. if r := recover(); r != nil {
  66. if e, ok := r.(error); ok {
  67. err = e
  68. } else {
  69. err = fmt.Errorf("%+v", r)
  70. }
  71. }
  72. }()
  73. provider, err := CheckProviderApiIsAvaliable(req.ProviderApiCode, req.ProviderMap, req.RangeNo)
  74. if err != nil {
  75. return "", err
  76. }
  77. req.ProviderInfo = provider
  78. if v, ok := ThirdpartyFuncMap[req.ProviderApiCode]; ok {
  79. // 存在本地复用函数
  80. if v.ExistLocalFunc {
  81. if req.Req.ReuseTime > 0 {
  82. reply, err = v.LocalFuc(req)
  83. if err == nil || err == errors.NoRecord{
  84. return reply, err
  85. }
  86. }
  87. }
  88. params, _ := json.Marshal(req.Params)
  89. req.LReq = &apis.ThirdpartLogWrite{ProviderApiId: provider.ProviderApiId}
  90. // 标志设为true表示调用该数据源为复用
  91. if v.ReuseFlag {
  92. req.LReq.ProviderCode = provider.ProviderApiCode
  93. }
  94. req.LReq.RequestParams = string(params)
  95. startTime := uint64(time.Now().UnixNano())
  96. if v.SearchKey != ""{
  97. req.LReq.Search = req.Params[v.SearchKey]
  98. }
  99. defer dutils.AppendLog(&req.Reply.LReq, req.LReq, startTime)
  100. // 判断是否开启熔断
  101. hystrixConf, ok := dutils.CheckProviderApiHystrix(req.ProviderApiCode)
  102. if ok {
  103. hystrixErr := hystrix.Do(req.ProviderApiCode, func() error {
  104. reply, err = v.Function(req)
  105. if err == errors.VendorError {
  106. return err
  107. }
  108. return nil
  109. }, nil)
  110. if hystrixErr == hystrix.ErrCircuitOpen {
  111. dutils.SetAccessLogReqCodeMsgState(req.LReq, 1101, "gd_1101", "接口熔断", false)
  112. }
  113. if hystrixErr != nil && err == nil {
  114. err = errors.VendorError
  115. }
  116. go dutils.CheckHystrixStatus(hystrixConf, hystrixErr, req.LReq.ResponseParams)
  117. } else {
  118. reply, err = v.Function(req)
  119. }
  120. return reply, err
  121. }
  122. return
  123. }