新增登陆的后端逻辑
- login_service.py
from dao.user_dao import UserDAO
from util.log_util import Log
userdao = UserDAO()
log = Log()
"""
登陆服务层逻辑
"""
class LoginService():
def login(self,id):
"""
登陆逻辑
:param userdao:
:return:
"""
res = userdao.get_user(id)
if res:
return res
else:
log.info("未查询到用户信息")
return False
- login_controller.py
from flask import request, jsonify
from flask_restx import Namespace, Resource
from service.login_service import LoginService
from server import api
from util.log_util import Log
from dao.user_dao import UserDAO
loginservice = LoginService()
userdao = UserDAO()
log = Log()
login_ns = Namespace("login", description="登陆")
@login_ns.route("")
class LoginController(Resource):
post_parser = api.parser()
post_parser.add_argument("id", type=int, required=True, location="json")
post_parser.add_argument("username", type=str, required=True, location="json")
post_parser.add_argument("password", type=str, required=True, location="json")
@login_ns.expect(post_parser)
def post(self):
user_data = request.json
log.info(f"接收登陆的用户参数<======={user_data}")
userdao = UserDAO(**user_data)
# 调用DO层中的as_user_entity_dict(),把python对象转换成前端可以使用的json
res = loginservice.login(userdao.id).as_user_entity_dict()
# 把从前端拿到到参数与DB中查询数据比对,相同时候登陆成功
if res == user_data:
return jsonify({"code":0, "msg":"user login success", "data":user_data})
else: #否则登陆失败
return jsonify({"code":40001, "msg":"user login fail, user or password error", "data":user_data})
- 测试登陆API
已存在数据的用户登陆
未存在数据的用户登陆
新增注册的后端逻辑
- register_service.py
from dao.user_dao import UserDAO
from util.log_util import Log
userdao = UserDAO()
log = Log()
"""
注册服务层逻辑
"""
class RegisterService():
def register(self ,userdao):
"""
注册逻辑
:param userdao:
:return:
"""
res = userdao.get_user(userdao.id)
if res:
log.info("用户已存在,无法注册®️")
return False
else:
return userdao.add_user(userdao)
- register_controller.py
from flask import request, jsonify
from flask_restx import Namespace, Resource
from service.register_service import RegisterService
from server import api
from util.log_util import Log
from dao.user_dao import UserDAO
registerservice = RegisterService()
userdao = UserDAO()
log = Log()
register_ns = Namespace("register", description="注册")
@register_ns.route("")
class RegisterController(Resource):
post_parser = api.parser()
post_parser.add_argument("id", type=int, required=True, location="json")
post_parser.add_argument("username", type=str, required=True, location="json")
post_parser.add_argument("password", type=str, required=True, location="json")
@register_ns.expect(post_parser)
def post(self):
user_data = request.json
log.info(f"接收登陆的用户参数<======={user_data}")
userdao = UserDAO(**user_data)
res = registerservice.register(userdao)
# 注册返回有值时,注册成功
if res:
return jsonify({"code":0, "msg":"user register success", "data":res})
else: #否则注册失败
return jsonify({"code":40001, "msg":"user register fail, userID or username exists", "data":res})
- 测试注册API
未存在的用户注册
已经存在的用户注册
注意:生成swagger接口文档,需要把login和register在启动类server.py导入加入命名空间
from flask import Flask
from flask_restx import Api
from flask_sqlalchemy import SQLAlchemy
"""
启动类
"""
app = Flask(__name__)
# todo 应用配置从py文件读取,便于后期维护
app.config.from_object("util.config.Config")
db = SQLAlchemy(app) # 创建SQLAlchemy对象,并与Flask实例建立关联
# 应用程序对象对其进行初始化,定义好SwaggerAPI文档的内容和地址
api = Api(app,version="1.0", title="测试平台API接口", description="TestPlatform API", doc="/doc")
@app.route("/")
def hello_flask():
return "welcome to flask"
# todo 添加路由,把controller的命名空间加到启动类中,否则swagger无法展示API接口
def add_router():
from controller.user_controller import user_ns
from controller.login_controller import login_ns
from controller.register_controller import register_ns
api.add_namespace(user_ns,"/user") # 添加api的命名空间,解决swagger不展示内容的问题
api.add_namespace(login_ns, "/login")
api.add_namespace(register_ns, "/register")
if __name__ == "__main__":
add_router()
app.run(debug=True)
好了,以上是注册和登录的简单逻辑