js处理卡密 – 通用方法

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
//去除重复卡密 传入卡密数组 function removeRepetitionKey(arr) { return Array.from(new Set(arr)) } //随机生成卡密 len:卡密长度 str:开头 type:1大写字母 2小写字母 3大小写混合 4数字 5数字大小写字母混合 默认数字大小写混合 function randomKey(len, str, nub, type) { len = len || 32; var arr = []; switch (type) { case 1: var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 2: var $chars = 'abcdefghijklmnopqrstuvwxyz'; break; case 3: var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; case 4: var $chars = '1234567890'; break; case 5: var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; break; default: var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; break; } var maxPos = $chars.length; for (j = 0; j < nub; j++) { var pwd = ''; for (i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } arr.push(str + pwd) } return arr } //替换卡密内容 传入卡密数组,将每条卡密的str1替换成str2 这个会替换原数组,不影响 function replaceKey(arr, str1, str2) { arr.forEach((v, i) => { arr[i] = arr[i].replace(str1, str2) }) return arr } //头部或尾部增加字符串 function addKey(arr, before, after) { arr.forEach((v, i) => { arr[i] = before + arr[i] + after }) return arr } //复制卡密 nub:复制几次 disorganize:是否打乱顺序 function copyKey(arr, nub, disorganize) { var arr_new = [] arr.forEach((v, i) => { for (let i = 0; i < nub; i++) { arr_new.push(v) } }) if (disorganize) { arr_new.sort(function() { return .5 - Math.random(); }); } return arr_new } //过滤卡密 len 长度 isCut(true是去除卡密 false是保留卡密)type=1 大于 type=2 小于 type=3等于 function removeLenKey(arr, len, isCut, type) { var arr_new = [] if (isCut) { for (let i = 0; i < arr.length; i++) { if (type == 1) { if (arr[i].length > len) { arr.splice(i, 1) i--; } } else if (type == 2) { if (arr[i].length < len) { arr.splice(i, 1) i--; } } else { if (arr[i].length == len) { arr.splice(i, 1) i--; } } } arr_new = arr } else { // arr.forEach((v, i) => { if (type == 1) { if (v.length > len) { arr_new.push(v) } } else if (type == 2) { if (v.length < len) { arr_new.push(v) } } else { if (v.length == len) { arr_new.push(v) } } }) } return arr_new } //截取卡密 len 截取长度 isLeft(true是截取前面(左侧) false是截取后面(右侧)) function cutOutKey(arr, len, isLeft) { arr.forEach((v, i) => { if (isLeft) { arr[i] = arr[i].substring(0, len); } else { arr[i] = arr[i].substring(arr[i].length - len, arr[i].length); } }) return arr; } //截取卡密 str 截取字符串之前或之后 isLeft(true是截取前面(左侧) false是截取后面(右侧)) function cutOutKey1(arr, str, isLeft) { arr.forEach((v, i) => { arr[i] = getCaption(arr[i], str, isLeft); }) return arr; } //这个函数是给其他调用的 function getCaption(obj, str, state) { if (state) { var index = obj.indexOf(str); } else { var index = obj.lastIndexOf(str); } if (state) { obj = obj.substring(0, index) == '' ? obj : obj.substring(0, index); } else { obj = obj.substring(index + 1, obj.length) == '' ? obj : obj.substring(index + 1, obj.length); } return obj; } //生成序列 function listKey(min, max) { var arr = [] var len = max - min + 1 for (var i = 0; i < len; i++) { arr.push(Number(min) + Number(i)) } return arr } |