8000 feature 密码组件新增自动生成随机密码 · laraveladmin-cn/laraveladmin@18b2e9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 18b2e9f

Browse files
author
张世平
committed
feature 密码组件新增自动生成随机密码
1 parent b535619 commit 18b2e9f

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

resources/js/components/passwordEdit.vue

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
22
<div class="input-group">
3+
<div class="input-group-addon" v-if="random">
4+
<i class="fa fa-random" @click="randomVal"></i>
5+
</div>
36
<input :type="type"
47
v-model="password"
58
:disabled="disabled"
@@ -34,6 +37,12 @@
3437
default: function () {
3538
return false;
3639
}
40+
},
41+
//随机生成密码
42+
random:{
43+
default: function () {
44+
return false;
45+
}
3746
}
3847
},
3948
data(){
@@ -56,6 +65,56 @@
5665
methods:{
5766
toggle(){
5867
this.type = this.type=='password'?'text':'password';
68+
},
69+
randomPass(len = 16, mode = "high"){
70+
const lowerCaseArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',];
71+
const blockLetterArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
72+
const numberArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
73+
const specialArr = ['!', '@', '-', '_', '=', '<', '>', '#', '*', '%', '+', '&', '^', '$'];
74+
const passArr = [];
75+
let password = '';
76+
77+
//指定参数随机获取一个字符
78+
const specifyRandom = function (...arr) {
79+
let str = "";
80+
arr.forEach(item => {
81+
str += item[Math.floor(Math.random() * item.length)]
82+
});
83+
return str;
84+
}
85+
86+
switch (mode) {
87+
case "high":
88+
//安全最高的
89+
password += specifyRandom(lowerCaseArr, blockLetterArr, numberArr, specialArr);
90+
passArr.push(...lowerCaseArr, ...blockLetterArr, ...numberArr, ...specialArr);
91+
break;
92+
case "medium":
93+
//中等的
94+
password += specifyRandom(lowerCaseArr, blockLetterArr, numberArr);
95+
passArr.push(...lowerCaseArr, ...blockLetterArr, ...numberArr);
96+
break;
97+
//低等的
98+
case "low":
99+
password += specifyRandom(lowerCaseArr, numberArr);
100+
passArr.push(...lowerCaseArr, ...numberArr);
101+
break;
102+
default:
103+
password += specifyRandom(lowerCaseArr, numberArr);
104+
passArr.push(...lowerCaseArr, ...numberArr);
105+
}
106+
107+
const forLen = len - password.length;
108+
for (let i = 0; i < forLen; i++) {
109+
password += specifyRandom(passArr);
110+
}
111+
112+
return password;
113+
},
114+
randomVal(){
115+
this.password = this.randomPass(6,'low');
116+
this.type = 'text';
117+
59118
}
60119
},
61120
computed:{

0 commit comments

Comments
 (0)
0