# -*- coding: utf-8 -*-
from application import app
from flask import request,g,jsonify
from common.models.member.Member import Member
from common.libs.member.MemberService import MemberService
import re
'''
api认证
'''
@app.before_request
def before_request_api():
api_ignore_urls = app.config['API_IGNORE_URLS']
path = request.path
if '/api' not in path:
return
member_info = check_member_login()
g.member_info = None
if member_info:
g.member_info = member_info
pattern = re.compile('%s' % "|".join( api_ignore_urls ))
if pattern.match(path):
return
if not member_info :
resp = {'code': -1, 'msg': '未登录~', 'data': {}}
return jsonify(resp)
return
'''
判断用户是否已经登录
'''
def check_member_login():
auth_cookie = request.headers.get("Authorization")
if auth_cookie is None:
return False
auth_info = auth_cookie.split("#")
if len(auth_info) != 2:
return False
try:
member_info = Member.query.filter_by(id=auth_info[1]).first()
except Exception:
return False
if member_info is None:
return False
if auth_info[0] != MemberService.geneAuthCode( member_info ):
return False
if member_info.status != 1:
return False
return member_info
flask网页制作拦截器的时候通过对网页的cookie进行获取和判断,例如 cookie值是不是合法的、是否正确符合格式等,小程序里不能做cookie,这该怎么办呢?
在每次请求的时候都会wx.request请求加载里面的header头部,可以在header头部里面加入一个信息,web取到就可以了。
中定义一个Authrization
getRequestHeader:function(){
return {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': this.getCache("token")
}
},
登录时有个cache,存储里有个token,
每次登录的时候都会cache一个信息,取出Authorization值就可以了。
API_IGNORE_URLS = [
"^/api"
]