Vestmore_GO/proto/stock.go

95 lines
2.3 KiB
Go

package proto
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
var appId = "2D64E3B7A9884A8C5E2BFF437763E76A"
var StockApiHost = "http://kindics.com:7040"
var appSecret = "5105D41CE0527A9AF6A307341B3ADA10"
func StockDetail(market string, stockType uint64, stockSymbol string) (map[string]any, error) {
instrument := fmt.Sprintf("%s|%d|%s", market, stockType, stockSymbol)
data := url.Values{
"appId": {appId},
"appSecret": {appSecret},
"action": {"/V2/Quotation/Detail"},
"instrument": {instrument},
"lang": {"zh-CN"},
}
resp, err := http.PostForm(StockApiHost, data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
if result["code"].(float64) == 1 {
response := result["data"].(map[string]interface{})
lastTradeDate := "1991-01-01"
if date, ok := response["lastTradeDate"].(string); ok && date != "" {
lastTradeDate = date[:4] + "-" + date[4:6] + "-" + date[6:]
}
maturityDate := "1991-01-01"
if date, ok := response["maturityDate"].(string); ok && date != "" {
maturityDate = date[:4] + "-" + date[4:6] + "-" + date[6:]
}
inlineFlag := 2
if flag, ok := response["inlineFlag"].(float64); ok {
inlineFlag = int(flag)
}
return map[string]interface{}{
"symbol": response["symbol"],
"name": response["name"],
"msgs": response["lotSize"],
"price": response["latestPrice"],
"securityStatus": response["securityStatus"],
"exercisePrice": getFloat(response, "exercisePrice"),
"makePeace": getFloat(response, "makePeace"),
"exchangeRatio": getFloat(response, "exchangeRatio"),
"exchangePrice": getFloat(response, "exchangePrice"),
"callPrice": getFloat(response, "callPrice"),
"lastTradeDate": lastTradeDate,
"maturityDate": maturityDate,
"callOrPut": getString(response, "callOrPut"),
"inlineFlag": inlineFlag,
}, nil
}
return nil, nil
}
func getFloat(m map[string]interface{}, key string) float64 {
if val, ok := m[key].(float64); ok {
return val
}
return 0
}
func getString(m map[string]interface{}, key string) string {
if val, ok := m[key].(string); ok {
return val
}
return ""
}