123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /*
- * @Author: Badguy
- * @Date: 2022-02-15 17:16:32
- * @LastEditTime: 2022-02-18 17:57:54
- * @LastEditors: your name
- * @Description: 账号管理方法
- * have a nice day!
- */
- export function generatePassword(rules, minNum, maxNum) {
- var rulesArr = rules.split('')
- var rulesArrLnArr = []
- var pwd = ''
- var numbers = '0123456789'
- var lower = 'abcdefghijklmnopqrstuvwxyz'
- var upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- var others = '~!@#$%^&*()_'
- function rand(min, max) {
- return Math.random() * (max - min) + min
- }
- var pwdLen = parseInt(rand(minNum, maxNum)) // 随机生成密码长度10-12位。
- // alert("密码长度:" + pwdLen);
- var item = new Array(pwdLen) // 存放密码的类型 数字(1),小写(2),大写(3),特殊字符(4)
- for (let i = 0; i < rulesArr.length; i++) {
- if (rulesArr[i] === '1') {
- rulesArrLnArr.push(i + 1 + '')
- }
- }
- for (let i = 0; i < rulesArrLnArr.length; i++) {
- item[rulesArrLnArr[i]] = rulesArrLnArr[i] + ''
- // console.log(`rulesArrLnArr[i]`, rulesArrLnArr[i])
- }
- for (let i = 0; i < pwdLen; i++) {
- if (rulesArrLnArr.indexOf(i + '') === -1) {
- item[i] = ''
- item[i] += rulesArrLnArr[parseInt(Math.random() * (rulesArrLnArr.length - 0) + 0)] // 其余部分随机生成1-4的数字。
- }
- }
- // 打乱次序,重新排序,否则密码第四位永远是特殊符号。
- item.sort(function () {
- return 0.5 - Math.random()
- }) // 返回0 两个数位置不变;小于0 就交换位置;大于0就不交换位置
- for (let j = 0; j < pwdLen; j++) {
- if (item[j] === '4') {
- pwd += numbers.charAt(Math.random() * (numbers.length - 1) + 1)
- } else if (item[j] === '1') {
- pwd += lower.charAt(Math.random() * (lower.length - 1) + 1)
- } else if (item[j] === '2') {
- pwd += upper.charAt(Math.random() * (upper.length - 1) + 1)
- } else if (item[j] === '3') {
- pwd += others.charAt(Math.random() * (others.length - 1) + 1)
- }
- }
- return pwd
- }
|