|
@@ -7,6 +7,7 @@ import (
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
"hongze/fms_api/global"
|
|
|
"hongze/fms_api/models/fms"
|
|
|
+ "hongze/fms_api/services/alarm_msg"
|
|
|
"hongze/fms_api/utils"
|
|
|
"strconv"
|
|
|
"time"
|
|
@@ -15,11 +16,13 @@ import (
|
|
|
const (
|
|
|
CurrencyRateApiHost = "https://jisuhuilv.market.alicloudapi.com"
|
|
|
CurrencyRateExchangeSingle = "/exchange/single"
|
|
|
- CurrencyRateApiAppCode = "ABC123" // TODO:APPCODE
|
|
|
+ CurrencyRateApiAppCode = "22553c4ba74545568aba70ac6cfd441d"
|
|
|
+ CurrencyRateApiAppKey = "203889624"
|
|
|
+ CurrencyRateApiAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
|
|
|
)
|
|
|
|
|
|
type CurrencyRateApiResp struct {
|
|
|
- Status string `json:"status"`
|
|
|
+ Status int `json:"status"`
|
|
|
Msg string `json:"msg"`
|
|
|
Result struct {
|
|
|
Currency string `json:"currency"`
|
|
@@ -43,18 +46,15 @@ type CurrencyRateItem struct {
|
|
|
func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- fmt.Println("请求汇率接口失败, Err: " + err.Error())
|
|
|
- //go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
|
|
|
+ go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- // TODO:请求汇率接口
|
|
|
+ // 请求汇率接口
|
|
|
query := "?currency=CNY"
|
|
|
reqUrl := fmt.Sprint(CurrencyRateApiHost, CurrencyRateExchangeSingle, query)
|
|
|
auth := fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode)
|
|
|
resultByte, e := utils.PublicGetRequest(reqUrl, auth)
|
|
|
- fmt.Println("reqUrl: ", reqUrl)
|
|
|
- fmt.Println("PublicGetRequest err: ", e)
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("HttpCurrencyRateApi errMsg: %s", e.Error())
|
|
|
return
|
|
@@ -65,15 +65,12 @@ func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err 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"}}}}`
|
|
|
results := new(CurrencyRateApiResp)
|
|
|
- fmt.Println("result JSON: ", string(resultByte))
|
|
|
if e := json.Unmarshal(resultByte, &results); e != nil {
|
|
|
- //if e := json.Unmarshal([]byte(resultJson), &results); e != nil {
|
|
|
- err = fmt.Errorf("rate api result unmarshal err: %s", results.Msg)
|
|
|
+ err = fmt.Errorf("rate api result unmarshal err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
- if results.Status != "0" {
|
|
|
+ if results.Status != 0 {
|
|
|
err = fmt.Errorf("curl rate api errMsg: %s", results.Msg)
|
|
|
return
|
|
|
}
|
|
@@ -106,6 +103,7 @@ func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
|
|
|
|
|
|
// GetTodayCurrencyRateList 获取今日货币及汇率列表
|
|
|
func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
|
|
|
+ list = make([]*fms.CurrencyUnitItem, 0)
|
|
|
// 货币列表
|
|
|
ob := new(fms.CurrencyUnit)
|
|
|
cond := `enable = 1`
|
|
@@ -120,7 +118,7 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
|
|
|
rateList := make([]*CurrencyRateItem, 0)
|
|
|
|
|
|
// 读取缓存
|
|
|
- cacheJson, _ := global.Redis.Get(context.TODO(), utils.CURRENCY_RMB_RATE).Result()
|
|
|
+ cacheJson, _ := global.Redis.Get(context.TODO(), utils.CACHE_KEY_CURRENCY_RMB_RATE).Result()
|
|
|
if cacheJson != "" {
|
|
|
if e = jsoniter.UnmarshalFromString(cacheJson, &rateList); e != nil {
|
|
|
err = fmt.Errorf("解析汇率缓存数据失败, Err: %s", e.Error())
|
|
@@ -135,6 +133,10 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
|
|
|
err = fmt.Errorf("请求汇率接口失败, Err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
+ if len(rateList) == 0 {
|
|
|
+ err = fmt.Errorf("汇率接口返回数据为空")
|
|
|
+ return
|
|
|
+ }
|
|
|
newCache, e := jsoniter.MarshalToString(rateList)
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("写入汇率缓存失败, Err: %s", e.Error())
|
|
@@ -143,23 +145,24 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
|
|
|
now := time.Now()
|
|
|
todayLast := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
|
|
|
sub := todayLast.Sub(now)
|
|
|
- _ = global.Redis.SetEX(context.TODO(), utils.CURRENCY_RMB_RATE, newCache, sub)
|
|
|
+ _ = global.Redis.SetEX(context.TODO(), utils.CACHE_KEY_CURRENCY_RMB_RATE, newCache, sub)
|
|
|
}
|
|
|
+
|
|
|
for i := range rateList {
|
|
|
rateMap[rateList[i].Code] = rateList[i].Rate
|
|
|
}
|
|
|
-
|
|
|
- respList := make([]*fms.CurrencyUnitItem, 0)
|
|
|
for i := range currencyList {
|
|
|
r := rateMap[currencyList[i].Code]
|
|
|
if currencyList[i].Code == fms.BaseCurrencyCode {
|
|
|
r = 1
|
|
|
}
|
|
|
- respList = append(respList, &fms.CurrencyUnitItem{
|
|
|
- Name: currencyList[i].Name,
|
|
|
- Code: currencyList[i].Code,
|
|
|
- Enable: currencyList[i].Enable,
|
|
|
- RMBRate: r,
|
|
|
+ list = append(list, &fms.CurrencyUnitItem{
|
|
|
+ Name: currencyList[i].Name,
|
|
|
+ UnitName: currencyList[i].UnitName,
|
|
|
+ Code: currencyList[i].Code,
|
|
|
+ Enable: currencyList[i].Enable,
|
|
|
+ RMBRate: r,
|
|
|
+ FlagImg: currencyList[i].FlagImg,
|
|
|
})
|
|
|
}
|
|
|
return
|