""" 阿龙电竞陪玩平台 - 邀请码生成工具 生成规则:毫秒时间戳 + 用户ID + 随机数 -> Base62编码 -> 固定20位邀请码 特点:唯一性高、生成快、无序不可读 """ import time import random # Base62 编码字符集 (数字+小写字母+大写字母) _BASE62_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" _BASE62_LENGTH = 62 def base62_encode(num): """将数字转换为Base62字符串""" if num == 0: return _BASE62_CHARS[0] result = [] while num > 0: num, remainder = divmod(num, _BASE62_LENGTH) result.append(_BASE62_CHARS[remainder]) # 反转结果字符串 return ''.join(reversed(result)) def chuangjianYaoqingma(yonghuid_str): """ 创建唯一邀请码 (20位固定长度) 参数: yonghuid_str: 用户ID字符串,如 "123456" 返回: 20位的Base62编码邀请码字符串 """ # 1. 获取当前毫秒时间戳 (13位) timestamp_ms = int(time.time() * 1000) # 2. 将用户ID字符串转换为数字 (确保处理字符串) try: # 先尝试直接转换整数 yonghuid_num = int(yonghuid_str) except ValueError: # 如果用户ID不是纯数字,使用哈希值 yonghuid_num = abs(hash(yonghuid_str)) % (10 ** 8) # 取8位数字 # 3. 生成一个随机数 (0-9999) random_salt = random.randint(0, 9999) # 4. 组合数字: 时间戳(13位) + 用户ID数字(最多8位) + 随机数(4位) # 示例: 1647850123456 + 123456 + 7890 = 16478501234561234567890 combined_num = int(f"{timestamp_ms}{yonghuid_num:08d}{random_salt:04d}") # 5. Base62编码 encoded_str = base62_encode(combined_num) # 6. 确保固定20位长度: 不足补0,超过取后20位 # 理论上组合数字非常大,编码后通常会超过20位 if len(encoded_str) < 20: # 左补0 (理论上不会发生,但保持安全) encoded_str = encoded_str.rjust(20, _BASE62_CHARS[0]) else: # 取后20位 (更随机) encoded_str = encoded_str[-20:] return encoded_str def yanzhengYaoqingmaWeiyixing(yaoqingma): """ 验证邀请码是否有效 (简单的格式验证) 参数: yaoqingma: 待验证的邀请码 返回: bool: 邀请码格式是否有效 """ if not yaoqingma or len(yaoqingma) != 20: return False # 检查是否只包含Base62字符 for char in yaoqingma: if char not in _BASE62_CHARS: return False return True