package common import ( "fmt" "github.com/gin-gonic/gin" "net/http" "regexp" "strconv" "strings" "wss-pool/config" "wss-pool/internal" "wss-pool/internal/data" "wss-pool/internal/redis" "wss-pool/logging/applogger" ) const ( StockProject string = "stock" CoinProject string = "coin" StockOn string = "1" StockIndexOn int = 1 StockIndexPrefix string = "StockIndex" StockOption string = "Option" StockOptionList string = "List" StockOptionInfo string = "Info" ) var CountryStartTime = map[string]int64{ "India": 42300000, "Thailand": 39000000, "Indonesia": 36000000, "Malaysia": 32400000, "Singapore": 32400000, "UK": 54000000, "France": 54000000, "Germany": 54000000, "Brazil": 75600000, "Japan": 42300000, } var StockToPHPMap = map[string]string{ "US": "US", "Thailand": "THA", "India": "IN", "Indonesia": "IDN", "Malaysia": "MYS", "Singapore": "SGD", "HongKong": "HKD", "UK": "UK", "Germany": "EUR", "France": "FUR", "Brazil": "BR", "Japan": "JP", } var path = map[string]bool{ "/spots/update/keep": true, "/stock/spots/update/keep": true, "/stock/spots/list/new/add": true, "/spots/list/new/add": true, "/spots/news/add": true, "/spots/index/list/new/add": true, "/stock/spots/index/list/new/add": true, "/option/php/list": true, "/stock/option/php/list": true, "/spots/php/update": true, "/stock/spots/php/update": true, } type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } // JWTAuthMiddleware 基于JWT的认证中间件 func JWTAuthMiddleware() func(c *gin.Context) { return func(c *gin.Context) { if path[c.FullPath()] { c.Next() return } token := strings.TrimSpace(c.Request.Header.Get("token")) if token == "" { c.Abort() JsonResult(401, "can't find the token", "", c) return } //applogger.Debug("鉴权Token用户:%v", fmt.Sprintf("TOKEN:USER:%s", token)) userId, _ := redis.Get_Cache_Data(fmt.Sprintf("TOKEN:USER:%s", token)) if userId == "" || userId == "0" { c.Abort() JsonResult(401, "token expired", "", c) return } } } func CheckToken(token string) bool { if token == "" { return false } userId, _ := redis.Get_Cache_Data(fmt.Sprintf("TOKEN:USER:%s", token)) if userId == "" || userId == "0" { return false } return true } func JsonResult(code int, msg string, data interface{}, c *gin.Context) { // 开始时间 c.JSON(http.StatusOK, Response{ code, msg, data, }) } func IsLetter(str string) bool { reg := regexp.MustCompile(`^[a-zA-Z0-9&-]+$`) return reg.MatchString(str) } func DelRes(hashListName, str string) { redis.RedisClient = redis.RedisInit(config.Config.Redis.DbEleven) keys, _ := redis.HGetAll(hashListName) for key, _ := range keys { if strings.Contains(key, str) { redis.HDel(hashListName, key) fmt.Println(key) } } //if len(k)> 0 { // redis.HDel(hashListName, strings.Join(k, ",")) // k = make([]string,0) //} } func IsExistStock(country, code string) bool { key := fmt.Sprintf("%s:STOCK:LIST:%s", StockToPHPMap[country], code) status, _ := redis.Hget(key, "status") if status == StockOn { return true } return false } func IsExistStockNew(country, code string, redisIp string) bool { red := redis.RedisClientMap[redisIp] key := fmt.Sprintf("%s:STOCK:LIST:%s", StockToPHPMap[country], code) status, _ := redis.HGetNew(key, "status", red) if status == StockOn { return true } return false } func IsExistOption(country, code string) (bool, float64) { key := fmt.Sprintf("%s:OPTION:LIST:%s", StockToPHPMap[country], code) status, _ := redis.Hget(key, "status") //fmt.Println(country,code,status) if status == StockOn { rate, _ := redis.Hget(key, "rate") rateFloat, _ := strconv.ParseFloat(rate, 64) return true, rateFloat } return false, 0 } func TgBotSendMsg(msg string) { if _, err := internal.HttpPost(config.Config.TgBot.URL, fmt.Sprintf(`{"text":"%s","chat_id":%d}`, msg, config.Config.TgBot.ChatId)); err != nil { applogger.Error("TgBotSendMsg", err) } } func UPdateAll(country string) { data.Mgo_init(config.Config.Mongodb) var per = []string{ "1day", "1week", "1mon", } for _, v := range per { UPdateTime(1696089600000, 1700796517000, country, v) } } func UPdateTime(from, to int64, country, period string) { //filter := bson.M{"timestamp": bson.M{"$gte": from, "$lte": to}} //tableName := data.GetStockSouthAsiaTableName(country, period) //res := make([]model.StockMogoParam, 0) //projection := bson.M{"symbol": 1, "stock_code": 1, "country": 1, "timestamp": 1} //sort := bson.M{"timestamp": 1} //data.MgoFindProjectionRes(tableName, filter, projection, sort, &res, 0) //if len(res) <= 0 { // applogger.Error(" no data", period) // return //} //var key = make(map[int64]bool) //for _, v := range res { // utcTime := time.Unix(v.Ts/1000, 0) // location, _ := time.LoadLocation("Asia/Singapore") // t := utcTime.In(location) // if t.Hour() != 0 { // continue // } // l := len(key) // key[v.Ts] = true // if l == len(key){ // continue // } // fmt.Println(v.Ts) // filter = bson.M{"timestamp": bson.M{"$eq": v.Ts}} // update := bson.D{{"$set", bson.D{ // {"timestamp", v.Ts + CountryStartTime[country]}, // }}} // applogger.Info("GetTimeNewPriceAll info: %v %v", update,v) // fmt.Println(data.GetStockSouthAsiaTableName(country, period)) // if err := data.MgoUpdateMany(data.GetStockSouthAsiaTableName(country, period), filter,update); err != nil { // applogger.Error("stock MgoInsertMany err:%v", err) // } //} }