|
@@ -3,12 +3,8 @@ package fms
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
- "hongze/fms_api/services/alarm_msg"
|
|
|
|
"hongze/fms_api/utils"
|
|
"hongze/fms_api/utils"
|
|
- "io/ioutil"
|
|
|
|
- "net/http"
|
|
|
|
"strconv"
|
|
"strconv"
|
|
- "strings"
|
|
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -42,16 +38,33 @@ type CurrencyRateItem struct {
|
|
func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
|
|
func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
|
|
defer func() {
|
|
defer func() {
|
|
if err != nil {
|
|
if err != nil {
|
|
- go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
|
|
|
|
|
|
+ fmt.Println("请求汇率接口失败, Err: " + err.Error())
|
|
|
|
+ //go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
|
|
}
|
|
}
|
|
}()
|
|
}()
|
|
- // TODO:请求接口
|
|
|
|
- //func HttpCurrencyRateApi(url, method, postData string, params ...string) ([]byte, error) {
|
|
|
|
|
|
+
|
|
|
|
+ // 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
|
|
|
|
+ }
|
|
|
|
+ if len(resultByte) == 0 {
|
|
|
|
+ err = fmt.Errorf("HttpCurrencyRateApi empty data")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
// 汇率结果
|
|
// 汇率结果
|
|
- 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"}}}}`
|
|
|
|
|
|
+ //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)
|
|
results := new(CurrencyRateApiResp)
|
|
- if e := json.Unmarshal([]byte(resultJson), &results); e != nil {
|
|
|
|
|
|
+ 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", results.Msg)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -85,24 +98,3 @@ func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
|
|
}
|
|
}
|
|
return
|
|
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
|
|
|
|
-}
|
|
|