<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button class="btn getRandomSymbol2" id="getRandomSymbol2" onclick="generatePasswordTest()">生成密码</button>
<input id="password" />
</body>
<script src="https://npm.elemecdn.com/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://npm.elemecdn.com/amazeui@2.7.2/dist/js/amazeui.min.js"></script>
<script type="application/javascript">
/**
* 测试API
* @returns {*}
*/
function generatePasswordTest() {
var a = + 4
, b = true
, c = true
, d = true
, e = false;
// 長度 小寫 大寫 數字 字符
var passwordText = generatePassword(a, b, c, d, e);
console.log("长度 小写 大写 数字 字符: ", passwordText)
$("#password").val(passwordText);
return a[Math.floor(Math.random() * a.length)]
}
var randomFunc = {
lower: getRandomLower,
upper: getRandomUpper,
number: getRandomNumber,
symbol: getRandomSymbol
};
/**
*
* @param a 长度
* @param b 小写
* @param c 大写
* @param d 数字
* @param e 字符
* @returns {string}
*/
function generatePassword(a, b, c, d, e) {
var i, f = "", g = b + c + d + e, h = [{
lower: b
}, {
upper: c
}, {
number: d
}, {
symbol: e
}].filter(function (a) {
return Object.values(a)[0]
});
if (0 === g)
return "";
for (i = 0; a > i; i++)
h.forEach(function (a) {
var b = Object.keys(a)[0];
f += randomFunc[b]()
});
return f.slice(0, a).split("").sort(function () {
return Math.random() - .5
}).join("")
}
function secureMathRandom() {
return window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1)
}
function getRandomLower() {
return String.fromCharCode(Math.floor(26 * Math.random()) + 97)
}
function getRandomUpper() {
return String.fromCharCode(Math.floor(26 * Math.random()) + 65)
}
function getRandomNumber() {
return String.fromCharCode(Math.floor(10 * secureMathRandom()) + 48)
}
function getRandomSymbol() {
var a = '~!@#$%^&*()_+{}":?><;.,';
return a[Math.floor(Math.random() * a.length)]
}
</script>
</html>