Browse Source

重构了后台,可以跑起来的第一个version

master
邓淳远 4 years ago
parent
commit
9544be3c9c
6 changed files with 257 additions and 158 deletions
  1. +11
    -126
      APP/server.py
  2. +123
    -0
      APP/view/auth.py
  3. +7
    -7
      APP/view/block.py
  4. +46
    -25
      APP/view/collection.py
  5. +19
    -0
      APP/view/database.py
  6. +51
    -0
      APP/view/model.py

+ 11
- 126
APP/server.py View File

@ -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)

+ 123
- 0
APP/view/auth.py View File

@ -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})

+ 7
- 7
APP/view/block.py View File

@ -1,10 +1,10 @@
from flask import Flask, render_template, request, jsonify, session, Blueprint
from bson import json_util
bp = Blueprint("block", __name__,url_prefix="/block")
bp_block = Blueprint("block", __name__,url_prefix="/block")
@bp.route("/add", methods=["POST"])
@bp_block.route("/add", methods=["POST"])
def add_block():
type = request.form.get('type')
content = request.form.get("content")
@ -16,7 +16,7 @@ def add_block():
return json_util.dumps()
@bp.route("/select", methods=["POST"])
@bp_block.route("/select", methods=["POST"])
def get_block():
# 查询这个id的collection的block
id = request.form.get('id')
@ -31,7 +31,7 @@ def get_block():
@bp.route("/delete", methods=["POST"])
@bp_block.route("/delete", methods=["POST"])
def delete():
collection_id = request.form.get('collection_id')
block_id = request.form.get('block_id')
@ -41,7 +41,7 @@ def delete():
ret = {'msg':'succuss'}
return json_util.dumps(ret)
@bp.route("/swap", methods=["POST"])
@bp_block.route("/swap", methods=["POST"])
def swap():
id = request.form.get('id')
collection_id = request.form.get('collection_id')
@ -53,7 +53,7 @@ def swap():
ret = {'msg':'succuss'}
return json_util.dumps(ret)
@bp.route("/edit", methods=["POST"])
@bp_block.route("/edit", methods=["POST"])
def edit():
collection_id = request.form.get('collection_id')
block_id = request.form.get('block_id')
@ -66,7 +66,7 @@ def edit():
return json_util.dumps(ret)
@bp.route("/get_web_name", methods=["POST"])
@bp_block.route("/get_web_name", methods=["POST"])
def get_web_name():
url = request.form.get('url')
ret = {'msg':'succuss'}

+ 46
- 25
APP/view/collection.py View File

@ -1,38 +1,54 @@
from flask import Flask, render_template, request, jsonify, session, Blueprint
from bson import json_util
bp = Blueprint("collection", __name__,url_prefix="/collection")
from APP.view.database import db_session
from APP.view.model import Collection
bp_collection = Blueprint("collection", __name__, url_prefix="/collection")
@bp.route("/add", methods=["POST"])
@bp_collection.route("/add", methods=["POST"])
def add_collection():
name = request.form.get('name')
tag = request.form.get('tag', None)
ret = {'msg':'succuss','name':name}
"""
phonenum = request.form.get('phonenum')
try:
count = Collection.query.count()
print(count)
c = Collection(id=count + 1, name=name, tag=tag, phonenum=phonenum)
db_session.add(c)
db_session.commit()
except BaseException as e:
print(e)
ret = {'msg': 'failed!', 'name': name}
return json_util.dumps(ret)
ret = {'msg': 'succuss', 'name': name, 'id': str(count + 1)}
"""
ret['id'] =
"""
return json_util.dumps(ret)
@bp.route("/select", methods=["POST"])
@bp_collection.route("/select", methods=["POST"])
def get_collection():
# 搜索的关键词
name = request.form.get('name', None)
# 搜索对象
phonenum = request.form.get('phonenum',None)
phonenum = request.form.get('phonenum', None)
collections = []
"""
collections.append({'id': '1', 'name': 'xuanz','like':1})
collections.append({'id': '2', 'name': 'mingg','like':2})
collections.append({'id': '3', 'name': 'wnqian','like':3})
"""
ret = {'collections':collections,'msg':'succuss'}
ret = {'collections': collections, 'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/recommand", methods=["POST"])
def get_collection():
@bp_collection.route("/recommand", methods=["POST"])
def recommand_collection():
collections = []
"""
@ -40,11 +56,12 @@ def get_collection():
collections.append({'id': '2', 'name': 'mingg','like':2})
collections.append({'id': '3', 'name': 'wnqian','like':3})
"""
ret = {'collections':collections,'msg':'succuss'}
ret = {'collections': collections, 'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/isLike", methods=["POST"])
def islike():
@bp_collection.route("/isLike", methods=["POST"])
def islike():
ret = {'msg': 'succuss'}
id = request.form.get('collection_id')
"""
@ -54,27 +71,29 @@ def islike():
return json_util.dumps(ret)
@bp.route("/like", methods=["POST"])
@bp_collection.route("/like", methods=["POST"])
def get_like():
id = request.form.get('colllection_id')
"""
"""
ret = {'msg':'succuss'}
ret = {'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/unlike", methods=["POST"])
@bp_collection.route("/unlike", methods=["POST"])
def get_unlike():
id = request.form.get('colllection_id')
"""
"""
ret = {'msg':'succuss'}
ret = {'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/swap", methods=["POST"])
@bp_collection.route("/swap", methods=["POST"])
def swap():
id = request.form.get('id')
order = request.form.get('new_order')
@ -82,26 +101,28 @@ def swap():
id的collection和顺序是order的collection交换
"""
ret = {'msg':'succuss'}
ret = {'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/delete", methods=["POST"])
@bp_collection.route("/delete", methods=["POST"])
def delete():
id = request.form.get('collection_id')
"""
id的collection
"""
ret = {'msg':'succuss'}
ret = {'msg': 'succuss'}
return json_util.dumps(ret)
@bp.route("/edit", methods=["POST"])
@bp_collection.route("/edit", methods=["POST"])
def edit():
id = request.form.get('collection_id')
name = request.form.get('name', None)
tag = request.form.get('tag', None)
"""
none的情况请直接返回
"""
ret = {'msg':'succuss'}
return json_util.dumps(ret)
ret = {'msg': 'succuss'}
return json_util.dumps(ret)

+ 19
- 0
APP/view/database.py View File

@ -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()

+ 51
- 0
APP/view/model.py View File

@ -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

Loading…
Cancel
Save