You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB

import hashlib
from collections import namedtuple
from datetime import datetime
from urllib import parse
from fastapi import Request, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pytz import timezone
from conf import setting
async def login_required(request: Request):
from models import User
users = request.headers.get('X-TOKEN', '').split(',')
# 用户所选柜体
client_id = request.headers.get("CLIENT-ID", '')
# 大类id
archive_id = request.headers.get("ARCHIVE-ID")
if len(users) == 0:
raise HTTPException(status_code=401)
current_user_id = users[0]
current_user = await User.get_or_none(id=current_user_id)
if not current_user:
raise HTTPException(status_code=401)
request.state.current_user = current_user
request.state.users = users
request.state.members = parse.unquote(request.headers.get('X-TOKEN-NAME', ''))
request.state.archive_id = archive_id
request.state.client_id = client_id
def respond_to(code: int = 200, desc: str = '成功', data=None):
if data is None:
return JSONResponse(content={"code": code, "desc": desc})
else:
return JSONResponse(content={"code": code, "desc": desc, "data": jsonable_encoder(data)})