|
- 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
-
-
- @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):
- # 从 kwargs 中获取 wxid,如果不存在,则从请求体中获取
- wxid = kwargs.get("wxid")
- if wxid is None:
- # 异步获取请求体
- body = await request.json()
- wxid = body.get("wxid")
-
- # 如果 wxid 仍然为空,返回错误
- if not wxid:
- return {"code": 400, "message": "wxid 不能为空"}
-
- # 验证 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} 已经离线"}
-
- # 将 k 和 loginfo 注入到路由函数的参数中
- # kwargs["loginfo_key"] = k
- # kwargs["loginfo"] = loginfo
- # 如果验证通过,继续执行原始函数
- 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
-
-
- # def auth_required_time(f):
- # @wraps(f)
- # async def decorated_function(request: Request, *args, **kwargs):
- # try:
- # body = await request.json()
- # print("Received body:", body) # 打印请求体
- # 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 Exception as e:
- # raise HTTPException(status_code=422, detail=f"请求体解析失败: {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:
- # 解析 JSON 只调用一次
- 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="用户创建不够三天,不能使用该功能")
-
- # 注入 token_id 和 app_id
- kwargs['token_id'] = loginfo.get('tokenId')
- kwargs['app_id'] = loginfo.get('appId')
-
- # 需要 `await` 调用被装饰的异步函数
- return await f(request, *args, **kwargs)
-
- except HTTPException as e:
- return {"code": e.status_code, "message": e.detail}
-
- return decorated_function
|