您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

173 行
6.2KB

  1. from pydantic import BaseModel, ValidationError
  2. from dataclasses import dataclass, asdict
  3. from typing import List
  4. from enum import Enum, unique
  5. from fastapi import HTTPException
  6. from functools import wraps
  7. from fastapi import Request
  8. import time
  9. @dataclass
  10. class AgentConfig(BaseModel):
  11. chatroomIdWhiteList: List[str] = []
  12. agentTokenId: str
  13. agentEnabled: bool
  14. addContactsFromChatroomIdWhiteList: List[str] = []
  15. chatWaitingMsgEnabled: bool
  16. privateGroupChatEnabled: bool=True
  17. @dataclass
  18. class AddGroupContactsHistory(BaseModel):
  19. chatroomId:str
  20. wxid:str
  21. contactWixd:str
  22. addTime:int
  23. @unique
  24. class OperationType(Enum):
  25. ADD_FRIEND = 2
  26. ACCEPT_FRIEND = 3
  27. REJECT_FRIEND = 4
  28. def validate_wxid(func):
  29. @wraps(func)
  30. async def wrapper(request: Request, *args, **kwargs):
  31. # 从 kwargs 中获取 wxid,如果不存在,则从请求体中获取
  32. wxid = kwargs.get("wxid")
  33. if wxid is None:
  34. # 异步获取请求体
  35. body = await request.json()
  36. wxid = body.get("wxid")
  37. # 如果 wxid 仍然为空,返回错误
  38. if not wxid:
  39. return {"code": 400, "message": "wxid 不能为空"}
  40. # 验证 wxid 是否存在
  41. k, loginfo = await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  42. if not k:
  43. return {"code": 404, "message": f"{wxid} 没有对应的登录信息"}
  44. login_status=loginfo.get('status','0')
  45. if login_status != '1':
  46. return {"code": 401, "message": f"{wxid} 已经离线"}
  47. # 将 k 和 loginfo 注入到路由函数的参数中
  48. # kwargs["loginfo_key"] = k
  49. # kwargs["loginfo"] = loginfo
  50. # 如果验证通过,继续执行原始函数
  51. return await func(request, *args, **kwargs)
  52. return wrapper
  53. def auth_required_time(f):
  54. @wraps(f)
  55. async def decorated_function(request: Request, *args, **kwargs):
  56. try:
  57. body = await request.json()
  58. wxid = body.get("wxid")
  59. if not wxid:
  60. return {"code": 400, "message": "wxid 不能为空"}
  61. # 模拟获取登录信息
  62. loginfo = {"status": "1", "create_at": time.time() - 2 * 24 * 60 * 60, "tokenId": "token123", "appId": "app123"}
  63. login_status = loginfo.get('status', '0')
  64. if login_status != '1':
  65. return {"code": 401, "message": f"{wxid} 已经离线"}
  66. creation_timestamp = int(loginfo.get('create_at', time.time()))
  67. current_timestamp = time.time()
  68. three_days_seconds = 3 * 24 * 60 * 60 # 三天的秒数
  69. diff_flag = (current_timestamp - creation_timestamp) >= three_days_seconds
  70. if not diff_flag:
  71. return {'code': 401, 'message': '用户创建不够三天,不能使用该功能'}
  72. kwargs['token_id'] = loginfo.get('tokenId')
  73. kwargs['app_id'] = loginfo.get('appId')
  74. return await f(*args, **kwargs)
  75. except ValidationError as e:
  76. raise HTTPException(status_code=422, detail=str(e))
  77. return decorated_function
  78. # def auth_required_time(f):
  79. # @wraps(f)
  80. # async def decorated_function(request: Request, *args, **kwargs):
  81. # try:
  82. # body = await request.json()
  83. # print("Received body:", body) # 打印请求体
  84. # wxid = body.get("wxid")
  85. # if not wxid:
  86. # return {"code": 400, "message": "wxid 不能为空"}
  87. # # 模拟获取登录信息
  88. # loginfo = {"status": "1", "create_at": time.time() - 2 * 24 * 60 * 60, "tokenId": "token123", "appId": "app123"}
  89. # login_status = loginfo.get('status', '0')
  90. # if login_status != '1':
  91. # return {"code": 401, "message": f"{wxid} 已经离线"}
  92. # creation_timestamp = int(loginfo.get('create_at', time.time()))
  93. # current_timestamp = time.time()
  94. # three_days_seconds = 3 * 24 * 60 * 60 # 三天的秒数
  95. # diff_flag = (current_timestamp - creation_timestamp) >= three_days_seconds
  96. # if not diff_flag:
  97. # return {'code': 401, 'message': '用户创建不够三天,不能使用该功能'}
  98. # kwargs['token_id'] = loginfo.get('tokenId')
  99. # kwargs['app_id'] = loginfo.get('appId')
  100. # return await f(*args, **kwargs)
  101. # except Exception as e:
  102. # raise HTTPException(status_code=422, detail=f"请求体解析失败: {str(e)}")
  103. # return decorated_function
  104. from functools import wraps
  105. import time
  106. from fastapi import Request, HTTPException
  107. def auth_required_time(f):
  108. @wraps(f)
  109. async def decorated_function(request: Request, *args, **kwargs):
  110. try:
  111. # 解析 JSON 只调用一次
  112. body = await request.json()
  113. wxid = body.get("wxid")
  114. if not wxid:
  115. raise HTTPException(status_code=400, detail="wxid 不能为空")
  116. # 调用异步方法获取登录信息
  117. k, loginfo = await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  118. if not k:
  119. raise HTTPException(status_code=404, detail=f"{wxid} 没有对应的登录信息")
  120. login_status = loginfo.get('status', '0')
  121. if login_status != '1':
  122. raise HTTPException(status_code=401, detail=f"{wxid} 已经离线")
  123. # 计算创建时间差
  124. creation_timestamp = int(loginfo.get('create_at', time.time()))
  125. current_timestamp = time.time()
  126. three_days_seconds = 3 * 24 * 60 * 60 # 三天的秒数
  127. if (current_timestamp - creation_timestamp) < three_days_seconds:
  128. raise HTTPException(status_code=401, detail="用户创建不够三天,不能使用该功能")
  129. # 注入 token_id 和 app_id
  130. kwargs['token_id'] = loginfo.get('tokenId')
  131. kwargs['app_id'] = loginfo.get('appId')
  132. # 需要 `await` 调用被装饰的异步函数
  133. return await f(request, *args, **kwargs)
  134. except HTTPException as e:
  135. return {"code": e.status_code, "message": e.detail}
  136. return decorated_function