// Copyright 2019 autocareai.com. All rights reserved. // Use of this source code is governed by autocareai.com. package thirdparty_impl import ( "encoding/json" "fmt" "gd_service/consts" "gd_service/impl/thirdparty_impl/adm" "gd_service/impl/thirdparty_impl/cdbd" "gd_service/impl/thirdparty_impl/dy" "gd_service/impl/thirdparty_impl/dybd" "gd_service/impl/thirdparty_impl/zr" //"fmt" "gd_service/apis" "gd_service/errors" dutils "gd_service/utils" "strconv" "time" "github.com/afex/hystrix-go/hystrix" //"gd_service/common.in/jsonrpc2" //"go.uber.org/zap" ) type ThirdpartyFunc struct { LocalFuc func(req *apis.ThirdpartRequest) (reply string, err error) Function func(req *apis.ThirdpartRequest) (reply string, err error) ExistLocalFunc bool ReuseFlag bool SearchKey string } var ThirdpartyFuncMap = map[string]ThirdpartyFunc{ dutils.ADMA01: {nil, adm.A01, false, true,"plate_no"}, dutils.ADMA02: {nil, adm.A02, false, true,"vin"}, dutils.ADMA04: {nil, adm.A04, false, true,"plate_no"}, dutils.ADMA05: {nil, adm.A05, false, true,"vin"}, dutils.ADMIS01: {nil, adm.IS01, false, true,"vin"}, dutils.ADMU01: {nil, adm.U01, false, true,""}, dutils.ADMV01: {nil, adm.V01, false, true,"plate_no"}, dutils.ADMV02: {nil, adm.V02, false, true,"id_card"}, dutils.ADMF01: {nil, adm.F01, false, true,""}, dutils.ADMX01: {nil, adm.X01, false, true,"degree_id"}, dutils.ADMX02: {nil, adm.X02, false, true,"education_id"}, dutils.ADMX03: {nil, adm.X03, false, true,"id_card"}, dutils.CDBDTWOELEMENTVERIFY: {cdbd.TwoElementVerifyLocal, cdbd.TwoElementVerify, true, false,"id_card"}, dutils.DYTWOELEMENTVERIFY: {dy.TwoElementVerifyLocal, dy.TwoElementVerify, true, false,"id_card"}, dutils.ZRTWOELEMENTVERIFY: {zr.TwoElementVerifyGuangdongLocal, zr.TwoElementVerifyGuangdong, true, false,"plateNo"}, dutils.ZRTWOELEMENTVERIFYNATIONAL: {zr.TwoElementVerifyNationalLocal, zr.TwoElementVerifyNational, true, false,"plateNum"}, dutils.DYXW: {dybd.DybdX001Local, dybd.DybdX001, true, false,consts.DyBdX001P1}, dutils.DYXL: {dybd.DybdX002Local, dybd.DybdX002, true, false,consts.DyBdX002P1}, dutils.DYXLALL: {dybd.DybdX003Local, dybd.DybdX003, true, false,consts.DyBdX003P1}, } func CheckProviderApiIsAvaliable(code string, providerMap map[string]apis.MerchantProviderLimitInfo, rangeNo int) (apis.MerchantProviderLimitInfo, error) { v, ok := providerMap[code+strconv.Itoa(rangeNo)] if ok { ret, _ := dutils.CheckProviderAvailable(v) if ret == false { return v, errors.ProviderUnavailable } } else { return v, errors.ProviderApiNotFound } return v, nil } func CallThirdparty(req *apis.ThirdpartRequest) (reply string, err error) { defer func() { if r := recover(); r != nil { if e, ok := r.(error); ok { err = e } else { err = fmt.Errorf("%+v", r) } } }() provider, err := CheckProviderApiIsAvaliable(req.ProviderApiCode, req.ProviderMap, req.RangeNo) if err != nil { return "", err } req.ProviderInfo = provider if v, ok := ThirdpartyFuncMap[req.ProviderApiCode]; ok { // 存在本地复用函数 if v.ExistLocalFunc { if req.Req.ReuseTime > 0 { reply, err = v.LocalFuc(req) if err == nil || err == errors.NoRecord{ return reply, err } } } params, _ := json.Marshal(req.Params) req.LReq = &apis.ThirdpartLogWrite{ProviderApiId: provider.ProviderApiId} // 标志设为true表示调用该数据源为复用 if v.ReuseFlag { req.LReq.ProviderCode = provider.ProviderApiCode } req.LReq.RequestParams = string(params) startTime := uint64(time.Now().UnixNano()) if v.SearchKey != ""{ req.LReq.Search = req.Params[v.SearchKey] } defer dutils.AppendLog(&req.Reply.LReq, req.LReq, startTime) // 判断是否开启熔断 hystrixConf, ok := dutils.CheckProviderApiHystrix(req.ProviderApiCode) if ok { hystrixErr := hystrix.Do(req.ProviderApiCode, func() error { reply, err = v.Function(req) if err == errors.VendorError { return err } return nil }, nil) if hystrixErr == hystrix.ErrCircuitOpen { dutils.SetAccessLogReqCodeMsgState(req.LReq, 1101, "gd_1101", "接口熔断", false) } if hystrixErr != nil && err == nil { err = errors.VendorError } go dutils.CheckHystrixStatus(hystrixConf, hystrixErr, req.LReq.ResponseParams) } else { reply, err = v.Function(req) } return reply, err } return }