Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
usermgr.py 4.02 KB
Copy Edit Web IDE Raw Blame History
Mendalas authored 2019-06-14 15:24 . 1
#coding:utf-8
from flask_restful import Resource
from auth import auth
from flask import abort, request, jsonify, g, url_for
from user import User
from flask_sqlalchemy import SQLAlchemy
from api import app
db = SQLAlchemy(app)
class LoginApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
if 'password' in request.get_json():
password = request.get_json()['password']
print "use try login, phone:%s, pwd:%s", phone, password
usr = db.session.query(User).filter(User.phone==phone).first()
if not usr:
return jsonify({'status':-1,'errormsg':'用户不存在'})
#if not usr.verify_password(password):
# return jsonify({'status': -2,'errormsg':'密码错误'})
if usr.password_hash != password:
return jsonify({'status': -2,'errormsg':'密码错误'})
token = usr.generate_auth_token(600)
g.user=usr
return jsonify({'status':0,'token': token.decode('ascii'), 'duration': 600})
class ChangePwdApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
if 'password' in request.get_json():
password = request.get_json()['password']
print "use try change pwd, phone:%s, pwd:%s", phone, password
user = db.session.query(User).filter(User.phone==phone).first()
if not user:
return jsonify({'status':-1,'errormsg':'用户不存在'})
user.password_hash = password
db.session.commit()
return jsonify({'status':0})
class AddUserApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
if 'username' in request.get_json():
username = request.get_json()['username']
if 'password' in request.get_json():
password = request.get_json()['password']
if 'eid' in request.get_json():
eid = request.get_json()['eid']
if 'position' in request.get_json():
position = request.get_json()['position']
if 'right' in request.get_json():
right = request.get_json()['right']
print "use try add, phone:%s, pwd:%s", phone, password
user = User(phone=phone,username=username,password_hash=password,position=position,eid=eid,right=right)
try:
db.session.add(user)
db.session.commit()
except Exception as e:
db.session.rollback()
return jsonify({'status':0})
return jsonify({'status':1})
class DelUserApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
print "use try del user, phone:%s", phone
try:
user = db.session.query(User).filter(User.phone==phone).first()
db.session.delete(user)
db.session.commit()
except Exception as e:
db.session.rollback()
return jsonify({'status':0})
return jsonify({'status':1})
class UpdateUserApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
if 'username' in request.get_json():
username = request.get_json()['username']
if 'eid' in request.get_json():
eid = request.get_json()['eid']
if 'position' in request.get_json():
position = request.get_json()['position']
if 'right' in request.get_json():
right = request.get_json()['right']
print "use try update, phone:%s", phone
user = db.session.query(User).filter(User.phone==phone).first()
if not user:
return jsonify({'status':-1,'errormsg':'用户不存在'})
user.username = username
user.eid = eid
user.right = right
user.position = position
db.session.commit()
return jsonify({'status':0})
class QueryUserApi(Resource):
def post(self):
app.logger.info(request.get_json())
if 'phone' in request.get_json():
phone = request.get_json()['phone']
print "use try query user, phone:%s", phone
if phone=="0":
users = db.session.query(User).all()
else:
users = db.session.query(User).filter(User.phone==phone)
v = []
for r in users:
v.append(r.to_json())
app.logger.info(v)
return v

Comment ( 0 )

Sign in for post a comment