feat: add api of body record 2
continuous-integration/drone/push Build is passing Details

This commit is contained in:
BryantHe 2023-08-12 20:15:48 +08:00
parent c30c644f7c
commit 95621cf3fe
4 changed files with 31 additions and 23 deletions

View File

@ -10,6 +10,7 @@ auth = APIBlueprint('auth', __name__)
@auth.post('/wechat_login')
@auth.input(WechatLoginIn)
@auth.output(WechatLoginOut)
@auth.doc(summary='微信登录', description='微信登录', tags=['认证'])
def wechat_login(json_data):
try:
result = rpc.admin.wechat_login(json_data['code'])
@ -21,6 +22,7 @@ def wechat_login(json_data):
@auth.get('/refresh_token')
@auth.input(RefreshTokenIn, location='query')
@auth.output(RefreshTokenOut)
@auth.doc(summary='刷新 Token', description='刷新 Token', tags=['认证'])
def refresh_token(query_data):
try:
result = rpc.admin.refresh_token(query_data['refresh_token'])

View File

@ -4,13 +4,14 @@ from flask import session
from app import rpc
from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsIn, BodyRecordsOut
from app.util.common import login_required
from app.util.auth import login_required
record = APIBlueprint('record', __name__)
@record.post('/')
@record.input(BodyRecordIn)
@record.doc(summary='添加身体记录', description='添加身体记录', tags=['身体记录'])
@login_required
def add_record(json_data):
try:
@ -25,6 +26,7 @@ def add_record(json_data):
@record.get('/<record_id>')
@record.output(BodyRecordOut)
@record.doc(summary='查询单条身体记录', description='查询单条身体记录', tags=['身体记录'])
@login_required
def get_record(record_id):
try:
@ -37,8 +39,9 @@ def get_record(record_id):
@record.get('/list')
@record.input(BodyRecordsIn, location='query')
@record.output(BodyRecordsOut)
@record.doc(summary='查询身体记录列表', description='查询身体记录列表', tags=['身体记录'])
@login_required
def get_record(query_data):
def get_records(query_data):
try:
result = rpc.body_record.get_all_by_user(query_data['user_id'])
except Exception as e:

24
app/util/auth.py Normal file
View File

@ -0,0 +1,24 @@
from functools import wraps
from flask import request, session
from app.api.v1.exception.auth import AuthError
def login_required(f):
"""
登陆保护验证用户是否登陆
"""
from app import rpc
@wraps(f)
def wrapper(*args, **kwargs):
token = request.headers.get("Authorization", default=None)
if not token:
raise AuthError()
user_id = rpc.admin.identify(token)
if not user_id:
raise AuthError()
session['user_id'] = user_id
return f(*args, **kwargs)
return wrapper

View File

@ -1,11 +1,6 @@
import os
from itertools import groupby
from operator import itemgetter
from functools import wraps
from flask import request, session
from app.api.v1.exception.auth import AuthError
def split_group(dict_list, key):
@ -20,20 +15,4 @@ def split_group(dict_list, key):
basedir = os.getcwd()
def login_required(f):
"""
登陆保护验证用户是否登陆
"""
from app import rpc
@wraps(f)
def wrapper(*args, **kwargs):
token = request.headers.get("Authorization", default=None)
if not token:
raise AuthError()
user_id = rpc.admin.identify(token)
if not user_id:
raise AuthError()
session['user_id'] = user_id
return f(*args, **kwargs)
return wrapper