|
1 | 1 | <template>
|
2 | 2 | <div class="input-group">
|
| 3 | + <div class="input-group-addon" v-if="random"> |
| 4 | + <i class="fa fa-random" @click="randomVal"></i> |
| 5 | + </div> |
3 | 6 | <input :type="type"
|
4 | 7 | v-model="password"
|
5 | 8 | :disabled="disabled"
|
|
34 | 37 | default: function () {
|
35 | 38 | return false;
|
36 | 39 | }
|
| 40 | + }, |
| 41 | + //随机生成密码 |
| 42 | + random:{ |
| 43 | + default: function () { |
| 44 | + return false; |
| 45 | + } |
37 | 46 | }
|
38 | 47 | },
|
39 | 48 | data(){
|
|
56 | 65 | methods:{
|
57 | 66 | toggle(){
|
58 | 67 | 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 | +
|
59 | 118 | }
|
60 | 119 | },
|
61 | 120 | computed:{
|
|
0 commit comments