|
@@ -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
|
|
|
+}
|