|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- from pydantic import BaseModel, ValidationError
- from dataclasses import dataclass, asdict
- from typing import List
- from enum import Enum, unique
- from fastapi import HTTPException
-
- from functools import wraps
- from fastapi import Request
- import time
-
- @dataclass
- class AgentConfig(BaseModel):
- chatroomIdWhiteList: List[str] = []
- agentTokenId: str
- agentEnabled: bool
- addContactsFromChatroomIdWhiteList: List[str] = []
- chatWaitingMsgEnabled: bool
- privateGroupChatEnabled: bool=True
-
-
- @dataclass
- class AddGroupContactsHistory(BaseModel):
- chatroomId:str
- wxid:str
- contactWixd:str
- addTime:int
-
-
-
- @unique
- class OperationType(Enum):
- ADD_FRIEND = 2
- ACCEPT_FRIEND = 3
- REJECT_FRIEND = 4
-
-
- def validate_wxid(func):
- @wraps(func)
- async def wrapper(request: Request, *args, **kwargs):
-
- wxid = kwargs.get("wxid")
- if wxid is None:
-
- body = await request.json()
- wxid = body.get("wxid")
-
-
- if not wxid:
- return {"code": 400, "message": "wxid 不能为空"}
-
-
- k, loginfo = await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
- if not k:
- return {"code": 404, "message": f"{wxid} 没有对应的登录信息"}
-
- login_status=loginfo.get('status','0')
-
- if login_status != '1':
- return {"code": 401, "message": f"{wxid} 已经离线"}
-
-
-
-
-
- return await func(request, *args, **kwargs)
- return wrapper
-
-
- def auth_required_time(f):
- @wraps(f)
- async def decorated_function(request: Request, *args, **kwargs):
- try:
- body = await request.json()
- wxid = body.get("wxid")
- if not wxid:
- return {"code": 400, "message": "wxid 不能为空"}
-
-
- loginfo = {"status": "1", "create_at": time.time() - 2 * 24 * 60 * 60, "tokenId": "token123", "appId": "app123"}
- login_status = loginfo.get('status', '0')
- if login_status != '1':
- return {"code": 401, "message": f"{wxid} 已经离线"}
-
- creation_timestamp = int(loginfo.get('create_at', time.time()))
- current_timestamp = time.time()
- three_days_seconds = 3 * 24 * 60 * 60
- diff_flag = (current_timestamp - creation_timestamp) >= three_days_seconds
- if not diff_flag:
- return {'code': 401, 'message': '用户创建不够三天,不能使用该功能'}
-
- kwargs['token_id'] = loginfo.get('tokenId')
- kwargs['app_id'] = loginfo.get('appId')
- return await f(*args, **kwargs)
- except ValidationError as e:
- raise HTTPException(status_code=422, detail=str(e))
- return decorated_function
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- from functools import wraps
- import time
- from fastapi import Request, HTTPException
-
- def auth_required_time(f):
- @wraps(f)
- async def decorated_function(request: Request, *args, **kwargs):
- try:
-
- body = await request.json()
- wxid = body.get("wxid")
-
- if not wxid:
- raise HTTPException(status_code=400, detail="wxid 不能为空")
-
-
- k, loginfo = await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
- if not k:
- raise HTTPException(status_code=404, detail=f"{wxid} 没有对应的登录信息")
-
- login_status = loginfo.get('status', '0')
- if login_status != '1':
- raise HTTPException(status_code=401, detail=f"{wxid} 已经离线")
-
-
- creation_timestamp = int(loginfo.get('create_at', time.time()))
- current_timestamp = time.time()
- three_days_seconds = 3 * 24 * 60 * 60
-
- if (current_timestamp - creation_timestamp) < three_days_seconds:
- raise HTTPException(status_code=401, detail="用户创建不够三天,不能使用该功能")
-
-
- kwargs['token_id'] = loginfo.get('tokenId')
- kwargs['app_id'] = loginfo.get('appId')
-
-
- return await f(request, *args, **kwargs)
-
- except HTTPException as e:
- return {"code": e.status_code, "message": e.detail}
-
- return decorated_function
|