update uniqueId & id

This commit is contained in:
Star 2024-11-26 18:02:09 +08:00
parent 00f1acfdb4
commit 9376e6d61a
3 changed files with 74 additions and 11 deletions

4
go.mod
View File

@ -3,9 +3,9 @@ module apigo.cc/gojs/util
go 1.18 go 1.18
require ( require (
apigo.cc/gojs v0.0.4 apigo.cc/gojs v0.0.7
github.com/ZZMarquis/gm v1.3.2 github.com/ZZMarquis/gm v1.3.2
github.com/emmansun/gmsm v0.29.2 github.com/emmansun/gmsm v0.29.3
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9 github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
github.com/ssgo/u v1.7.11 github.com/ssgo/u v1.7.11
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1

73
util.go
View File

@ -604,16 +604,75 @@ func init() {
panic(vm.NewGoError(err)) panic(vm.NewGoError(err))
} }
}, },
"id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(u.Id12())
},
"uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(u.UniqueId()) args := gojs.MakeArgs(&argsIn, vm)
size := args.Int(0)
var id string
if size >= 20 {
id = u.UniqueId()
} else if size >= 16 {
id = u.UniqueId()[0:16]
} else if size >= 14 {
id = u.ShortUniqueId()
} else if size >= 12 {
id = u.ShortUniqueId()[0:12]
} else {
id = u.UniqueId()
}
return vm.ToValue(id)
},
"uniqueIdL": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
size := args.Int(0)
var id string
if size >= 20 {
id = u.UniqueId()
} else if size >= 16 {
id = u.UniqueId()[0:16]
} else if size >= 14 {
id = u.ShortUniqueId()
} else if size >= 12 {
id = u.ShortUniqueId()[0:12]
} else {
id = u.UniqueId()
}
return vm.ToValue(strings.ToLower(id))
},
"id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
size := args.Int(0)
var id string
if size >= 12 {
id = u.Id12()
} else if size >= 10 {
id = u.Id10()
} else if size >= 8 {
id = u.Id8()
} else {
id = u.Id6()
}
return vm.ToValue(id)
},
"idL": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
size := args.Int(0)
var id string
if size >= 12 {
id = u.Id12()
} else if size >= 10 {
id = u.Id10()
} else if size >= 8 {
id = u.Id8()
} else {
id = u.Id6()
}
return vm.ToValue(strings.ToLower(id))
}, },
"token": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "token": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
size := 20 args := gojs.MakeArgs(&argsIn, vm)
if len(argsIn.Arguments) > 0 { size := args.Int(0)
size = u.Int(argsIn.Arguments[0].Export()) if size == 0 {
size = 20
} }
return vm.ToValue(u.MakeToken(size)) return vm.ToValue(u.MakeToken(size))
}, },

View File

@ -45,7 +45,9 @@ export default {
gzip, gzip,
gunzip, gunzip,
id, id,
idL,
uniqueId, uniqueId,
uniqueIdL,
token, token,
md5, md5,
sha1, sha1,
@ -127,8 +129,10 @@ function encryptRSA(data: any, pubKey: any): any { return '' }
function decryptRSA(data: any, priKey: any): any { return '' } function decryptRSA(data: any, priKey: any): any { return '' }
function gzip(data: any): string { return '' } function gzip(data: any): string { return '' }
function gunzip(data: string): any { return null } function gunzip(data: string): any { return null }
function id(): string { return '' } function id(size?: number): string { return '' }
function uniqueId(): string { return '' } function idL(size?: number): string { return '' }
function uniqueId(size?: number): string { return '' }
function uniqueIdL(size?: number): string { return '' }
function token(size: number): any { return '' } function token(size: number): any { return '' }
function md5(...data: any[]): any { return '' } function md5(...data: any[]): any { return '' }
function sha1(...data: any[]): any { return '' } function sha1(...data: any[]): any { return '' }