| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- function randomWord(randomFlag: boolean, min: number, max: number = 15) {
- let str = ''
- let range = min
- const arr = [
- '0',
- '1',
- '2',
- '3',
- '4',
- '5',
- '6',
- '7',
- '8',
- '9',
- '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',
- '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'
- ]
- // 随机产生
- if (randomFlag) {
- range = Math.round(Math.random() * (max - min)) + min
- }
- for (var i = 0; i < range; i++) {
- const pos = Math.round(Math.random() * (arr.length - 1))
- str += arr[pos]
- }
- return str
- }
- const encodeStr = (str: string, strv = '') => {
- const NUM = 2
- const front = randomWord(false, 8)
- const middle = randomWord(false, 8)
- const end = randomWord(false, 8)
- const str1 = str.substring(0, NUM)
- const str2 = str.substring(NUM)
- if (strv) {
- const strv1 = strv.substring(0, NUM)
- const strv2 = strv.substring(NUM)
- return [front + str2 + middle + str1 + end, front + strv2 + middle + strv1 + end]
- }
- return front + str2 + middle + str1 + end
- }
- export const decodeStr = (encodedStr: string, NUM: number = 2) => {
- // 移除前后各8字符的前后缀
- const content = encodedStr.substring(8, encodedStr.length - 8)
- // 中间部分由 str2 + middle + str1 组成
- // middle也是固定8字符
- const str2 = content.substring(0, content.length - 8 - NUM)
- const str1 = content.substring(content.length - NUM)
- return str1 + str2
- }
- export default encodeStr
|