密码 随机生成

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)
                })
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值