add function randomNumber

This commit is contained in:
陆柯 2022-03-22 14:33:24 +08:00
parent 39173e47bb
commit 2224e9c00f

View File

@ -117,6 +117,22 @@ module.exports = {
return parseInt(port)
},
randomNumber ({ length, min, max } = {}) {
// 长度为 length 的随机数字,或者 (min||0) <= num < max
var num = 0
if (typeof length === 'number' && length > 0) {
num = parseInt(Math.random() * Math.pow(10, length))
num = num.toString().padStart(length, '0')
} else if (typeof max === 'number' && max > 0) {
min = typeof min === 'number' && min >= 0 ? min : 0
num = parseInt(Math.random() * (max - min)) + min
} else {
// 如果 option 为空
num = Math.random()
}
return num
},
/**
* 用户编号转邀请码
*
@ -125,7 +141,7 @@ module.exports = {
* @return {*}
* @memberof TICrypto
*/
aiid2regcode(aiid) {
aiid2regcode (aiid) {
const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
const base = 16367
let num = (aiid + base) * (base - alphabet.length)
@ -147,7 +163,7 @@ module.exports = {
* @return {*}
* @memberof TICrypto
*/
regcode2aiid(code) {
regcode2aiid (code) {
if (typeof code === 'string' && /^[a-zA-Z0-9]+$/.test(code)) {
const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
const base = 16367