diff --git a/apps/device/api/device.go b/apps/device/api/device.go index b4bcda3..4c4861e 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -186,7 +186,7 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) { data.OrgId = rc.LoginAccount.OrganizationId list, _ := p.DeviceApp.FindList(entity.Device{Name: data.Name}) biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("名称%s已存在,设置其他命名", data.Name)) - data.Id = utils.GenerateID() + data.Id = utils.GenerateTdID("d") data.LinkStatus = global.INACTIVE data.LastAt = time.Now() data.Protocol = product.ProtocolName diff --git a/apps/device/api/product.go b/apps/device/api/product.go index ba90499..568494e 100644 --- a/apps/device/api/product.go +++ b/apps/device/api/product.go @@ -107,7 +107,7 @@ func (p *ProductApi) GetProduct(rc *restfulx.ReqCtx) { func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) { var data entity.Product restfulx.BindJsonAndValid(rc, &data) - data.Id = utils.GenerateID() + data.Id = utils.GenerateTdID("p") data.Owner = rc.LoginAccount.UserName data.OrgId = rc.LoginAccount.OrganizationId // 如果未设置规则链,默认为主链 diff --git a/kit/utils/str_utils.go b/kit/utils/str_utils.go index 174a9db..1a5e849 100644 --- a/kit/utils/str_utils.go +++ b/kit/utils/str_utils.go @@ -167,3 +167,35 @@ func GenerateID() string { } return base64.URLEncoding.EncodeToString(id)[:10] } + +const ( + letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +) + +func RandString(n int) string { + rand.Seed(time.Now().UnixNano()) + output := make([]byte, n) + // We will take n bytes, one byte for each character of output. + randomness := make([]byte, n) + // read all random + _, err := rand.Read(randomness) + if err != nil { + panic(err) + } + l := len(letterBytes) + // fill output + for pos := range output { + // get random item + random := randomness[pos] + // random % 64 + randomPos := random % uint8(l) + // put into output + output[pos] = letterBytes[randomPos] + } + + return string(output) +} + +func GenerateTdID(px string) string { + return px + RandString(9) +}