ht_account_api.go 7.4 KB

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