pzx-web-api/app/util/common.py

40 lines
971 B
Python

import os
from itertools import groupby
from operator import itemgetter
from functools import wraps
from flask import request, session
def split_group(dict_list, key):
dict_list.sort(key=itemgetter(key))
tmps = groupby(dict_list, itemgetter(key))
result = []
for key, group in tmps:
result.append({key: list(group)})
return result
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:
# TODO raise apiflask 的 json 格式的 error
return '请登陆'
user_id = rpc.admin.identify(token)
if not user_id:
# TODO raise apiflask 的 json 格式的 error
return '请登陆'
session['user_id'] = user_id
return f(*args, **kwargs)
return wrapper