Compare commits

..

5 Commits

Author SHA1 Message Date
cd72620e49 add HttpPostJSON 2025-05-23 10:45:28 +08:00
zhaoxiaorong
5bb23deb3b fix 2025-05-21 20:13:55 +08:00
2c713adc16 add sync map data. 2025-05-03 15:49:16 +08:00
zhaoxiaorong
21f09ea41e fix 2025-04-24 17:52:29 +08:00
4d06ad3e8b add err code 2025-04-19 20:14:37 +08:00
7 changed files with 140 additions and 17 deletions

View File

@@ -8,7 +8,6 @@ type Base struct {
BindIP string `yaml:"BindIP"` // 绑定IP BindIP string `yaml:"BindIP"` // 绑定IP
Addr string `yaml:"Addr"` Addr string `yaml:"Addr"`
OnMicroService bool `yaml:"OnMicroService"` OnMicroService bool `yaml:"OnMicroService"`
LoginUrl string `yaml:"LoginUrl"`
} }
type DBConf struct { type DBConf struct {

58
data/map_float.go Normal file
View File

@@ -0,0 +1,58 @@
package data
import (
"sync"
)
var (
// Cache
CacheMapFloat *MapFloat
)
// lock
type MapFloat struct {
sync.RWMutex
Data map[string]float64
}
func NewMapFloat() *MapFloat {
return &MapFloat{
Data: make(map[string]float64),
}
}
func (c *MapFloat) All() map[string]float64 {
c.RLock()
defer c.RUnlock()
return c.Data
}
func (c *MapFloat) Get(key string) float64 {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *MapFloat) Set(key string, val float64) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *MapFloat) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}

51
data/map_string.go Normal file
View File

@@ -0,0 +1,51 @@
package data
import (
"sync"
)
var (
// Cache
CacheMapString *MapString
)
// lock
type MapString struct {
sync.RWMutex
Data map[string]string
}
func NewMapString() *MapString {
return &MapString{
Data: make(map[string]string),
}
}
func (c *MapString) Get(key string) string {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return ""
}
return vals
}
func (c *MapString) Set(key, val string) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *MapString) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}

View File

@@ -15,13 +15,18 @@ var (
// standard error code ,start:110 // standard error code ,start:110
var ( var (
ErrEmpty = NewError(110, "Data Is Empty") ErrEmpty = NewError(110, "Data Is Empty")
ErrRequestParse = NewError(111, "Request Parse Fail") ErrRequestParse = NewError(111, "Request Parse Fail")
ErrRequestMust = NewError(112, "Request Params Required") ErrRequestMust = NewError(112, "Request Params Required")
ErrPermission = NewError(113, "Permission Denied") ErrPermission = NewError(113, "Permission Denied")
ErrJsonUnmarshal = NewError(114, "Json Unmarshal Fail") ErrJsonUnmarshal = NewError(114, "Json Unmarshal Fail")
ErrJsonMarshal = NewError(115, "Json Marshal Fail") ErrJsonMarshal = NewError(115, "Json Marshal Fail")
ErrInternal = NewError(116, "Internal Server Error") ErrInternal = NewError(116, "Internal Server Error")
ErrPassword = NewError(117, "Password Incorrect")
ErrAccountNotFound = NewError(118, "Account Not Found")
ErrAccountDisabled = NewError(119, "Account Disabled")
ErrDisabled = NewError(120, "Status Disabled")
ErrRecordNotFound = NewError(121, "Record Not Found")
) )
// jwt error code ,start:130 // jwt error code ,start:130

View File

@@ -2,7 +2,6 @@ package middleware
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
@@ -33,14 +32,14 @@ func JwtAuth(redis *redis.RedisClient) gin.HandlerFunc {
} }
// 从redis 获取token,判断当前redis 是否为空 // 从redis 获取token,判断当前redis 是否为空
tokenKey := fmt.Sprintf("%d-%s-%s", claims.ID, claims.Role, "token") // tokenKey := fmt.Sprintf("%d-%s-%s", claims.ID, claims.Role, "token")
redisToken := redis.Client.Get(redis.Ctx, tokenKey) // redisToken := redis.Client.Get(redis.Ctx, tokenKey)
if redisToken.Val() == "" { // if redisToken.Val() == "" {
log.Println("redis异常", "Token status unauthorized") // log.Println("redis异常", "Token status unauthorized")
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token status unauthorized"}) // c.JSON(http.StatusUnauthorized, gin.H{"error": "Token status unauthorized"})
c.Abort() // c.Abort()
return // return
} // }
// 将解析后的 Token 存储到上下文中 // 将解析后的 Token 存储到上下文中
c.Set("Auth", claims) c.Set("Auth", claims)

View File

@@ -15,4 +15,5 @@ var (
Type_Delete string = "delete" Type_Delete string = "delete"
Type_Query string = "query" Type_Query string = "query"
Type_Other string = "other" Type_Other string = "other"
Type_Create string = "create"
) )

View File

@@ -2,6 +2,7 @@ package utils
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -121,6 +122,15 @@ func HttpGet(url string) ([]byte, error) {
return body, err return body, err
} }
func HttpPostJSON(url string, header map[string]string, data map[string]any) ([]byte, error) {
bytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
return HttpPost(url, header, bytes)
}
func HttpPost(url string, header map[string]string, data []byte) ([]byte, error) { func HttpPost(url string, header map[string]string, data []byte) ([]byte, error) {
var err error var err error
reader := bytes.NewBuffer(data) reader := bytes.NewBuffer(data)