|
- package services
-
- import (
- "encoding/json"
- "fmt"
- "github.com/tal-tech/go-zero/core/logx"
- "job_risk_third/config"
- "job_risk_third/internal/proto"
- "job_risk_third/model"
- "job_risk_third/pkg/utils"
- )
-
- //职务风险防范系统第三方请求业务模块
- type JobRiskThirdServices interface {
- //PushFeatureDeal 接收风险特征处置结果接口
- PushFeatureDeal(req proto.PushFeatureDealRequest) error
- //PushUserInfo 获取用户信息接口
- PushUserInfo(req proto.PushUserInfoRequest) error
- //PushFeatureInfo 推送的风险结果信息
- PushFeatureInfo(req proto.PushFeatureInfoRequest) error
- // HealthCheck check service health status
- HealthCheck() bool
- }
-
- type ServiceMiddleware func(JobRiskThirdServices) JobRiskThirdServices
-
- type JobRiskThirdService struct {
- }
-
- // HealthCheck implement Service method
- // 用于检查服务的健康状态,这里仅仅返回true。
- func (s JobRiskThirdService) HealthCheck() bool {
- return true
- }
-
- //PushFeatureDeal 接收风险特征处置结果接口
- func (s JobRiskThirdService) PushFeatureDeal(req proto.PushFeatureDealRequest) error {
- jsBody, _ := json.Marshal(req)
- body, err := RequestSign(jsBody, proto.PushFeatureDeal)
- if err != nil {
- return err
- }
- res := new(proto.ZyResponse)
- err = json.Unmarshal(body, res)
- if err != nil {
- logx.Errorf("PushFeatureDeal call error: %s", err)
- return err
- }
- if res.Status != 200 {
- logx.Errorf("PushFeatureDeal call error: %s", res.Message)
- return fmt.Errorf(res.Message)
- }
- return nil
- }
-
- //PushUserInfo 获取用户信息接口
- func (s JobRiskThirdService) PushUserInfo(req proto.PushUserInfoRequest) error {
- jsBody, _ := json.Marshal(req)
- body, err := RequestSign(jsBody, proto.PushUserInfo)
- if err != nil {
- return err
- }
- res := new(proto.ZyResponse)
- err = json.Unmarshal(body, res)
- if err != nil {
- logx.Errorf("PushUserInfo call error: %s", err)
- return err
- }
- if res.Status != 200 {
- logx.Errorf("PushUserInfo call error: %s", res.Message)
- return fmt.Errorf(res.Message)
- }
- return nil
- }
-
- //PushFeatureInfo 推送的风险结果信息
- func (s JobRiskThirdService) PushFeatureInfo(req proto.PushFeatureInfoRequest) error {
- jsBody, _ := json.Marshal(req)
- body, err := RequestSign(jsBody, proto.PushFeatureInfo)
- if err != nil {
- return err
- }
- res := new(proto.ZyResponse)
- err = json.Unmarshal(body, res)
- if err != nil {
- logx.Errorf("PushFeatureInfo call error: %s", err)
- return err
- }
- if res.Status != 200 {
- logx.Errorf("PushFeatureInfo call error: %s", res.Message)
- return fmt.Errorf(res.Message)
- }
- return nil
- }
-
- func RequestSign(data []byte, url string) (body []byte, err error) {
- item := new(model.TbRequestThirdLog)
- reqMap := make(map[string]string)
- reqMap["obj"] = string(data)
-
- headerMap := make(map[string]string)
- headerMap["Content-Type"] = "application/x-www-form-urlencoded"
- b, err := utils.HttpDo(config.Cfg.GetString("sign_server.url"), "POST", reqMap, headerMap)
- if err != nil {
- logx.Error(err)
- return nil, err
- }
- item.SourceData = string(data)
- item.CryptoData = string(b)
- signResponse := new(proto.SignResponse)
- err = json.Unmarshal(b, signResponse)
- if err != nil {
- logx.Error(err)
- return nil, err
- }
- requestStruct := new(proto.ZyBaseRequest)
- err = json.Unmarshal([]byte(signResponse.Data), requestStruct)
- if err != nil {
- logx.Error(err)
- return nil, err
- }
- requestMap := make(map[string]string)
- requestId := utils.NewId()
- requestMap["data"] = requestStruct.Data
- requestMap["key"] = requestStruct.Key
- requestMap["salt"] = requestStruct.Salt
- requestMap["sign"] = requestStruct.Sign
- requestMap["source"] = requestStruct.Source
-
- //decryptMap := make(map[string]string)
- //decryptMap["reqData"]=string(b)
- //b, err = utils.HttpDo("http://sign.ningdatech.com/decrypt","POST",decryptMap,headerMap)
- //if err!= nil{
- // logx.Error(err)
- //}
- //logx.Info(string(b))
- headerMap["Content-Type"] = "application/json"
- resBody, err := utils.HttpDo(fmt.Sprintf("%s:%d%s", config.Cfg.GetString("zy_host.address"),
- config.Cfg.GetInt("zy_host.port"), url), "POST", requestMap, headerMap)
- if err != nil {
- logx.Error(err)
- return nil, err
- }
- item.RequestId = requestId
- item.RequestUri = fmt.Sprintf("%s:%d%s", config.Cfg.GetString("zy_host.address"),
- config.Cfg.GetInt("zy_host.port"), url)
- item.ResultData = string(resBody)
- go model.DB.Create(item)
- return resBody, err
- }
|