56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from config import config
|
||
import re
|
||
import aiohttp
|
||
|
||
AUTHORIZED_USERS = {}
|
||
|
||
|
||
def normalize_phone(phone: str) -> str:
|
||
print(phone)
|
||
digits = re.sub(r"\D", "", phone)
|
||
if len(digits) > 10:
|
||
digits = digits[-10:]
|
||
print(digits)
|
||
return digits
|
||
|
||
|
||
async def authorize_user(user_id: int, phone: str) -> bool:
|
||
normalized = normalize_phone(phone)
|
||
api_url = config.get("user_api_url")
|
||
if not api_url:
|
||
print("Не указан user_api_url в конфигурации")
|
||
return False
|
||
|
||
async with aiohttp.ClientSession() as session:
|
||
try:
|
||
async with session.get(api_url, params={"tel": normalized}) as response:
|
||
print(response.status)
|
||
if response.status != 200:
|
||
print(f"Ошибка запроса к API: статус {response.status}")
|
||
return False
|
||
data = await response.json()
|
||
except Exception as e:
|
||
print(f"Исключение при запросе к API: {str(e)}")
|
||
return False
|
||
|
||
if data.get("response") != "1":
|
||
print(f"Доступ запрещен для номера: {normalized}")
|
||
return False
|
||
|
||
zone_str = data["data"].get("zone", "")
|
||
zones = [zone.strip() for zone in zone_str.split(";") if zone.strip()]
|
||
card_code = data["data"].get("card-code", "")
|
||
if not zones or not card_code:
|
||
print("Некорректный ответ API: отсутствуют зоны или код карты.")
|
||
return False
|
||
|
||
AUTHORIZED_USERS[user_id] = {"tel": normalized, "card": card_code, "zones": zones}
|
||
print(
|
||
f"{user_id} авторизован с номером: {normalized}, карта: {card_code}, зоны: {zones}"
|
||
)
|
||
return True
|
||
|
||
|
||
def is_user_auth(user_id: int) -> bool:
|
||
return user_id in AUTHORIZED_USERS
|