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

38 lines
853 B
Python
Raw Normal View History

2023-08-10 12:27:18 +08:00
import os
from itertools import groupby
from operator import itemgetter
2023-08-11 16:36:56 +08:00
from functools import wraps
from flask import request, session
2023-08-10 12:27:18 +08:00
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()
2023-08-11 16:36:56 +08:00
def login_required(f):
"""
登陆保护验证用户是否登陆
"""
2023-08-11 16:45:23 +08:00
from app import rpc
2023-08-11 16:36:56 +08:00
@wraps(f)
def wrapper(*args, **kwargs):
token = request.headers.get("Authorization", default=None)
if not token:
return '请登陆'
user_id = rpc.admin.identify(token)
if not user_id:
return '请登陆'
session['user_id'] = user_id
return f(*args, **kwargs)
return wrapper