/*
//验证密码强弱
var validatePass = (rule, value, callback)=>{
// let acount = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\[\]_+=-]).*[^]{8,20}$/
let acount = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\[\]{}|;:,.<>?]).{8,20}$/;
if (value === '') {
callback(new Error('请输入新密码'));
}else if (!acount.test(value)) {
callback(new Error('长度为8~20个字符,必须包含大小写字母,数字和特殊字符( !@#$%^&*()_+[]{}|;:,.<>? )'));
} else {
callback();
}
};
*/
function generateSecureTempPassword(length = 8) {
// const lowercase = "abcdefghijklmnopqrstuvwxyz";
// const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// const numbers = "0123456789";
// const specialChars = "!@#$%^&*()_+[]{}|;:,.<>?";
// const allChars = lowercase + uppercase + numbers + specialChars;
// let password = "";
// // 确保密码包含至少一个大写字母、小写字母、数字和特殊字符
// password += lowercase[Math.floor(Math.random() * lowercase.length)];
// password += uppercase[Math.floor(Math.random() * uppercase.length)];
// password += numbers[Math.floor(Math.random() * numbers.length)];
// password += specialChars[Math.floor(Math.random() * specialChars.length)];
// // 剩余的字符随机生成
// for (let i = 4; i < length; i++) {
// password += allChars[Math.floor(Math.random() * allChars.length)];
// }
// // 打乱密码顺序
// password = password.split('').sort(() => 0.5 - Math.random()).join('');
// return password;
//随机生成数必须以字母或者数字开头
const lowercase = "abcdefghijklmnopqrstuvwxyz";
const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "0123456789";
const specialChars = "!@#$%^&*()_+[]{}|;:,.<>?";
const lettersAndNumbers = lowercase + uppercase + numbers;
const allChars = lettersAndNumbers + specialChars;
let password = "";
// 确保密码以字母或数字开头
password += lettersAndNumbers[Math.floor(Math.random() * lettersAndNumbers.length)];
// 确保密码包含至少一个大写字母、小写字母、数字和特殊字符
password += lowercase[Math.floor(Math.random() * lowercase.length)];
password += uppercase[Math.floor(Math.random() * uppercase.length)];
password += numbers[Math.floor(Math.random() * numbers.length)];
password += specialChars[Math.floor(Math.random() * specialChars.length)];
// 剩余的字符随机生成
for (let i = 5; i < length; i++) { // 这里从第5个字符开始填充,前5个已定义
password += allChars[Math.floor(Math.random() * allChars.length)];
}
// 打乱除第一个字符外的顺序
const firstChar = password[0];
const remainingChars = password.slice(1).split('').sort(() => 0.5 - Math.random()).join('');
password = firstChar + remainingChars;
return password;
},
// 示例使用
const securePassword = generateSecureTempPassword();
console.log(securePassword); // 例如: "A3!dG#7QwR8"