将文件夹命名从拼音改为英文,整理了一些代码,将导入语句迁移到了头部
This commit is contained in:
@@ -3,9 +3,9 @@ import json
|
||||
import logging
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from dingdan.models import Dingdan, DingdanPingtai, DingdanShangjia
|
||||
from peizhi.models import ClubConfig
|
||||
from yonghu.models import UserMain, UserDashou, UserBoss, UserShangjia
|
||||
from orders.models import Dingdan, DingdanPingtai, DingdanShangjia
|
||||
from config.models import ClubConfig
|
||||
from users.models import UserMain, UserDashou, UserBoss, UserShangjia
|
||||
|
||||
logger = logging.getLogger('chat_utils')
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# utils/fadan_utils.py
|
||||
from dingdan.models import Fadan
|
||||
from orders.models import Fadan
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger('xiaochengxu')
|
||||
|
||||
56
utils/invitationcode_utils.py
Normal file
56
utils/invitationcode_utils.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
阿龙电竞陪玩平台 - 邀请码生成工具
|
||||
生成规则:毫秒时间戳 + 用户ID + 随机数 -> Base62编码 -> 固定20位邀请码
|
||||
特点:唯一性高、生成快、无序不可读
|
||||
"""
|
||||
import time
|
||||
import random
|
||||
|
||||
# Base62 编码字符集 (数字+小写字母+大写字母)
|
||||
_BASE62_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
_BASE62_LENGTH = len(_BASE62_CHARS)
|
||||
|
||||
|
||||
def base62_encode(num: int) -> str:
|
||||
"""
|
||||
将整数转换为 Base62 字符串,固定长度为 20 位
|
||||
"""
|
||||
if not num:
|
||||
return _BASE62_CHARS[0]
|
||||
res = []
|
||||
while num:
|
||||
num, r = divmod(num, _BASE62_LENGTH)
|
||||
res.append(_BASE62_CHARS[r])
|
||||
return ''.join(reversed(res))
|
||||
|
||||
|
||||
def CreateInvitationCode(uid_str: str) -> str:
|
||||
"""
|
||||
创建唯一邀请码 (20位固定长度)
|
||||
|
||||
参数:
|
||||
uid_str: 用户ID字符串,如 "123456"
|
||||
|
||||
返回:
|
||||
str: 生成的唯一邀请码
|
||||
"""
|
||||
ts = int(time.time()*1000)
|
||||
try:
|
||||
uid = int(uid_str)
|
||||
except ValueError:
|
||||
uid = abs(hash(uid_str)) % 10**8
|
||||
num = int(f"{ts}{uid:08d}{random.randint(0,9999):04d}")
|
||||
res = base62_encode(num)
|
||||
return res.rjust(20, _BASE62_CHARS[0]) if len(res)<20 else res[-20:]
|
||||
|
||||
|
||||
def VerifyInvitationCode(yaoqingma: str) -> bool:
|
||||
"""
|
||||
验证邀请码是否有效 (简单的格式验证)
|
||||
参数:
|
||||
yaoqingma: 待验证的邀请码
|
||||
返回:
|
||||
bool: 邀请码格式是否有效
|
||||
"""
|
||||
|
||||
return bool(yaoqingma and len(yaoqingma)==20 and all(c in _BASE62_CHARS for c in yaoqingma))
|
||||
@@ -5,7 +5,7 @@ import json # 新增,用于格式化打印
|
||||
import requests
|
||||
from django.core.cache import cache
|
||||
from django.conf import settings
|
||||
from yonghu.models import OfficialAccountUser
|
||||
from users.models import OfficialAccountUser
|
||||
|
||||
logger = logging.getLogger('weixin_broadcast')
|
||||
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
"""
|
||||
阿龙电竞陪玩平台 - 邀请码生成工具
|
||||
生成规则:毫秒时间戳 + 用户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
|
||||
Reference in New Issue
Block a user