Compare commits

...

18 Commits

Author SHA1 Message Date
yanweidong fc7c1e87a6 fix licence watch 2025-05-28 15:58:28 +08:00
zhaoxiaorong 8c62f529e3 Merge branch 'main' of https://git.apinb.com/bsm-sdk/core 2025-05-27 09:41:00 +08:00
zhaoxiaorong 9d3b3404e4 fix 2025-05-27 09:40:56 +08:00
yanweidong bfccf4d468 fix licence 2025-05-26 22:11:47 +08:00
yanweidong cd72620e49 add HttpPostJSON 2025-05-23 10:45:28 +08:00
zhaoxiaorong 5bb23deb3b fix 2025-05-21 20:13:55 +08:00
david.yan 2c713adc16 add sync map data. 2025-05-03 15:49:16 +08:00
zhaoxiaorong 21f09ea41e fix 2025-04-24 17:52:29 +08:00
yanweidong 4d06ad3e8b add err code 2025-04-19 20:14:37 +08:00
yanweidong 52a81a404e fix infra response 2025-04-18 19:11:50 +08:00
yanweidong 6cd06d86bc add std_owner 2025-04-17 17:18:58 +08:00
zhaoxiaorong ca9f7047c6 fix 兼容性调整 2025-04-15 21:49:17 +08:00
zhaoxiaorong 2de73fea00 fix 兼容性调整 2025-04-15 20:50:28 +08:00
zhaoxiaorong 4b73f086b1 dev oplog 2025-04-11 18:14:07 +08:00
zhaoxiaorong c08950c10a fix 2025-04-11 18:06:08 +08:00
zhaoxiaorong d691648916 fix 2025-04-11 17:53:50 +08:00
zhaoxiaorong 8060cdb508 fix 2025-04-11 17:50:06 +08:00
zhaoxiaorong 50c23df124 fix 2025-04-11 17:44:49 +08:00
12 changed files with 215 additions and 81 deletions

View File

@ -8,7 +8,6 @@ type Base struct {
BindIP string `yaml:"BindIP"` // 绑定IP
Addr string `yaml:"Addr"`
OnMicroService bool `yaml:"OnMicroService"`
LoginUrl string `yaml:"LoginUrl"`
}
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
var (
ErrEmpty = NewError(110, "Data Is Empty")
ErrRequestParse = NewError(111, "Request Parse Fail")
ErrRequestMust = NewError(112, "Request Params Required")
ErrPermission = NewError(113, "Permission Denied")
ErrJsonUnmarshal = NewError(114, "Json Unmarshal Fail")
ErrJsonMarshal = NewError(115, "Json Marshal Fail")
ErrInternal = NewError(116, "Internal Server Error")
ErrEmpty = NewError(110, "Data Is Empty")
ErrRequestParse = NewError(111, "Request Parse Fail")
ErrRequestMust = NewError(112, "Request Params Required")
ErrPermission = NewError(113, "Permission Denied")
ErrJsonUnmarshal = NewError(114, "Json Unmarshal Fail")
ErrJsonMarshal = NewError(115, "Json Marshal Fail")
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

View File

@ -8,29 +8,29 @@ import (
var Response Reply
type Reply struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
Code int32 `json:"code"`
Message string `json:"message"`
Result any `json:"result"`
}
func (reply *Reply) Success(ctx *gin.Context, data any) {
reply.Code = 200
reply.Data = data
reply.Msg = ""
reply.Code = 0
reply.Result = data
reply.Message = ""
if data == nil {
reply.Data = ""
reply.Result = ""
}
ctx.JSON(200, reply)
}
func (reply *Reply) Error(ctx *gin.Context, err error) {
reply.Code = 500
reply.Data = ""
reply.Result = ""
// Status code defaults to 500
e, ok := status.FromError(err)
if ok {
reply.Code = int(e.Code())
reply.Code = int32(e.Code())
}
reply.Msg = e.Message()
reply.Message = e.Message()
// Send error
ctx.JSON(200, reply)

View File

@ -58,8 +58,8 @@ var (
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const (
signKey = "8E853B589944FF7A56BEF02AAA51D6F4"
LICENCE_KEY = "TRAIN_LICENCE_KEY"
signKey = "1F36659EC27CFFF849E068EA80B1A4CA"
LICENCE_KEY = "BLOCKS_KEY"
)
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -69,13 +69,13 @@ func init() {
}
func WatchCheckLicence(licPath, licName string) {
for {
utils.SetInterval(func() {
if CheckLicence(licPath, licName) == false {
log.Println("授权文件失效,请重新部署授权文件:", licPath)
os.Exit(99)
}
time.Sleep(time.Hour * 1)
}
}, time.Hour*1)
}
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

View File

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

14
oplog/new.go Normal file
View File

@ -0,0 +1,14 @@
package oplog
import (
"encoding/json"
"git.apinb.com/bsm-sdk/core/utils"
)
func New(endpoint string, data []*LogItem) {
jsonBytes, _ := json.Marshal(data)
go utils.HttpPost(endpoint, nil, jsonBytes)
}

View File

@ -1,27 +0,0 @@
package oplog
import (
"bytes"
"encoding/json"
"net/http"
)
func PostLog(data any, path string) (resp *http.Response, err error) {
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", path, bytes.NewBuffer(jsonBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return
}

19
oplog/types.go Normal file
View File

@ -0,0 +1,19 @@
package oplog
type LogItem struct {
OpID uint `json:"op_id"`
OpName string `json:"op_name"`
OpType string `json:"op_type"`
Text string `json:"text"`
}
var (
Type_Login string = "login"
Type_Logout string = "logout"
Type_Register string = "register"
Type_Update string = "update"
Type_Delete string = "delete"
Type_Query string = "query"
Type_Other string = "other"
Type_Create string = "create"
)

View File

@ -10,62 +10,68 @@ type (
// sql options
SqlOptions struct {
MaxIdleConns int
MaxOpenConns int
MaxIdleConns int `gorm:"column:max_idle_conns;" json:"max_idle_conns"`
MaxOpenConns int `gorm:"column:max_open_conns;" json:"max_open_conns"`
ConnMaxLifetime time.Duration
LogStdout bool
Debug bool
LogStdout bool `gorm:"column:log_stdout;" json:"log_stdout"`
Debug bool `gorm:"column:debug;" json:"debug"`
}
// standard ID,Identity definition.
Std_IDIdentity struct {
ID uint `gorm:"primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;default:uuid_generate_v4()" json:"identity"` // 唯一标识24位NanoID,36位为ULID
ID uint `gorm:"column:id;primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为ULID
}
// standard ID,Created,Updated,Deleted definition.
Std_IICUDS struct {
ID uint `gorm:"primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;default:uuid_generate_v4()" json:"identity"` // 唯一标识24位NanoID,36位为ULID
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
Status int8 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
ID uint `gorm:"column:id;primarykey;" json:"id"`
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为ULID
CreatedAt time.Time `gorm:"column:created_at;" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index;" json:"deleted_at"`
Status int8 `gorm:"column:status;default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
}
// standard ID,Identity,Created,Updated,Deleted,Status definition.
Std_ICUD struct {
ID uint `gorm:"primarykey;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
ID uint `gorm:"column:id;primarykey;" json:"id"`
CreatedAt time.Time `gorm:"column:created_at;" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index;" json:"deleted_at"`
}
// standard ID,Created definition.
Std_IdCreated struct {
ID uint `gorm:"primarykey;" json:"id"`
CreatedAt time.Time `json:"created_at"`
ID uint `gorm:"column:id;primarykey;" json:"id"`
CreatedAt time.Time `gorm:"column:created_at;" json:"created_at"`
}
// standard PassportID,PassportIdentity definition.
Std_Passport struct {
PassportID uint `gorm:"column:passport_id;Index;" json:"passport_id"`
PassportIdentity string `gorm:"column:passport_identity;type:varchar(36);Index;default:uuid_generate_v4()" json:"passport_identity"` // 用户唯一标识24位NanoID,36位为ULID
PassportIdentity string `gorm:"column:passport_identity;type:varchar(36);Index;" json:"passport_identity"` // 用户唯一标识24位NanoID,36位为UUID
}
// standard OwnerID,OwnerIdentity definition.
Std_Owner struct {
OwnerID uint `gorm:"column:owner_id;Index;" json:"owner_id"`
OwnerIdentity string `gorm:"column:owner_identity;type:varchar(36);Index;" json:"owner_identity"` // 用户唯一标识24位NanoID,36位为UUID
}
// standard ID definition.
Std_ID struct {
ID uint `gorm:"primarykey;" json:"id"`
ID uint `gorm:"column:id;primarykey;" json:"id"`
}
// standard Identity definition.
Std_Identity struct {
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;default:uuid_generate_v4()" json:"identity"` // 唯一标识24位NanoID,36位为ULID
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识24位NanoID,36位为UUID
}
// standard Status definition.
Std_Status struct {
Status int64 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
Status int64 `gorm:"column:status;default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
}
)

View File

@ -2,6 +2,7 @@ package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@ -121,6 +122,15 @@ func HttpGet(url string) ([]byte, error) {
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) {
var err error
reader := bytes.NewBuffer(data)