@ -1,132 +1,17 @@ | |||
from flask import request, Flask, jsonify | |||
import json | |||
from flask_sqlalchemy import SQLAlchemy | |||
from APP.view import collection, block | |||
from APP.view.auth import bp_auth | |||
from APP.view.block import bp_block | |||
from APP.view.collection import bp_collection | |||
from APP.view.database import init_db | |||
app = Flask(__name__) | |||
app.debug = True | |||
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:cui,logic@127.0.0.1/elderlyassistant' | |||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |||
app.config['SECRET_KEY'] = '24' | |||
db = SQLAlchemy(app) | |||
class Users(db.Model): | |||
__tablename__ = 'users' | |||
phonenum = db.Column(db.String(255), primary_key=True) | |||
password = db.Column(db.String(255)) | |||
name = db.Column(db.String(255)) | |||
@app.route('/', methods=['POST', 'GET']) | |||
@app.route('/login', methods=['POST', 'GET']) | |||
def login(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
password = datas['password'] | |||
name = '' | |||
msg = '' | |||
userlist = db.session.query(Users) | |||
flag = 0 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
if user.password == password: | |||
flag = 1 | |||
name = user.name | |||
else: | |||
flag = 2 | |||
break | |||
if flag == 1: | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if flag == 0: | |||
msg = "手机号还未注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
msg = "密码错误!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "请输入完整登录信息!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新登录!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
@app.route('/register', methods=['POST', 'GET']) | |||
def register(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
name = datas['name'] | |||
password = datas['password'] | |||
password1 = datas['password1'] | |||
msg = '' | |||
userlist = db.session.query(Users) | |||
flag = 1 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
flag = 0 | |||
break | |||
if flag == 0: | |||
msg = "手机号已被注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if password == password1: | |||
user = Users(phonenum=phonenum, name=name, password=password) | |||
db.session.add(user) | |||
db.session.commit() | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
msg = "两次输入密码不一致!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "请输入完整登录信息!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新注册!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
@app.route('/forget', methods=['POST', 'GET']) | |||
def forget(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
name = datas['name'] | |||
password = datas['password'] | |||
password1 = datas['password1'] | |||
msg = '' | |||
userlist = db.session.query(Users) | |||
flag = 0 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
if user.name == name: | |||
user.password = password | |||
db.session.commit() | |||
flag = 1 | |||
else: | |||
flag = 2 | |||
break | |||
if flag == 0: | |||
msg = "手机号还未注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if password == password1: | |||
if flag == 1: | |||
msg = "修改密码成功!" | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
msg = "用户名输入错误!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "两次输入密码不一致!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "请输入完整修改密码信息!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新修改密码!" | |||
return jsonify({'code': 5, 'msg': msg}) | |||
app = Flask(__name__) | |||
app.register_blueprint(bp_auth) | |||
app.register_blueprint(bp_block) | |||
app.register_blueprint(bp_collection) | |||
init_db() | |||
app.run(debug=True) |
@ -0,0 +1,123 @@ | |||
from flask import request, Flask, jsonify, Blueprint | |||
import json | |||
from APP.view.database import db_session | |||
from APP.view.model import Users | |||
bp_auth = Blueprint("bp_auth", __name__,url_prefix="/bp_auth") | |||
@bp_auth.route('/login', methods=['POST', 'GET']) | |||
def login(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
password = datas['password'] | |||
name = '' | |||
msg = '' | |||
userlist = db_session.query(Users) | |||
flag = 0 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
if user.password == password: | |||
flag = 1 | |||
name = user.name | |||
else: | |||
flag = 2 | |||
break | |||
if flag == 1: | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if flag == 0: | |||
msg = "手机号还未注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
msg = "密码错误!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "请输入完整登录信息!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新登录!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
@bp_auth.route('/register', methods=['POST', 'GET']) | |||
def register(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
name = datas['name'] | |||
password = datas['password'] | |||
password1 = datas['password1'] | |||
msg = '' | |||
userlist = db_session.query(Users) | |||
flag = 1 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
flag = 0 | |||
break | |||
if flag == 0: | |||
msg = "手机号已被注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if password == password1: | |||
user = Users(phonenum=phonenum, name=name, password=password) | |||
db_session.add(user) | |||
db_session.commit() | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
msg = "两次输入密码不一致!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "请输入完整登录信息!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新注册!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
@bp_auth.route('/forget', methods=['POST', 'GET']) | |||
def forget(): | |||
if request.method == 'POST': | |||
datas = json.loads(request.get_data()) | |||
phonenum = datas['phonenum'] | |||
name = datas['name'] | |||
password = datas['password'] | |||
password1 = datas['password1'] | |||
msg = '' | |||
userlist = db_session.query(Users) | |||
flag = 0 | |||
for user in userlist: | |||
if user.phonenum == phonenum: | |||
if user.name == name: | |||
user.password = password | |||
db_session.commit() | |||
flag = 1 | |||
else: | |||
flag = 2 | |||
break | |||
if flag == 0: | |||
msg = "手机号还未注册!" | |||
return jsonify({'code': 0, 'msg': msg}) | |||
else: | |||
if phonenum != '' and password != '': | |||
if password == password1: | |||
if flag == 1: | |||
msg = "修改密码成功!" | |||
return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) | |||
else: | |||
msg = "用户名输入错误!" | |||
return jsonify({'code': 2, 'msg': msg}) | |||
else: | |||
msg = "两次输入密码不一致!" | |||
return jsonify({'code': 3, 'msg': msg}) | |||
else: | |||
msg = "请输入完整修改密码信息!" | |||
return jsonify({'code': 4, 'msg': msg}) | |||
else: | |||
msg = "提交失败,请重新修改密码!" | |||
return jsonify({'code': 5, 'msg': msg}) |
@ -0,0 +1,19 @@ | |||
from sqlalchemy import create_engine | |||
from sqlalchemy.orm import scoped_session, sessionmaker | |||
from sqlalchemy.ext.declarative import declarative_base | |||
engine = create_engine('mysql+pymysql://root:WAMM0609dd@localhost:3306/cp', convert_unicode=True) | |||
db_session = scoped_session(sessionmaker(autocommit=False, | |||
autoflush=False, | |||
bind=engine)) | |||
Base = declarative_base() | |||
Base.query = db_session.query_property() | |||
def init_db(): | |||
# 在这里导入定义模型所需要的所有模块,这样它们就会正确的注册在 | |||
# 元数据上。否则你就必须在调用 init_db() 之前导入它们。 | |||
import APP.view.model | |||
Base.metadata.drop_all(bind=engine) | |||
Base.metadata.create_all(bind=engine) | |||
init_db() |
@ -0,0 +1,51 @@ | |||
from sqlalchemy import Column, Integer, String, Text | |||
from APP.view.database import Base | |||
class Users(Base): | |||
__tablename__ = 'users' | |||
phonenum = Column(String(255), primary_key=True) | |||
password = Column(String(255)) | |||
name = Column(String(255)) | |||
def __init__(self, name=None, password=None, phonenum=None): | |||
self.name = name | |||
self.password = password | |||
self.phonenum = phonenum | |||
class Collection(Base): | |||
__tablename__ = 'collection' | |||
id = Column(String(255), primary_key=True) | |||
name = Column(String(255)) | |||
tag = Column(String(255)) | |||
phonenum = Column(String(255)) | |||
def __init__(self, id=None, name=None, tag=None, phonenum=None): | |||
self.id = id | |||
self.name = name | |||
self.tag = tag | |||
self.phonenum = phonenum | |||
class Block(Base): | |||
__tablename__ = 'block' | |||
id = Column(String(255), primary_key=True) | |||
content = Column(String(255)) | |||
# type = Column(String(255)) | |||
def __init__(self, id=None, content=None): | |||
self.id = id | |||
self.content = content | |||
class CollectionBlock(Base): | |||
__tablename__ = 'collection_block' | |||
id = Column(String(255), primary_key=True) | |||
# password = Column(String(255)) | |||
block_id = Column(String(255)) | |||
def __init__(self, id=None, block_id=None): | |||
self.id = id | |||
self.block_id = block_id |