模型定义ok
This commit is contained in:
parent
5b46db540a
commit
029b88c5cd
@ -0,0 +1 @@
|
||||
from .user import *
|
49
models/base.py
Normal file
49
models/base.py
Normal file
@ -0,0 +1,49 @@
|
||||
import hashlib
|
||||
from pydantic import Field
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
from settings import settings
|
||||
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
FIELD_MAP = {
|
||||
'ip': 'a01',
|
||||
'os': 'a08',
|
||||
}
|
||||
|
||||
|
||||
def to_alias(k: str) -> str:
|
||||
return FIELD_MAP.get(k) or k
|
||||
|
||||
|
||||
class IP4(str):
|
||||
|
||||
@classmethod
|
||||
def __get_validators__(cls):
|
||||
yield cls.validate
|
||||
|
||||
@classmethod
|
||||
def validate(cls, v):
|
||||
IPv4Address(v)
|
||||
return str(v)
|
||||
|
||||
|
||||
class Base(BaseModel):
|
||||
# sign = md5(distinct_id+account_id+act+ts+salt)
|
||||
distinct_id: str = Field(..., title='访客 ID')
|
||||
game: str
|
||||
account_id: str
|
||||
act: str
|
||||
event_name: str = None
|
||||
# preset: dict
|
||||
properties: dict
|
||||
ts: int
|
||||
sign: str
|
||||
|
||||
@validator('sign')
|
||||
def sign_validator(cls, v: str, values: dict):
|
||||
s = f'{values.get("distinct_id")}{values.get("account_id", "")}{values.get("act", "")}{values.get("ts", "")}{settings.SALT}'
|
||||
if hashlib.md5(s.encode()).hexdigest() == v:
|
||||
return v
|
||||
raise ValueError(f'打击违法犯罪行为{hashlib.md5(s.encode()).hexdigest()}')
|
0
models/event.py
Normal file
0
models/event.py
Normal file
28
models/user.py
Normal file
28
models/user.py
Normal file
@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
from .base import Base, IP4, to_alias
|
||||
|
||||
__all__ = ('UserModel',)
|
||||
|
||||
|
||||
class Preset(BaseModel):
|
||||
ip: IP4
|
||||
os: str
|
||||
ttt: str = None
|
||||
|
||||
def dict(self, **kwargs):
|
||||
res = super().dict(**kwargs)
|
||||
return {'#' + k: v for k, v in res.items() if v is not None}
|
||||
|
||||
class Config:
|
||||
alias_generator = to_alias
|
||||
|
||||
|
||||
class UserModel(Base):
|
||||
preset: Preset
|
||||
|
||||
def dict(self, **kwargs):
|
||||
kwargs.setdefault('exclude', {'preset'})
|
||||
self.properties.update(self.preset.dict(**kwargs))
|
||||
return super().dict(**kwargs)
|
0
routers/event.py
Normal file
0
routers/event.py
Normal file
29
routers/user.py
Normal file
29
routers/user.py
Normal file
@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
from handler_data import HandlerUser, HandlerEvent
|
||||
from settings import settings
|
||||
|
||||
router = APIRouter()
|
||||
from models import UserModel
|
||||
|
||||
|
||||
@router.post("/user/")
|
||||
async def user(request: Request, item: UserModel):
|
||||
ta = getattr(request.app.state.ta, item.act)
|
||||
# 将不同游戏发送到不同 topic_name
|
||||
request.app.state.ta.consumer.topic_name = item.game
|
||||
rdb = request.app.state.redis
|
||||
await asyncio.gather(*map(lambda o: asyncio.create_task(o(rdb, item)), HandlerUser.handler_link))
|
||||
|
||||
ta(item.distinct_id, item.account_id, item.properties)
|
||||
results = {"code": 0, 'msg': 'ok'}
|
||||
return results
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user