def change_token(uid): ''' 修改token ''' tokens = yield from Token.findAll('uid=?', [uid]) shal = '%s=%s=%s' % ('Token', uid, time.time()) shal_uid = hashlib.sha1(shal.encode('utf-8')).hexdigest() token = Token(id=tokens[0].id, uid=uid, token_key=shal_uid, last_time=time.time()) yield from token.update() return token.token_key
def wrapper(*args, **kw): err = False #获取request里面的参数 if kw['request'].method == 'POST': params = yield from kw['request'].json() else: qs = kw['request'].query_string if qs: q = dict() for k, v in parse.parse_qs(qs, True).items(): q[k] = v[0] params = q kws = dict(**params) key = kws.get('token', None) if key is None: err = True else: token = yield from Token.findAll('token_key=?', [key]) if len(token) == 0: err = True else: now = datetime.datetime.now() end = now - datetime.timedelta(hours=3) #有效期 nowNum = time.mktime(now.timetuple()) endNum = time.mktime(end.timetuple()) lastTime = token[0]['last_time'] if lastTime < endNum: #过期 err = True if err: r = web.Response() r.content_type = 'application/json' Dict = dict(code=-1, msg='token失效,请重新登录') logging.info(Dict) r.body = json.dumps(Dict, ensure_ascii=False).encode('utf-8') return r res = yield from func(*args, **kw) return res