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.
281 lines
8.6 KiB
281 lines
8.6 KiB
package processor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strings"
|
|
"wss-pool/config"
|
|
"wss-pool/dictionary"
|
|
"wss-pool/internal"
|
|
"wss-pool/internal/data/business"
|
|
red "wss-pool/internal/redis"
|
|
"wss-pool/logging/applogger"
|
|
"wss-pool/pkg/model"
|
|
"wss-pool/pkg/model/stock"
|
|
)
|
|
|
|
/*
|
|
现货-K线数据(蜡烛图) https://api.huobi.pro/market/history/kline?symbol=btcusdt&period=1min&size=20
|
|
1、symbol 交易对 例如:btcusdt, ethbtc
|
|
2、period 返回数据时间粒度,也就是每根蜡烛的时间区间 目前提供:[1min, 5min, 15min, 30min, 60min, 4hour, 1day, 1mon, 1week, 1year]
|
|
3、size 返回K线数据条数 例如:[1-2000]
|
|
*/
|
|
|
|
var TradeMap = map[string]int{
|
|
"buy": 1,
|
|
"sell": 2,
|
|
}
|
|
|
|
func SpotsKline(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
period := internal.ReplaceStr(c.Query("period"))
|
|
size := internal.IntegerInit(internal.ReplaceStr(c.Query("size")))
|
|
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
if len(period) > 0 {
|
|
param = param + "&" + fmt.Sprintf("period=%v", period)
|
|
}
|
|
if size > 0 {
|
|
param = param + "&" + fmt.Sprintf("size=%v", size)
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%v/market/history/kline?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusBadRequest, err, internal.QueryError))
|
|
return
|
|
}
|
|
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-聚合行情(Ticker) https://api.huobi.pro/market/detail/merged?symbol=btcusdt
|
|
1、symbol 交易对 例如:btcusdt, ethbtc
|
|
*/
|
|
func SpotsMerged(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%v/market/detail/merged?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
return
|
|
}
|
|
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|
|
// 现货列表 https://api.huobi.pro/market/detail/merged?symbol=btcusdt
|
|
func SpotsMergedList(c *gin.Context) {
|
|
var mergedList []model.GetSpotsTickCompleteList
|
|
|
|
// 循环获取现货列表
|
|
for _, value := range dictionary.Symbol {
|
|
param := fmt.Sprintf("%vusdt", value)
|
|
url := fmt.Sprintf("https://%v/market/detail/merged?symbol=%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
return
|
|
}
|
|
applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
var merged model.GetSpotsTickList
|
|
if err := json.Unmarshal([]byte(bodyStr), &merged); err != nil {
|
|
applogger.Error("Unmarshal err:%v", err)
|
|
continue
|
|
}
|
|
|
|
mergedCon := model.GetSpotsTickCompleteList{
|
|
Tick: merged.Tick,
|
|
Ch: merged.Ch,
|
|
Status: merged.Status,
|
|
Ts: merged.Ts,
|
|
Icon: "",
|
|
FullName: "",
|
|
}
|
|
|
|
mergedList = append(mergedList, mergedCon)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, mergedList, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-所有交易对的最新 Tickers https://api.huobi.pro/market/tickers
|
|
*/
|
|
func SpotsTickers(c *gin.Context) {
|
|
url := fmt.Sprintf("https://%v/market/tickers", config.Config.HbApi.HbSpotsApiHost)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
return
|
|
}
|
|
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-市场深度数据 https://api.huobi.pro/market/depth?symbol=btcusdt&depth=5&type=step0
|
|
1、symbol 交易对 例如:btcusdt, ethbtc
|
|
2、depth 返回深度的数量 例如:5,10,20
|
|
3、type 深度的价格聚合度,具体说明见下方 例如:step0,step1,step2,step3,step4,step5
|
|
*/
|
|
func SpotsDepth(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
depth := internal.IntegerInit(internal.ReplaceStr(c.Query("depth")))
|
|
typeS := internal.ReplaceStr(c.Query("type"))
|
|
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
if depth > 0 {
|
|
param = param + "&" + fmt.Sprintf("depth=%v", depth)
|
|
}
|
|
if len(typeS) > 0 {
|
|
param = param + "&" + fmt.Sprintf("type=%v", typeS)
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%v/market/depth?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
//return
|
|
}
|
|
if strings.Contains(bodyStr, "invalid-parameter") {
|
|
chStep6 := fmt.Sprintf("market-%s-depth-step0", symbol)
|
|
bodyStr, _ = red.Get_Cache_Data(chStep6)
|
|
}
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-最近市场成交记录 https://api.huobi.pro/market/trade?symbol=btcusdt
|
|
1、symbol 交易对 例如:btcusdt, ethbtc
|
|
*/
|
|
func SpotsTrade(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%v/market/trade?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
return
|
|
}
|
|
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-获得近期交易记录 https://api.huobi.pro/market/history/trade?symbol=btcusdt&size=2
|
|
1、symbol 交易对 例如:btcusdt, ethbtc...
|
|
2、size 返回的交易记录数量,最大值2000
|
|
*/
|
|
func SpotsHistoryTrade(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
size := internal.IntegerInit(internal.ReplaceStr(c.Query("size")))
|
|
//symbols := strings.Split(symbol, "usdt")
|
|
//phpRes := websocketservice.PHPMarketTrade(SpotsStatus, size, symbols[0])
|
|
//num := size - len(phpRes.List)
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
if size > 0 {
|
|
param = param + "&" + fmt.Sprintf("size=%v", size)
|
|
}
|
|
//if num > 0 {
|
|
phpRes := stock.PHPMarketTradeList{}
|
|
url := fmt.Sprintf("https://%v/market/history/trade?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
huobiRes := business.MarketTrade{}
|
|
bodyStr, _ := internal.HttpGet(url)
|
|
json.Unmarshal([]byte(bodyStr), &huobiRes)
|
|
for _, v := range huobiRes.Data {
|
|
item := stock.MarketTrade{
|
|
ID: v.Data[0].ID,
|
|
OrderNumber: fmt.Sprintf("%f", v.Data[0].Amount),
|
|
DealPrice: fmt.Sprintf("%f", v.Data[0].Price),
|
|
OrderTime: v.Data[0].Ts,
|
|
TradeType: TradeMap[v.Data[0].Direction],
|
|
IsHuobi: true,
|
|
}
|
|
phpRes.List = append(phpRes.List, item)
|
|
}
|
|
if len(phpRes.List) <= 0 {
|
|
title := fmt.Sprintf("market-%s-trade-detail", symbol)
|
|
item, _ := red.Get_Cache_Data(title)
|
|
json.Unmarshal([]byte(item), &phpRes.List)
|
|
}
|
|
//}
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, phpRes.List, internal.QuerySuccess))
|
|
}
|
|
|
|
/*
|
|
现货-最近24小时行情数据 https://api.huobi.pro/market/detail?symbol=btcusdt
|
|
1、symbol 交易对 例如:btcusdt, ethbtc
|
|
*/
|
|
func SpotsDetail(c *gin.Context) {
|
|
symbol := internal.ReplaceStr(c.Query("symbol"))
|
|
|
|
var param string
|
|
if len(symbol) > 0 {
|
|
param = fmt.Sprintf("symbol=%v", symbol)
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%v/market/detail?%v", config.Config.HbApi.HbSpotsApiHost, param)
|
|
applogger.Debug("查询数据信息:%v", url)
|
|
|
|
bodyStr, err := internal.HttpGet(url)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, err, internal.QueryError))
|
|
return
|
|
}
|
|
if strings.Contains(bodyStr, "invalid-parameter") {
|
|
title := fmt.Sprintf("market-%s-detail-merged", symbol)
|
|
bodyStr, _ = red.Get_Cache_Data(title)
|
|
}
|
|
//applogger.Debug("第三方数据接收:%v", bodyStr)
|
|
|
|
c.JSON(http.StatusOK, internal.GinResult(http.StatusOK, bodyStr, internal.QuerySuccess))
|
|
}
|
|
|