|
@@ -3,26 +3,30 @@ package api
|
|
|
import (
|
|
|
"encoding/base64"
|
|
|
"encoding/json"
|
|
|
+ "eta/eta_mini_ht_api/common/component/cache"
|
|
|
"eta/eta_mini_ht_api/common/component/config"
|
|
|
logger "eta/eta_mini_ht_api/common/component/log"
|
|
|
"eta/eta_mini_ht_api/common/contants"
|
|
|
"eta/eta_mini_ht_api/common/exception"
|
|
|
"eta/eta_mini_ht_api/common/utils/auth"
|
|
|
"eta/eta_mini_ht_api/common/utils/client"
|
|
|
+ "eta/eta_mini_ht_api/common/utils/redis"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"sync"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- clientSuitInfoUrl = "/open/api/getClientSuitInfo"
|
|
|
+ clientSuitInfoUrl = "/wt/front/api/open/api/getClientSuitInfo"
|
|
|
accessTokenUrl = "/api/openapi/authless/token"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
htFacadeOnce sync.Once
|
|
|
|
|
|
- htFacade *HTAccountApi
|
|
|
+ htFacade *HTAccountApi
|
|
|
+ redisUtils = cache.GetInstance()
|
|
|
)
|
|
|
|
|
|
type HTAccountApi struct {
|
|
@@ -84,18 +88,44 @@ type EncodeReq struct {
|
|
|
Param string `json:"param"`
|
|
|
}
|
|
|
|
|
|
+func (f *HTAccountApi) getToken() (token string, err error) {
|
|
|
+ accessToken := redis.GenerateCAPAccessTokenKey()
|
|
|
+ token = redisUtils.GetString(accessToken)
|
|
|
+ var tokenInfo TokenInfo
|
|
|
+ if token == "" {
|
|
|
+ tokenInfo, err = f.GetAccessToken()
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("获取token失败%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var parseErr error
|
|
|
+ expireTime, parseErr := time.Parse(time.DateTime, tokenInfo.ExpiresAt)
|
|
|
+ if parseErr != nil {
|
|
|
+ logger.Error("解析过期时间失败:%v", parseErr)
|
|
|
+ err = parseErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ duration := expireTime.Sub(time.Now()).Seconds()
|
|
|
+ token = tokenInfo.AccessToken
|
|
|
+ _ = redisUtils.SetString(accessToken, tokenInfo.AccessToken, int(duration)-5)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
func (f *HTAccountApi) GetCustomerRiskLevelInfo(req ClientSuitInfoReq) (info CustomerAccountInfo, err error) {
|
|
|
url := f.htConfig.GetAccountApiUrl() + clientSuitInfoUrl
|
|
|
+ token, err := f.getToken()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
publicKey, err := auth.ParsePublicKey(f.htConfig.GetWebhookPublicKey())
|
|
|
if err != nil {
|
|
|
logger.Error("解析公钥失败:%v", err)
|
|
|
}
|
|
|
reqStr, err := json.Marshal(req)
|
|
|
encodeData, _ := auth.EncryptWithRSA(publicKey, reqStr)
|
|
|
- resp, err := f.client.Post(url, EncodeReq{
|
|
|
+ resp, err := f.client.PostWithAuth(url, EncodeReq{
|
|
|
Param: base64.StdEncoding.EncodeToString(encodeData),
|
|
|
- })
|
|
|
- err = nil
|
|
|
+ }, token)
|
|
|
if err != nil {
|
|
|
logger.Error("调用CAP customerRiskInfo接口失败:[%v]", err)
|
|
|
return
|
|
@@ -107,12 +137,8 @@ func (f *HTAccountApi) GetCustomerRiskLevelInfo(req ClientSuitInfoReq) (info Cus
|
|
|
}
|
|
|
}(resp.Body)
|
|
|
|
|
|
- //body, _ := io.ReadAll(resp.Body)
|
|
|
- ss := WebhookRequest{
|
|
|
- Data: "Zta5UfBQpvZSQPMo41CGda6/MnMKvz4cTiBptQ/KPiBTiyDDnqE7remZfKzPqYkuzcMDTN7lrr5W2by2gsTd2JjTlg3NzfotoorqHbdjP6FyVCRBCbkSM1OOnkuldtbWaP1b0APh1QhT40zdOvDqwnYcSa7z3zEG0EKh6kYGCcd+ugSmXxrZzZR8uguYZniFQFc+Or2dDFAuQfOhmN9kScQ90iHhlJ6g9pqlSB4sJxA8S2q3MR1qkW1MOA/9Z/Mgj2fy2m2AjP18hlzKMP71A/EezBbusXVNo5occ9xV2L7tfbDqnpk77NJL+YnoIoPhj7WjFC3pDXfD3phHuov8CQ==",
|
|
|
- }
|
|
|
- jsonstr, _ := json.Marshal(ss)
|
|
|
- return decodeResponse(jsonstr)
|
|
|
+ body, _ := io.ReadAll(resp.Body)
|
|
|
+ return decodeResponse(body)
|
|
|
}
|
|
|
|
|
|
type AccessTokenReq struct {
|
|
@@ -180,8 +206,9 @@ type TokenInfo struct {
|
|
|
ExpiresAt string
|
|
|
}
|
|
|
|
|
|
-type WebhookRequest struct {
|
|
|
- Data string `json:"data"`
|
|
|
+type CustomerRiskInfoResp struct {
|
|
|
+ Error Error `json:"error"`
|
|
|
+ Data string `json:"data"`
|
|
|
}
|
|
|
|
|
|
type SyncCustomerRiskLevelReq struct {
|
|
@@ -211,10 +238,20 @@ type CustomerAccountInfo struct {
|
|
|
}
|
|
|
|
|
|
func decodeResponse(resp []byte) (info CustomerAccountInfo, err error) {
|
|
|
- webhookRequest := new(WebhookRequest)
|
|
|
- err = json.Unmarshal(resp, &webhookRequest)
|
|
|
+ customerRiskInfoResp := new(CustomerRiskInfoResp)
|
|
|
+ err = json.Unmarshal(resp, &customerRiskInfoResp)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("customerRiskInfoResp解析失败: %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
- logger.Error("WebhookRequest解析失败: %v", err)
|
|
|
+ logger.Warn("[cap 接口调用]解析客户风险信息接口应答失败:%v", err)
|
|
|
+ err = exception.New(exception.GetCapTokenFailed)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if customerRiskInfoResp.Error.ErrorNo != "0" {
|
|
|
+ logger.Warn("[cap 接口调用] 获取客户风险信息失败:[code:%v, msg:%v, path:%v]", customerRiskInfoResp.Error.ErrorNo, customerRiskInfoResp.Error.ErrorInfo, customerRiskInfoResp.Error.ErrorPathInfo)
|
|
|
+ err = exception.NewWithException(exception.GetCustomerRiskInfoFailed, customerRiskInfoResp.Error.ErrorInfo)
|
|
|
return
|
|
|
}
|
|
|
privateKey, err := auth.ParsePrivateKey(htFacade.htConfig.GetWebhookPrivateKey())
|
|
@@ -222,8 +259,7 @@ func decodeResponse(resp []byte) (info CustomerAccountInfo, err error) {
|
|
|
logger.Error("解析私钥失败: %v", err)
|
|
|
return
|
|
|
}
|
|
|
- logger.Info("解码请求: %v", webhookRequest.Data)
|
|
|
- decodeData, err := auth.DecryptWithRSA(privateKey, webhookRequest.Data)
|
|
|
+ decodeData, err := auth.DecryptWithRSA(privateKey, customerRiskInfoResp.Data)
|
|
|
if err != nil {
|
|
|
logger.Error("解密请求体失败: %v", err)
|
|
|
return
|