Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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