123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package tick
- import (
- "net/http"
- "tickserver/server/market"
- )
- import "io/ioutil"
- import "crypto/rand"
- import "encoding/json"
- import "encoding/hex"
- //import "log"
- //在检查之前,确定已经登录
- type JsonResponse struct {
- Result interface{} `json:"result"`
- Err string `json:"err"`
- Code int `json:"code"`
- }
- func NewJsonResponse(data interface{}, err string, code int) *JsonResponse {
- resp := &JsonResponse{}
- resp.Result = data
- resp.Err = err
- resp.Code = code
- return resp
- }
- func readRequest(w http.ResponseWriter, r *http.Request) ([]byte, error) {
- data, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- func decodeRequestInner(w http.ResponseWriter, r *http.Request, data []byte, v interface{}) error {
- err := json.Unmarshal(data, v)
- if err != nil {
- return err
- }
- return err
- }
- func decodeRequest(w http.ResponseWriter, r *http.Request, v interface{}) error {
- data, err := readRequest(w, r)
- if err != nil {
- return err
- }
- return decodeRequestInner(w, r, data, v)
- }
- func sendErrResponse(w http.ResponseWriter, r *http.Request, code int, err string) {
- w.WriteHeader(code)
- res := NewJsonResponse(nil, err, -1)
- s, _ := json.Marshal(res)
- w.Write(s)
- }
- func sendResponse(w http.ResponseWriter, r *http.Request, data interface{}) {
- w.WriteHeader(http.StatusOK)
- res := NewJsonResponse(data, "", 0)
- s, err := json.Marshal(res)
- if err != nil {
- sendErrResponse(w, r, 200, err.Error())
- return
- }
- w.Write(s)
- }
- func getId() string {
- var b [5]byte
- rand.Read(b[:])
- return hex.EncodeToString(b[:])
- }
- func checkTime(exId string, ts int64) bool {
- var tis []tInterval
- switch exId {
- case "SHFE": // 上海期货交易所
- tis = shefTi
- case "CFFEX": // 中国金融期货交易所
- tis = cffexTi
- case "DCE", "CZCE": //大连、郑州商品交易所
- tis = decAndCzceTi
- default:
- panic("can't go here")
- }
- t := market.GetTime(ts)
- //log.Println("debug checktime", t)
- mm := t.Hour()*60 + t.Minute()
- for _, ti := range tis {
- m1 := ti.st.hour*60 + ti.st.minute
- m2 := ti.et.hour*60 + ti.et.minute
- if mm >= m1 && mm <= m2 {
- return true
- }
- }
- return false
- }
|