async random() {
let mima = []
let open = false
//8位密码
for (let i = 0; i < 8; i++) {
/*随机生成两个0-10的数字 通过对比大小 随机控制 当前位数生成字母还是数字
false:生成数字 true:生成随机大小写字母*/
open = parseInt(Math.random() * 10) > parseInt(Math.random() * 10)
let zm = mima.join('').match(/[A-z]/g) || [] //匹配字母 match返回一个已匹配到的数组
let nm = mima.join('').match(/[0-9]/g) || [] //匹配数字 match返回一个已匹配到的数组
/*通过返回的匹配到的数组
判断已需要的字母或字母是否满足最少字母或数字位数条件
条件满足,干预随机生成条件,反之继续运行
现在生成条件为4个数字4个随机大小写字母
*/
if (nm.length == 4) {
open = false
}
if (zm.length == 4) {
open = true
}
//执行生成
if (open) {
let number = this.randomNumberFn()
mima.push(number)
} else {
let code = await this.randomUnicodeFn()
mima.push(String.fromCharCode(code))
}
}
this.postData.password = mima.join('')
console.log(mima.join(''), '---')
},
//随机生成数字 0-10
randomNumberFn() {
return parseInt(Math.random() * 10);
},
/*
随机生成 大小写英文字母
如随机生成的是91-96之间的Unicode编码数字无法对应生成大小写 英文字母 则重新生成
*/
randomUnicodeFn() {
return new Promise((resolve, reject) => { //
let numberarry = /^9[1-6]{1}$/
let timer = setInterval(() => {
let code = parseInt(Math.random() * 57) + 65
if (!numberarry.test(code)) {
console.log(String.fromCharCode(code), code)
clearInterval(timer)
resolve(code)
}
}, 100)
})
}