Browse Source

temp commit

hsun 2 years ago
parent
commit
c08ccb5f1c
3 changed files with 47 additions and 19 deletions
  1. 2 2
      controller/contract/register.go
  2. 3 0
      models/fms/constants.go
  3. 42 17
      services/fms/currency_rate.go

+ 2 - 2
controller/contract/register.go

@@ -2010,7 +2010,7 @@ func (rg *RegisterController) CurrencyList(c *gin.Context) {
 
 	rateMap := make(map[string]float64)
 	rateList := make([]*fmsService.CurrencyRateItem, 0)
-	
+
 	// 读取缓存
 	cacheJson, _ := global.Redis.Get(context.TODO(), utils.CURRENCY_RMB_RATE).Result()
 	if cacheJson != "" {
@@ -2022,7 +2022,7 @@ func (rg *RegisterController) CurrencyList(c *gin.Context) {
 
 	// 请求汇率接口
 	if cacheJson == "" {
-		rateList, e = fmsService.CurlRateApi()
+		rateList, e = fmsService.GetCurrencyRateList()
 		if e != nil {
 			resp.FailData("获取失败", "请求汇率接口失败, Err: "+e.Error(), c)
 			return

+ 3 - 0
models/fms/constants.go

@@ -34,6 +34,9 @@ const (
 	ContractPaymentPayTypeQuarter  = 3 // 季付
 	ContractPaymentPayTypeOther    = 4 // 次付
 	ContractPaymentPayTypeAbnormal = 5 // 异常
+
+	// 基础货币代码(人民币)
+	BaseCurrencyCode = "CNY"
 )
 
 var ContractStatusKeyNameMap = map[int]string{

+ 42 - 17
services/fms/currency_rate.go

@@ -5,28 +5,31 @@ import (
 	"fmt"
 	"hongze/fms_api/services/alarm_msg"
 	"hongze/fms_api/utils"
+	"io/ioutil"
+	"net/http"
 	"strconv"
+	"strings"
 	"time"
 )
 
-const CurrencyRateApiAppCode = "ABC123"
+const (
+	CurrencyRateApiHost        = "https://jisuhuilv.market.alicloudapi.com"
+	CurrencyRateExchangeSingle = "/exchange/single"
+	CurrencyRateApiAppCode     = "ABC123" // TODO:APPCODE
+)
 
 type CurrencyRateApiResp struct {
-	Status string                    `json:"status" description:"状态码"`
-	Msg    string                    `json:"msg" description:"响应信息"`
-	Result CurrencyRateApiRespResult `json:"result" description:"响应数据"`
-}
-
-type CurrencyRateApiRespResult struct {
-	Currency string `json:"currency"`
-	Name     string `json:"name"`
-	List     map[string]CurrencyRateApiRespResultList
-}
-
-type CurrencyRateApiRespResultList struct {
-	Name       string `json:"name"`
-	Rate       string `json:"rate"`
-	UpdateTime string `json:"updatetime"`
+	Status string `json:"status"`
+	Msg    string `json:"msg"`
+	Result struct {
+		Currency string `json:"currency"`
+		Name     string `json:"name"`
+		List     map[string]struct {
+			Name       string `json:"name"`
+			Rate       string `json:"rate"`
+			UpdateTime string `json:"updatetime"`
+		} `json:"list"`
+	} `json:"result"`
 }
 
 type CurrencyRateItem struct {
@@ -36,13 +39,14 @@ type CurrencyRateItem struct {
 	UpdateTime time.Time `json:"update_time"`
 }
 
-func CurlRateApi() (resList []*CurrencyRateItem, err error) {
+func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
 	defer func() {
 		if err != nil {
 			go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
 		}
 	}()
 	// TODO:请求接口
+	//func HttpCurrencyRateApi(url, method, postData string, params ...string) ([]byte, error) {
 
 	// 汇率结果
 	resultJson := `{"status":"0","msg":"ok","result":{"currency":"CNY","name":"人民币","list":{"HKD":{"name":"港币","rate":"1.2198","updatetime":"2015-10-26 16:56:22"},"USD":{"name":"美元","rate":"0.1574","updatetime":"2015-10-26 16:56:22"},"EUR":{"name":"欧元","rate":"0.1426","updatetime":"2015-10-26 16:56:22"}}}}`
@@ -81,3 +85,24 @@ func CurlRateApi() (resList []*CurrencyRateItem, err error) {
 	}
 	return
 }
+
+func HttpCurrencyRateApi(url, method, postData string, params ...string) ([]byte, error) {
+	body := ioutil.NopCloser(strings.NewReader(postData))
+	client := &http.Client{}
+	req, err := http.NewRequest(method, url, body)
+	if err != nil {
+		return nil, err
+	}
+	contentType := "application/json;charset=utf-8"
+	if len(params) > 0 && params[0] != "" {
+		contentType = params[0]
+	}
+	req.Header.Set("Content-Type", contentType)
+	req.Header.Set("Authorization", fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode))
+	resp, err := client.Do(req)
+	defer func() {
+		_ = resp.Body.Close()
+	}()
+	b, err := ioutil.ReadAll(resp.Body)
+	return b, err
+}