You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.9 KiB

package exchange
import (
"encoding/json"
"epur-pay/model"
"epur-pay/pkg/httpclient"
"epur-pay/pkg/logger"
"epur-pay/pkg/rds"
"epur-pay/pkg/utils"
"fmt"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"time"
)
type CurBody struct {
Name string `json:"name"` // 名称
Rate string `json:"rate"` // 汇率
Updatetime string `json:"updatetime"` // 时间2022-11-01 14:09:24
}
type ResultMsg struct {
Currency string `json:"currency"` // 币种CNY
Name string `json:"name"` // 币种名称人民币
List map[string]CurBody `json:"list"` // USD
}
type CurResponse struct {
Status int `json:"status"`
Msg string `json:"msg"`
Result ResultMsg `json:"result"`
}
type WanWeiYuanCurrency struct {
HuiIn string `json:"hui_in,omitempty"` // 现汇买入价
HuiOut string `json:"hui_out,omitempty"` // 现汇卖出价
ChaoOut string `json:"chao_out,omitempty"` // 现钞卖出价
ChaoIn string `json:"chao_in,omitempty"` // 现钞买入价
Name string `json:"name"` // 货币名称
Zhesuan string `json:"zhesuan,omitempty"` // 中行折算价
Code string `json:"code"` // 货币简码
Day string `json:"day"` // 发布日期
Time string `json:"time"` // 发布时间
}
type WanWeiYuanCurrencyResponse struct {
ShowapiResError string `json:"showapi_res_error"`
ShowapiResId string `json:"showapi_res_id"`
ShowapiResCode int64 `json:"showapi_res_code"`
ShowapiFeeNum int64 `json:"showapi_fee_num"`
ShowapiResBody struct {
RetCode int64 `json:"ret_code"`
ListSize int64 `json:"listSize"`
List []WanWeiYuanCurrency `json:"list"`
} `json:"showapi_res_body"`
}
type ExchangeResponse struct {
ShowapiResError string `json:"showapi_res_error"`
ShowapiResId string `json:"showapi_res_id"`
ShowapiResCode int `json:"showapi_res_code"`
ShowapiFeeNum int `json:"showapi_fee_num"`
ShowapiResBody ExchangeResBody `json:"showapi_res_body"`
}
type ExchangeResBody struct {
List []ExchangeList `json:"list"`
ListSize int `json:"listSize"`
RetCode int `json:"ret_code"`
}
type ExchangeList struct {
Code string `json:"code"`
HuiOut string `json:"hui_out"`
Zhesuan string `json:"zhesuan"`
ChaoOut string `json:"chao_out"`
Time string `json:"time"`
Name string `json:"name"`
HuiIn string `json:"hui_in"`
ChaoIn string `json:"chao_in"`
Day string `json:"day"`
}
func GetRate() {
btime := time.Now().UnixNano()
logger.AccessLogger.Info("ExchangeRate...", btime)
if err := rds.DB.Transaction(func(ts *gorm.DB) error {
resp := ExchangeResponse{}
// TWD新台币 MYR马来西亚林吉特 SGD新加坡元 PHP菲律宾比索 IDR印度尼西亚盾 THB泰铢 BRL巴西雷亚尔 GBP英镑 MXN墨西哥比索 VND越南盾
//Currency := []string{"TWD", "MYR", "SGD", "PHP", "IDR", "THB", "BRL", "GBP", "MXN", "VND"}
str, _ := AliyunExchangeApi()
err := json.Unmarshal([]byte(str), &resp)
if err != nil {
logger.AccessLogger.Error("ERROR:", err.Error())
return err
}
if resp.ShowapiResCode != 0 {
logger.AccessLogger.Error("ERROR:", resp.ShowapiResError)
return err
}
for idx, tmp := range resp.ShowapiResBody.List {
logger.AccessLogger.Debug(idx, "处理币种:", tmp.Code, "汇率:", tmp.HuiIn)
if len(tmp.HuiIn) < 1 {
tmp.HuiIn = tmp.ChaoIn
}
rate := model.ExchangeRate{}
rate.RateId = 0
rate.FromName = "人民币"
rate.FromCurrency = "CNY"
rate.FromAmt = decimal.NewFromFloat(1.00)
rate.Unit = "元"
rate.ToCurrency = tmp.Code
rate.ToName = tmp.Name
rate.Date = utils.Str2FormatDateTime(tmp.Day, "2006-01-02", nil).Unix()
rate.Status = model.Normal
rate.CreateTime = utils.Time2StampSecond()
rate.ToAmt, _ = decimal.NewFromString(tmp.HuiIn)
rate.ToAmt = decimal.NewFromInt(100).DivRound(rate.ToAmt, 6)
res := ts.Table(model.ExchangeRate{}.TableName()).
Where(model.ExchangeRate{FromCurrency: rate.FromCurrency, ToCurrency: rate.ToCurrency, Date: rate.Date, Status: model.Normal}).
FirstOrCreate(&rate)
if res.Error != nil {
logger.AccessLogger.Error("ERROR:", res.Error)
return res.Error
}
//// 刷新缓存
//cache.Global.Caches.Currency.Refresh()
}
return nil
}); err != nil {
logger.AccessLogger.Error("ERROR:", err.Error())
}
etime := time.Now().UnixNano()
logger.AccessLogger.Info("END...", etime-btime)
//logger.AccessLogger.Info("USD:", cache.Global.Caches.Currency.ToCNY("USD", decimal.NewFromInt(100)))
}
// curl -i -k --get --include
// 'https://ali-waihui.showapi.com/waihui-transform?fromCode=GBP&money=100&toCode=EUR'
// -H 'Authorization:APPCODE 你自己的AppCode'
func HttpGet(toCode string) (str string, code int) {
header := map[string]string{}
header["Authorization"] = "APPCODE 63e68d5081144e0185139b496a999000"
url := fmt.Sprintf("https://ali-waihui.showapi.com/waihui-transform?fromCode=%s&money=%s&toCode=%s",
"CNY", "100", toCode)
str, code = httpclient.DoHttp(url, "GET", "", header)
logger.AccessLogger.Info("返回信息:", str, "http代码", code)
return str, code
}
// https://jisuhuilv.market.alicloudapi.com/exchange/single
// curl -i -k -X GET'https://jisuhuilv.market.alicloudapi.com/exchange/single?currency=CNY'
// https://market.aliyun.com/products/57000002/cmapi010841.html?spm=5176.2020520132.101.3.2b9b72189hTyJH#sku=yuncode484100008
// -H 'Authorization:APPCODE 你自己的AppCode'
func AliyunExchangeApi() (str string, code int) {
header := map[string]string{}
header["Authorization"] = "APPCODE 63e68d5081144e0185139b496a999000"
//url := "https://jisuhuilv.market.alicloudapi.com/exchange/single?currency=CNY"
url := "https://ali-waihui.showapi.com/waihui-list?code=code"
str, code = httpclient.DoHttp(url, "GET", "", header)
//logger.AccessLogger.Info("返回信息:", str, "http代码", code)
logger.AccessLogger.Info("http代码:", code, "\n", str)
return str, code
}