zwxi 1 year ago
parent
commit
60240abbb2
1 changed files with 85 additions and 37 deletions
  1. 85 37
      services/base_from_ths_ds.go

+ 85 - 37
services/base_from_ths_ds.go

@@ -7,6 +7,7 @@ import (
 	"eta/eta_index_lib/utils"
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/http"
+	"strings"
 )
 
 func GetEdbDataFromThsDs(stockCode, edbCode, startDate, endDate, edbTerminalCode string) (item models.EdbDataFromThs, err error) {
@@ -36,43 +37,44 @@ func GetEdbDataFromThsDs(stockCode, edbCode, startDate, endDate, edbTerminalCode
 		return getEdbDataFromThsDsHttp(stockCode, edbCode, startDate, endDate, terminal.Value, token)
 	}
 
-	return getEdbDataFromThsDsApp(stockCode,edbCode, startDate, endDate, 0, terminal.ServerUrl)
+	return getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate, 0, terminal.ServerUrl)
 }
 
 type EdbDataFromThsSdInterface struct {
-	Errorcode   int      `json:"errorcode"`
-	Errmsg      string   `json:"errmsg"`
-	Tables      []Table  `json:"tables"`
-	Datatype    []Type   `json:"datatype"`
-	InputParams interface{}   `json:"inputParams"`
-	DataVol     int      `json:"dataVol"`
-	Perf        int      `json:"perf"`
+	Errorcode   int         `json:"errorcode"`
+	Errmsg      string      `json:"errmsg"`
+	Tables      []Table     `json:"tables"`
+	Datatype    []Type      `json:"datatype"`
+	InputParams interface{} `json:"inputParams"`
+	DataVol     int         `json:"dataVol"`
+	Perf        int         `json:"perf"`
 }
 
 type Table struct {
-	Thscode string    `json:"thscode"`
-	Time    []string  `json:"time"`
+	Thscode string                 `json:"thscode"`
+	Time    []string               `json:"time"`
 	Table   map[string]interface{} `json:"table"`
 }
 
 type StockData struct {
-	THSOpenPriceStock      []float64 `json:"ths_open_price_stock"`
-	THSHighPriceStock      []float64 `json:"ths_high_price_stock"`
-	THSLowStock            []float64 `json:"ths_low_stock"`
-	THSClosePriceStock     []float64 `json:"ths_close_price_stock"`
-	THSChgRatioStock       []float64 `json:"ths_chg_ratio_stock"`
-	THSChgStock            []float64 `json:"ths_chg_stock"`
-	THSVolStock            []float64 `json:"ths_vol_stock"`
-	THSPreCloseStock       []float64 `json:"ths_pre_close_stock"`
-	THSSwingStock          []float64 `json:"ths_swing_stock"`
-	THSTurnoverRatioStock  []float64 `json:"ths_turnover_ratio_stock"`
-	THSAmtStock            []float64 `json:"ths_amt_stock"`
+	THSOpenPriceStock     []float64 `json:"ths_open_price_stock"`
+	THSHighPriceStock     []float64 `json:"ths_high_price_stock"`
+	THSLowStock           []float64 `json:"ths_low_stock"`
+	THSClosePriceStock    []float64 `json:"ths_close_price_stock"`
+	THSChgRatioStock      []float64 `json:"ths_chg_ratio_stock"`
+	THSChgStock           []float64 `json:"ths_chg_stock"`
+	THSVolStock           []float64 `json:"ths_vol_stock"`
+	THSPreCloseStock      []float64 `json:"ths_pre_close_stock"`
+	THSSwingStock         []float64 `json:"ths_swing_stock"`
+	THSTurnoverRatioStock []float64 `json:"ths_turnover_ratio_stock"`
+	THSAmtStock           []float64 `json:"ths_amt_stock"`
 }
 
 type Type struct {
 	Itemid string `json:"itemid"`
 	Type   string `json:"type"`
 }
+
 //
 //type Params struct {
 //	Jsonrpc bool     `json:"jsonrpc"`
@@ -98,7 +100,7 @@ func getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate string, num i
 		return
 	}
 	thsUrl := serverUrl + `edbInfo/ths/ds?StockCode=%s&EdbCode=%s&StartDate=%s&EndDate=%s`
-	thsUrl = fmt.Sprintf(thsUrl, stockCode, edbCode, startDate, endDate)
+	thsUrl = fmt.Sprintf(thsUrl, stockCode, edbCode, "2023-11-01", endDate)
 	utils.FileLog.Info("thsUrl:" + thsUrl)
 	body, err := http.Get(thsUrl)
 	utils.FileLog.Info("ths result:" + string(body))
@@ -110,19 +112,65 @@ func getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate string, num i
 		err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
 		return
 	}
-println(string(body))
-
-	var jsonArray []string
-	if err = json.Unmarshal(body, &jsonArray); err != nil {
-		fmt.Println("json.Unmarshal Err:", err)
-		return
-	}
+	println(string(body))
 	tablesList := make([]models.Tables, 0)
-
-	// 解码数组内的每个 JSON 字符串
-	//var responses []TerminalResponse
 	var errCode int64
-	for _, data := range jsonArray {
+	if strings.Contains(edbCode, ",") {
+		var jsonArray []string
+		if err = json.Unmarshal(body, &jsonArray); err != nil {
+			fmt.Println("json.Unmarshal Err:", err)
+			return
+		}
+		// 解码数组内的每个 JSON 字符串
+		//var responses []TerminalResponse
+
+		for _, data := range jsonArray {
+			tableTimeList := make([]string, 0)
+			tableValueList := make([]float64, 0)
+			var response TerminalResponse
+			if err = json.Unmarshal([]byte(data), &response); err != nil {
+				fmt.Println("json.Unmarshal Err:", err)
+				return
+			}
+
+			errCode = int64(response.ErrorCode)
+			if response.ErrorCode != 0 {
+				//session has expired,please re-login after using the system
+				//如果是同花顺登录session失效了,那么就重新请求获取数据
+				if response.ErrorCode == -1020 && num == 0 {
+					return getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate, 1, serverUrl)
+				}
+				err = errors.New(string(body))
+				return
+			}
+
+			for _, stockData := range response.Data {
+				time := stockData["time"].(string)
+				//thsCode := stockData["thscode"].(string)
+				tableTimeList = append(tableTimeList, time)
+
+				for k, v := range stockData {
+					if k != "time" && k != "thscode" {
+						tableValueList = append(tableValueList, v.(float64))
+					}
+				}
+			}
+			tmpTable := models.Tables{
+				ID:    []string{},
+				Time:  tableTimeList,
+				Value: tableValueList,
+			}
+			tablesList = append(tablesList, tmpTable)
+		}
+	} else {
+		var data string
+		if err = json.Unmarshal(body, &data); err != nil {
+			fmt.Println("json.Unmarshal Err:", err)
+			return
+		}
+		// 解码数组内的每个 JSON 字符串
+		//var responses []TerminalResponse
+
 		tableTimeList := make([]string, 0)
 		tableValueList := make([]float64, 0)
 		var response TerminalResponse
@@ -149,17 +197,19 @@ println(string(body))
 
 			for k, v := range stockData {
 				if k != "time" && k != "thscode" {
-					tableValueList = append(tableValueList,  v.(float64))
+					tableValueList = append(tableValueList, v.(float64))
 				}
 			}
 		}
 		tmpTable := models.Tables{
-			ID:  []string{},
+			ID:    []string{},
 			Time:  tableTimeList,
 			Value: tableValueList,
 		}
 		tablesList = append(tablesList, tmpTable)
+
 	}
+
 	item = models.EdbDataFromThs{
 		DataVol:   0,
 		Errmsg:    "",
@@ -174,8 +224,6 @@ println(string(body))
 	//	return
 	//}
 
-
-
 	//// 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
 	//tablesList := make([]models.Tables, 0)
 	//for _, table := range tmpItems.Tables {