From 67b38b6e7b7a16d6995d1fbcef936d77002c701d Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Fri, 24 Sep 2021 16:43:05 +0800 Subject: [PATCH] move aiid2regcode/regcode2aiid to core.tool --- index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ecf4d24..4bbf9aa 100644 --- a/index.js +++ b/index.js @@ -36,5 +36,55 @@ module.exports = { .replace(/[tuv]/g, 8) .replace(/[wxyz]/g, 9) return parseInt(port) - } + }, + + /** + * 用户编号转邀请码 + * + * @static + * @param {*} aiid + * @return {*} + * @memberof TICrypto + */ + aiid2regcode(aiid) { + const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz' + const base = 16367 + let num = (aiid + base) * (base - alphabet.length) + let code = '' + let mod + while (num > 0) { + mod = num % alphabet.length + num = (num - mod) / alphabet.length + code = code + alphabet[mod] // 倒序存放 + } + return code + }, + + /** + * 邀请码转用户编号 + * + * @static + * @param {*} code + * @return {*} + * @memberof TICrypto + */ + regcode2aiid(code) { + if (typeof code === 'string' && /^[a-zA-Z0-9]+$/.test(code)) { + const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz' + const base = 16367 + code = code.toLowerCase() + let len = code.length + let num = 0 + for (let i = 0; i < len; i++) { + num += alphabet.indexOf(code[i]) * Math.pow(alphabet.length, i) + } + let aiid = num / (base - alphabet.length) - base + if (aiid >= 0 && Number.isInteger(aiid)) { + // 允许 aiid===0:当第一个用户(aiid==1)登录时,需要一个系统默认的邀请码。 + return aiid + } + } + return null // null 代表一切非法的regcode + }, + } \ No newline at end of file