package utils import ( "time" "github.com/dgrijalva/jwt-go" ) var ( sn int64 // 序列号占 12 位,十进制范围是 [ 0, 4095 ] lastTimeStamp int64 // 上次的时间戳(毫秒级), 1秒=1000毫秒, 1毫秒=1000微秒,1微秒=1000纳秒 ) func init() { lastTimeStamp = time.Now().UnixNano() / 1000000 } type Claims struct { IsAdmin int `json:"isAdmin"` UserId int `json:"userId"` UserName string `json:"userName"` jwt.StandardClaims } type ToolsLogic struct { } // GetOnlyId // @函数名:GetOnlyId // @函数功能描述: 获取唯一id // @对象名:c // @参数定义:appId // @返回值:int64 // func (c *ToolsLogic) GetOnlyId(appId int64) int64 { if appId == 0 { return 0 } nowTime := time.Now().UnixNano() / 1000000 appId = appId & 0x3FF sfOne := nowTime << 22 sfTwo := appId << 12 //每次累加,减小时间回滚发生重复的概率 sn++ sfThree := sn & 0xFFF return sfOne | sfTwo | sfThree } // ParseOnlyId // @函数名:ParseOnlyId // @函数功能描述: 通过唯一id获取appId // @对象名:c // @参数定义:mid // @返回值:appId // func (c *ToolsLogic) ParseOnlyId(mid int64) (appId int64) { if mid == 0 { return 0 } appId = mid & 0x3FF000 return appId >> 12 } // ParseToken // @函数名:ParseToken // @函数功能描述: 传入token和公钥文件路径 // @对象名:c // @参数定义:token // @参数定义:path // func (c *ToolsLogic) ParseToken(token, path string) (*Claims, error) { var ( resp = new(Claims) ) err := RSADeToken(token, path, resp) if err != nil { return resp, err } return resp, nil } func (c *ToolsLogic) EncryptMsg(message []byte, key []byte, isDebug ...bool) (string, error) { return EncryptByAes(message, key, isDebug...) } func (c *ToolsLogic) DecryptMsg(message string, key []byte, isDebug ...bool) ([]byte, error) { return DecryptByAes(message, key, isDebug...) }