Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
用于生成唯一的、递增的Id。生成的规则如下:
|
||||
1、生成的Id包含一个固定前缀值
|
||||
2、为了生成尽可能多的不重复数字,所以使用int64来表示一个数字,其中:
|
||||
0 000000000000000 0000000000000000000000000000 00000000000000000000
|
||||
第一部分:1位,固定为0
|
||||
第二部分:共TimeBit位,表示当前时间距离基础时间的秒数。范围为[0, math.Pow(2, TimeBit)),以2020-1-1 00:00:00为基准
|
||||
第三部分:共IdentifierBit位,表示固定唯一标识(机器号或者服务器Id等)。范围为[0, math.Pow(2, IdentifierBit))
|
||||
第四部分:共SeedBit位,表示自增种子。范围为[0, math.Pow(2, SeedBit))
|
||||
3、总体而言,此规则支持每秒生成math.Pow(2, SeedBit)个不同的数字,并且在math.Pow(2, TimeBit)/60/60/24/365年的时间范围内有效
|
||||
*/
|
||||
|
||||
/*
|
||||
修改记录:
|
||||
2020-03-04 14:30:00 调整了时间和唯一标识在Id中的位置,以便生成递增的Id
|
||||
2020-04-20 21:10:00 同步了C#版本的逻辑
|
||||
*/
|
||||
|
||||
package idUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeIdentifierSeedGenerator struct {
|
||||
timeBit int32 // 时间戳所占的位数
|
||||
timeBitOffset int32 // 时间戳的偏移位数
|
||||
startTimeStamp int64 // 起始时间戳
|
||||
endTimeStamp int64 // 结束时间戳
|
||||
|
||||
identifier int64 // 唯一标识
|
||||
identifierBit int32 // 唯一标识(机器号或者服务器Id等)所占的位数
|
||||
identifierBitOffset int32 // 唯一标识的偏移位数
|
||||
|
||||
seedBit int32 // 自增种子所占的位数
|
||||
seedBitOffset int32 // 自增种子的偏移位数
|
||||
minSeed int64 // 最小的种子值
|
||||
maxSeed int64 // 最大的种子值
|
||||
currSeed int64 // 当前种子值
|
||||
|
||||
mutex sync.Mutex // 锁对象
|
||||
}
|
||||
|
||||
func (this *TimeIdentifierSeedGenerator) getTimeStamp() (int64, error) {
|
||||
if time.Now().Unix() > this.endTimeStamp {
|
||||
return 0, fmt.Errorf("Time's value is out of scope")
|
||||
}
|
||||
|
||||
return time.Now().Unix() - this.startTimeStamp, nil
|
||||
}
|
||||
|
||||
func (this *TimeIdentifierSeedGenerator) getNewSeed() int64 {
|
||||
this.mutex.Lock()
|
||||
defer this.mutex.Unlock()
|
||||
|
||||
if this.currSeed >= this.maxSeed {
|
||||
this.currSeed = this.minSeed
|
||||
} else {
|
||||
this.currSeed = this.currSeed + 1
|
||||
}
|
||||
|
||||
return this.currSeed
|
||||
}
|
||||
|
||||
// 生成新的Id
|
||||
// identifier:Id的唯一标识值。取值范围必须可以用创建对象时指定的唯一标识值的位数来表示,否则会返回参数超出范围的错误
|
||||
// 返回值:
|
||||
// 新的Id
|
||||
// 错误对象
|
||||
func (this *TimeIdentifierSeedGenerator) GenerateNewId() (int64, error) {
|
||||
timestamp, err := this.getTimeStamp()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
seed := this.getNewSeed()
|
||||
id := (timestamp << this.timeBitOffset) | (this.identifier << this.identifierBitOffset) | (seed << this.seedBitOffset)
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// 创建新的Id生成器对象(为了保证Id的唯一,需要保证生成的对象全局唯一)
|
||||
// timeBit + identifierBit + seedBit <= 63
|
||||
// timeBit:时间的位数
|
||||
// identifier:id唯一标识
|
||||
// identifierBit:id唯一标识(机器号或者服务器Id等)的位数
|
||||
// seedBit:自增种子的位数
|
||||
// 返回值:
|
||||
// 新的Id生成器对象
|
||||
// 错误对象
|
||||
func NewTimeIdentifierSeedGenerator(timeBit int32, identifier int64, identifierBit, seedBit int32) (*TimeIdentifierSeedGenerator, error) {
|
||||
// 之所以使用63位而不是64,是为了保证值为正数
|
||||
if timeBit+identifierBit+seedBit > 63 {
|
||||
return nil, fmt.Errorf("总位数%d超过63位,请调整所有值的合理范围。", timeBit+identifierBit+seedBit)
|
||||
}
|
||||
|
||||
if identifier < 0 || identifier > int64(math.Pow(2, float64(identifierBit)))-1 {
|
||||
return nil, fmt.Errorf("唯一标识值溢出,有效范围为【0,%d】", int64(math.Pow(2, float64(identifierBit)))-1)
|
||||
}
|
||||
|
||||
startTimeStamp := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.Local).Unix()
|
||||
obj := &TimeIdentifierSeedGenerator{
|
||||
timeBit: timeBit,
|
||||
startTimeStamp: startTimeStamp,
|
||||
endTimeStamp: startTimeStamp + int64(math.Pow(2, float64(timeBit))) - 1,
|
||||
identifier: identifier,
|
||||
identifierBit: identifierBit,
|
||||
seedBit: seedBit,
|
||||
minSeed: 0,
|
||||
maxSeed: int64(math.Pow(2, float64(seedBit))) - 1,
|
||||
currSeed: 0,
|
||||
}
|
||||
|
||||
obj.seedBitOffset = 0
|
||||
obj.identifierBitOffset = obj.seedBitOffset + obj.seedBit
|
||||
obj.timeBitOffset = obj.identifierBitOffset + obj.identifierBit
|
||||
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package managecenterMgr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"Framework/managecenterModel"
|
||||
. "Framework/managecenterModel"
|
||||
"goutil/logUtil"
|
||||
"goutil/webUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
whiteListMap = make(map[int32]map[string]*WhiteList, 64)
|
||||
whiteListHash string
|
||||
)
|
||||
|
||||
// 重新加载白名单
|
||||
func reloadWhiteList(isInit bool) error {
|
||||
//logUtil.DebugLog("开始刷新白名单列表")
|
||||
|
||||
url := getManageCenterUrl("/API/UserWhiteList.ashx")
|
||||
|
||||
// 定义请求参数
|
||||
postDict := make(map[string]string)
|
||||
postDict["HashValue"] = whiteListHash
|
||||
|
||||
//请求url,请求头
|
||||
header := webUtil.GetFormHeader()
|
||||
transport := webUtil.NewTransport()
|
||||
transport.DisableKeepAlives = true
|
||||
transport = webUtil.GetTimeoutTransport(transport, 30)
|
||||
|
||||
statusCode, returnBytes, err := webUtil.PostMapData(url, postDict, header, transport)
|
||||
//statusCode, returnBytes, err := webUtil.PostMapData(url, postDict, header, nil)
|
||||
if err != nil {
|
||||
logUtil.ErrorLog(fmt.Sprintf("获取白名单列表出错,url:%s,错误信息为:%s", url, err))
|
||||
return err
|
||||
}
|
||||
if statusCode != 200 {
|
||||
logUtil.ErrorLog(fmt.Sprintf("获取白名单列表出错,url:%s,错误码为:%d", url, statusCode))
|
||||
return err
|
||||
}
|
||||
|
||||
// 解析返回值
|
||||
returnObj := new(ReturnObject)
|
||||
if err = json.Unmarshal(returnBytes, &returnObj); err != nil {
|
||||
logUtil.ErrorLog(fmt.Sprintf("获取白名单列表出错,反序列化返回值出错,错误信息为:%s, str:%s", err, string(returnBytes)))
|
||||
return err
|
||||
}
|
||||
|
||||
// 判断返回状态是否为成功
|
||||
if returnObj.Code != 0 {
|
||||
// 数据没有变化,所以没有获取到新的数据,不能算错误。
|
||||
if returnObj.Code == 47 || returnObj.Message == "DataNotChanged" {
|
||||
//如果本地集合为空,且数据又没变化时,重新初始化一下本地hash值
|
||||
if len(whiteListMap) == 0 {
|
||||
whiteListHash = ""
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
msg := fmt.Sprintf("获取白名单列表出错,返回状态:%d,信息为:%s", returnObj.Code, returnObj.Message)
|
||||
logUtil.ErrorLog(msg)
|
||||
return errors.New(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析Data
|
||||
tmpWhiteList := make([]*WhiteList, 0, 128)
|
||||
if data, ok := returnObj.Data.(string); !ok {
|
||||
msg := "获取白名单列表出错,返回的数据不是string类型"
|
||||
logUtil.ErrorLog(msg)
|
||||
return errors.New(msg)
|
||||
} else {
|
||||
if err = json.Unmarshal([]byte(data), &tmpWhiteList); err != nil {
|
||||
logUtil.ErrorLog(fmt.Sprintf("获取白名单列表出错,反序列化数据出错,错误信息为:%s", err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
//logUtil.DebugLog(fmt.Sprintf("刷新白名单信息结束,白名单数量:%d", len(tmpWhiteList)))
|
||||
|
||||
tmpWhiteListMap := make(map[int32]map[string]*WhiteList, 64)
|
||||
for _, item := range tmpWhiteList {
|
||||
if _, exist := tmpWhiteListMap[item.PartnerId]; !exist {
|
||||
tmpWhiteListMap[item.PartnerId] = make(map[string]*WhiteList, 8)
|
||||
}
|
||||
|
||||
tmpWhiteListMap[item.PartnerId][item.UserId] = item
|
||||
}
|
||||
|
||||
// 赋值给最终的whiteListMap
|
||||
whiteListMap = tmpWhiteListMap
|
||||
whiteListHash = returnObj.HashValue
|
||||
|
||||
//通知变更
|
||||
mcDataChangeCallBack(managecenterModel.UserWhiteListData, isInit)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 判断用户是否在白名单里面
|
||||
// partnerId: 合作商ID
|
||||
// userId: userId
|
||||
func IsInWhiteList(partnerId int32, userId string) bool {
|
||||
subWhiteListMap, exist := whiteListMap[partnerId]
|
||||
if !exist {
|
||||
return false
|
||||
}
|
||||
|
||||
_, exist = subWhiteListMap[userId]
|
||||
return exist
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
这个包包含安全方面的util对象和方法,例如md5,sha1,rsa等。
|
||||
使用时需要先import "goutil.security"。
|
||||
|
||||
当前这里面包括有2个对象md5和sha1,在使用时,可以参照如下方式
|
||||
s := "hello world"
|
||||
result := Md5String(s, true)
|
||||
*/
|
||||
package securityUtil
|
||||
|
||||
//此文档是专为写包注释而添加的,无实际意义。因此其它文件也不用再添加包注释了
|
||||
Reference in New Issue
Block a user