def verify(account, secret): user = User.query.filter_by(account=account).first() if not user: raise ApiException(msg='账号不存在') if not user.check_password(secret): raise ApiException(msg='密码错误') return {'uid': user.id}
def verify_auth_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except BadSignature: raise ApiException(msg='token无效') except SignatureExpired: raise ApiException(msg='token超时', error_code=402) uid = data['uid'] return {'uid': uid}
def reset_password(uid, new_password): user = User.query.filter_by(id=uid).first() if user is None: raise ApiException(msg='用户不存在') else: user.password = new_password db.session.commit()
def decorator(*args, **kwargs): token = request.headers.get('token') data = verify_auth_token(token) if data: g.user = data else: raise ApiException(msg='token无效') return f(*args, **kwargs)
def add_collection(): uid = g.user['uid'] c_id = request.form['c_id'] collection = Collection.query.filter_by(id=c_id, user_id=uid).first() if collection: raise ApiException(code='001', msg='已添加收藏') else: collection = Collection.query.filter_by(id=c_id).first() collection.user_id = uid db.session.add(collection) db.session.commit() return jsonify({ 'code': '000', 'msg': '添加成功', "success": True })
def upload_file(): uploaded_file = request.files['file'] if uploaded_file: paste_file = PasteFile.create_by_upload_file(uploaded_file) db.session.add(paste_file) db.session.commit() width, height = paste_file.image_size return jsonify({ 'url': paste_file.url_i, 'short_url': paste_file.url_s, 'origin_filename': paste_file.filename, 'hash': paste_file.filehash, 'width': width, 'height': height }) raise ApiException(code='001', msg='文件上传错误')
def get_user(uid): user = User.query.filter_by(id=uid).first() if not user: raise ApiException(msg='用户不存在') else: return user