ht_account_api.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package api
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_mini_ht_api/common/component/config"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "eta/eta_mini_ht_api/common/contants"
  8. "eta/eta_mini_ht_api/common/exception"
  9. "eta/eta_mini_ht_api/common/utils/auth"
  10. "eta/eta_mini_ht_api/common/utils/client"
  11. "fmt"
  12. "io"
  13. "sync"
  14. )
  15. const (
  16. clientSuitInfoUrl = "/open/api/getClientSuitInfo"
  17. accessTokenUrl = "/api/openapi/authless/token"
  18. )
  19. var (
  20. htFacadeOnce sync.Once
  21. htFacade *HTAccountApi
  22. )
  23. type HTAccountApi struct {
  24. htConfig *config.HTBizConfig
  25. // HTTP请求客户端
  26. client *client.HttpClient
  27. }
  28. func GetInstance() *HTAccountApi {
  29. htFacadeOnce.Do(func() {
  30. htFacade = &HTAccountApi{
  31. htConfig: config.GetConfig(contants.HT).(*config.HTBizConfig),
  32. client: client.DefaultClient()}
  33. })
  34. return htFacade
  35. }
  36. type ClientSuitInfoReq struct {
  37. ClientName string `json:"client_name"`
  38. IdKind int `json:"id_kind"`
  39. IdNo string `json:"id_no"`
  40. }
  41. type CustomerRiskReq struct {
  42. MobileTel string `json:"mobile_tel"`
  43. ClientName string `json:"client_name"`
  44. IdKind int `json:"id_kind"`
  45. IdNo string `json:"id_no"`
  46. CreateTime string `json:"create_time"`
  47. LoginType string `json:"login_type"`
  48. }
  49. func (f *HTAccountApi) EnCodeData(req CustomerRiskReq) (token string, err error) {
  50. publicKey, err := auth.ParsePublicKey(f.htConfig.GetWebhookPublicKey())
  51. if err != nil {
  52. logger.Error("解析公钥失败:%v", err)
  53. }
  54. str, _ := json.Marshal(req)
  55. rsa, err := auth.EncryptWithRSA(publicKey, str)
  56. if err != nil {
  57. return
  58. }
  59. token = base64.StdEncoding.EncodeToString(rsa)
  60. fmt.Printf("加密后的请求: %v", token)
  61. privateKey, err := auth.ParsePrivateKey(f.htConfig.GetWebhookPrivateKey())
  62. if err != nil {
  63. logger.Error("解析私钥失败: %v", err)
  64. return
  65. }
  66. decodeData, err := auth.DecryptWithRSA(privateKey, token)
  67. if err != nil {
  68. return
  69. }
  70. fmt.Printf("解密后的请求: %v", string(decodeData))
  71. return
  72. }
  73. func (f *HTAccountApi) GetCustomerRiskLevelInfo(req ClientSuitInfoReq) (info CustomerAccountInfo, err error) {
  74. url := f.htConfig.GetAccountApiUrl() + clientSuitInfoUrl
  75. resp, err := f.client.Post(url, nil)
  76. if err != nil {
  77. logger.Error("调用CAP customerRiskInfo接口失败:[%v]", err)
  78. return
  79. }
  80. defer func(Body io.ReadCloser) {
  81. closeErr := Body.Close()
  82. if closeErr != nil {
  83. logger.Error("关闭Response失败:%v", closeErr)
  84. }
  85. }(resp.Body)
  86. body, _ := io.ReadAll(resp.Body)
  87. return decodeResponse(body)
  88. }
  89. type AccessTokenReq struct {
  90. AppId string `json:"app_id"`
  91. SecretKey string `json:"secret_key"`
  92. }
  93. func (f *HTAccountApi) GetAccessToken() (token TokenInfo, err error) {
  94. url := f.htConfig.GetAccountApiUrl() + accessTokenUrl
  95. req := AccessTokenReq{
  96. AppId: f.htConfig.GetAppId(),
  97. SecretKey: f.htConfig.GetSecretKey(),
  98. }
  99. resp, err := f.client.Post(url, req)
  100. if err != nil {
  101. logger.Error("调用CAP accessToken接口失败:[%v]", err)
  102. return
  103. }
  104. defer func(Body io.ReadCloser) {
  105. closeErr := Body.Close()
  106. if closeErr != nil {
  107. logger.Error("关闭Response失败:%v", closeErr)
  108. }
  109. }(resp.Body)
  110. body, _ := io.ReadAll(resp.Body)
  111. var tokenResp AccessTokenResp
  112. err = json.Unmarshal(body, &tokenResp)
  113. if err != nil {
  114. logger.Warn("[cap 接口调用]解析cap token接口应答失败:%v", err)
  115. err = exception.New(exception.GetCapTokenFailed)
  116. return
  117. }
  118. if tokenResp.Error.ErrorNo != "0" {
  119. logger.Warn("[cap 接口调用] 获取token失败:[code:%v, msg:%v, path:%v]", tokenResp.Error.ErrorNo, tokenResp.Error.ErrorInfo, tokenResp.Error.ErrorPathInfo)
  120. err = exception.NewWithException(exception.GetCapTokenFailed, tokenResp.Error.ErrorInfo)
  121. return
  122. }
  123. token = TokenInfo{
  124. AccessToken: tokenResp.Data.AccessToken,
  125. ExpiresAt: tokenResp.Data.ExpiresAt,
  126. }
  127. return
  128. }
  129. type AccessTokenResp struct {
  130. Error Error `json:"error"`
  131. Data AccessTokenData `json:"data"`
  132. }
  133. type Error struct {
  134. ErrorNo string `json:"error_no"`
  135. ErrorInfo string `json:"error_info"`
  136. ErrorPathInfo string `json:"error_path_info"`
  137. }
  138. type AccessTokenData struct {
  139. AccessToken string `json:"accessToken"`
  140. ExpiresAt string `json:"expiresAt"`
  141. }
  142. type TokenInfo struct {
  143. AccessToken string
  144. ExpiresAt string
  145. }
  146. type WebhookRequest struct {
  147. Data string `json:"data"`
  148. }
  149. type SyncCustomerRiskLevelReq struct {
  150. CustInfo CustInfo `json:"custInfo"`
  151. RiskInfo RiskInfo `json:"riskInfo"`
  152. }
  153. type CustInfo struct {
  154. MobileTel string `json:"mobile_tel"`
  155. ClientName string `json:"client_name"`
  156. IdKind string `json:"id_kind"`
  157. IdNo string `json:"id_no"`
  158. IdBeginDate string `json:"id_begindate"`
  159. IdEndDate string `json:"id_enddate"`
  160. }
  161. type RiskInfo struct {
  162. CorpBeginDate string `json:"corp_begin_date"`
  163. CorpEndDate string `json:"corp_end_date"`
  164. UserInvestTerm string `json:"user_invest_term"`
  165. UserInvestKind string `json:"user_invest_kind"`
  166. CorpRiskLevel string `json:"corp_risk_level"`
  167. }
  168. type CustomerAccountInfo struct {
  169. CustInfo CustInfo `json:"custInfo"`
  170. RiskInfo RiskInfo `json:"riskInfo"`
  171. }
  172. func decodeResponse(resp []byte) (info CustomerAccountInfo, err error) {
  173. webhookRequest := new(WebhookRequest)
  174. err = json.Unmarshal(resp, &webhookRequest)
  175. if err != nil {
  176. logger.Error("WebhookRequest解析失败: %v", err)
  177. return
  178. }
  179. privateKey, err := auth.ParsePrivateKey(htFacade.htConfig.GetWebhookPrivateKey())
  180. if err != nil {
  181. logger.Error("解析私钥失败: %v", err)
  182. return
  183. }
  184. logger.Info("解码请求: %v", webhookRequest.Data)
  185. decodeData, err := auth.DecryptWithRSA(privateKey, webhookRequest.Data)
  186. if err != nil {
  187. logger.Error("解密请求体失败: %v", err)
  188. return
  189. }
  190. fmt.Printf("解密后的请求: %v", string(decodeData))
  191. err = json.Unmarshal(decodeData, &info)
  192. if err != nil {
  193. logger.Error("customerInfo解析失败: %v", err)
  194. return
  195. }
  196. return
  197. }