将权限与部门的逻辑分包
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
from applications.view.admin import init_admin_views
|
||||
from applications.view.index import init_index_views
|
||||
from applications.view.admin import register_admin_views
|
||||
from applications.view.index import register_index_views
|
||||
from applications.view.passport import register_passport_views
|
||||
from applications.view.rights import register_rights_view
|
||||
from applications.view.department import register_dept_views
|
||||
|
||||
|
||||
def init_view(app):
|
||||
init_admin_views(app)
|
||||
init_index_views(app)
|
||||
register_admin_views(app)
|
||||
register_index_views(app)
|
||||
register_rights_view(app)
|
||||
register_passport_views(app)
|
||||
register_dept_views(app)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
from flask import Flask
|
||||
|
||||
from applications.view.admin.admin_log import admin_log
|
||||
from applications.view.admin.dept import admin_dept
|
||||
from applications.view.admin.dict import admin_dict
|
||||
from applications.view.admin.index import admin_index
|
||||
from applications.view.admin.index import admin_bp
|
||||
from applications.view.admin.file import admin_file
|
||||
from applications.view.admin.power import admin_power
|
||||
from applications.view.admin.role import admin_role
|
||||
@@ -11,8 +10,8 @@ from applications.view.admin.user import admin_user
|
||||
from applications.view.admin.monitor import admin_monitor_bp
|
||||
|
||||
|
||||
def init_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_index)
|
||||
def register_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_bp)
|
||||
app.register_blueprint(admin_user)
|
||||
app.register_blueprint(admin_file)
|
||||
app.register_blueprint(admin_monitor_bp)
|
||||
@@ -20,4 +19,3 @@ def init_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_power)
|
||||
app.register_blueprint(admin_role)
|
||||
app.register_blueprint(admin_dict)
|
||||
app.register_blueprint(admin_dept)
|
||||
|
||||
@@ -1,100 +1,19 @@
|
||||
from flask import Blueprint, render_template, jsonify, request, session, redirect, url_for
|
||||
from flask_login import login_user, login_required, logout_user, current_user
|
||||
from applications.common.admin import index_curd
|
||||
from applications.common.admin_log import login_log
|
||||
from applications.common.utils.http import fail_api, success_api
|
||||
from applications.models import User
|
||||
from flask import Blueprint, render_template
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
admin_index = Blueprint('adminIndex', __name__, url_prefix='/admin')
|
||||
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
|
||||
|
||||
|
||||
# 首页
|
||||
@admin_index.get('/')
|
||||
@admin_bp.get('/')
|
||||
@login_required
|
||||
def index():
|
||||
user = current_user
|
||||
return render_template('admin/index.html', user=user)
|
||||
|
||||
|
||||
# 渲染配置
|
||||
@admin_index.get('/configs')
|
||||
@login_required
|
||||
def configs():
|
||||
return index_curd.get_render_config()
|
||||
|
||||
|
||||
# 获取验证码
|
||||
@admin_index.get('/getCaptcha')
|
||||
def get_captcha():
|
||||
resp, code = index_curd.get_captcha()
|
||||
session["code"] = code
|
||||
return resp
|
||||
|
||||
|
||||
# 登录
|
||||
@admin_index.get('/login')
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('adminIndex.index'))
|
||||
return render_template('admin/login.html')
|
||||
|
||||
|
||||
# 登录
|
||||
@admin_index.post('/login')
|
||||
def login_post():
|
||||
req = request.form
|
||||
username = req.get('username')
|
||||
password = req.get('password')
|
||||
code = req.get('captcha')
|
||||
|
||||
if not username or not password or not code:
|
||||
return fail_api(msg="用户名或密码没有输入")
|
||||
s_code = session.get("code", None)
|
||||
|
||||
if not all([code, s_code]):
|
||||
return fail_api(msg="参数错误")
|
||||
|
||||
if code != s_code:
|
||||
return fail_api(msg="验证码错误")
|
||||
user = User.query.filter_by(username=username).first()
|
||||
|
||||
if user is None:
|
||||
return fail_api(msg="不存在的用户")
|
||||
|
||||
if user.enable is 0:
|
||||
return fail_api(msg="用户被暂停使用")
|
||||
|
||||
if username == user.username and user.validate_password(password):
|
||||
# 登录
|
||||
login_user(user)
|
||||
# 记录登录日志
|
||||
login_log(request, uid=user.id, is_access=True)
|
||||
# 存入权限
|
||||
index_curd.add_auth_session()
|
||||
return success_api(msg="登录成功")
|
||||
login_log(request, uid=user.id, is_access=False)
|
||||
return fail_api(msg="用户名或密码错误")
|
||||
|
||||
|
||||
# 退出登录
|
||||
@admin_index.post('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
session.pop('permissions')
|
||||
return success_api(msg="注销成功")
|
||||
|
||||
|
||||
# 菜单
|
||||
@admin_index.get('/menu')
|
||||
@login_required
|
||||
def menu():
|
||||
menu_tree = index_curd.make_menu_tree()
|
||||
return jsonify(menu_tree)
|
||||
|
||||
|
||||
# 控制台页面
|
||||
@admin_index.get('/welcome')
|
||||
@admin_bp.get('/welcome')
|
||||
@login_required
|
||||
def welcome():
|
||||
return render_template('admin/console/console.html')
|
||||
|
||||
@@ -7,16 +7,20 @@ from applications.common.utils.validate import check_data
|
||||
from applications.models import DeptSchema
|
||||
from applications.common.admin import dept_curd as dept_curd
|
||||
|
||||
admin_dept = Blueprint('adminDept', __name__, url_prefix='/admin/dept')
|
||||
dept_bp = Blueprint('dept', __name__, url_prefix='/dept')
|
||||
|
||||
|
||||
@admin_dept.get('/')
|
||||
def register_dept_views(app):
|
||||
app.register_blueprint(dept_bp)
|
||||
|
||||
|
||||
@dept_bp.get('/')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/dept/main.html')
|
||||
|
||||
|
||||
@admin_dept.get('/data')
|
||||
@dept_bp.get('/data')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def data():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
@@ -26,13 +30,13 @@ def data():
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@admin_dept.get('/add')
|
||||
@dept_bp.get('/add')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/dept/add.html')
|
||||
|
||||
|
||||
@admin_dept.get('/tree')
|
||||
@dept_bp.get('/tree')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def tree():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
@@ -44,7 +48,7 @@ def tree():
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@admin_dept.post('/save')
|
||||
@dept_bp.post('/save')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
@@ -53,7 +57,7 @@ def save():
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@admin_dept.get('/edit')
|
||||
@dept_bp.get('/edit')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def edit():
|
||||
_id = request.args.get("deptId")
|
||||
@@ -62,7 +66,7 @@ def edit():
|
||||
|
||||
|
||||
# 启用
|
||||
@admin_dept.put('/enable')
|
||||
@dept_bp.put('/enable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def enable():
|
||||
_id = request.json.get('deptId')
|
||||
@@ -75,7 +79,7 @@ def enable():
|
||||
|
||||
|
||||
# 禁用
|
||||
@admin_dept.put('/disable')
|
||||
@dept_bp.put('/disable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('deptId')
|
||||
@@ -87,7 +91,7 @@ def dis_enable():
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@admin_dept.put('/update')
|
||||
@dept_bp.put('/update')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def update():
|
||||
req = request.json
|
||||
@@ -98,7 +102,7 @@ def update():
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
@admin_dept.delete('/remove/<int:_id>')
|
||||
@dept_bp.delete('/remove/<int:_id>')
|
||||
@authorize("admin:dept:remove", log=True)
|
||||
def remove(_id):
|
||||
res = dept_curd.remove_dept(_id)
|
||||
@@ -5,7 +5,7 @@ index_bp = Blueprint('Index', __name__, url_prefix='/')
|
||||
from . import index
|
||||
|
||||
|
||||
def init_index_views(app):
|
||||
def register_index_views(app):
|
||||
"""
|
||||
初始化蓝图
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
from flask import Blueprint, session, redirect, url_for, render_template, request
|
||||
from flask_login import current_user, login_user, login_required, logout_user
|
||||
|
||||
from applications.common.admin import index_curd
|
||||
from applications.common.admin_log import login_log
|
||||
from applications.common.utils.http import fail_api, success_api
|
||||
from applications.models import User
|
||||
|
||||
passport_bp = Blueprint('passport', __name__, url_prefix='/passport')
|
||||
|
||||
|
||||
def register_passport_views(app):
|
||||
app.register_blueprint(passport_bp)
|
||||
|
||||
|
||||
# 获取验证码
|
||||
@passport_bp.get('/getCaptcha')
|
||||
def get_captcha():
|
||||
resp, code = index_curd.get_captcha()
|
||||
session["code"] = code
|
||||
return resp
|
||||
|
||||
|
||||
# 登录
|
||||
@passport_bp.get('/login')
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('admin.index'))
|
||||
return render_template('admin/login.html')
|
||||
|
||||
|
||||
# 登录
|
||||
@passport_bp.post('/login')
|
||||
def login_post():
|
||||
req = request.form
|
||||
username = req.get('username')
|
||||
password = req.get('password')
|
||||
code = req.get('captcha')
|
||||
|
||||
if not username or not password or not code:
|
||||
return fail_api(msg="用户名或密码没有输入")
|
||||
s_code = session.get("code", None)
|
||||
|
||||
if not all([code, s_code]):
|
||||
return fail_api(msg="参数错误")
|
||||
|
||||
if code != s_code:
|
||||
return fail_api(msg="验证码错误")
|
||||
user = User.query.filter_by(username=username).first()
|
||||
|
||||
if user is None:
|
||||
return fail_api(msg="不存在的用户")
|
||||
|
||||
if user.enable is 0:
|
||||
return fail_api(msg="用户被暂停使用")
|
||||
|
||||
if username == user.username and user.validate_password(password):
|
||||
# 登录
|
||||
login_user(user)
|
||||
# 记录登录日志
|
||||
login_log(request, uid=user.id, is_access=True)
|
||||
# 存入权限
|
||||
index_curd.add_auth_session()
|
||||
return success_api(msg="登录成功")
|
||||
login_log(request, uid=user.id, is_access=False)
|
||||
return fail_api(msg="用户名或密码错误")
|
||||
|
||||
|
||||
# 退出登录
|
||||
@passport_bp.post('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
session.pop('permissions')
|
||||
return success_api(msg="注销成功")
|
||||
@@ -0,0 +1,9 @@
|
||||
from flask import Blueprint, Flask
|
||||
|
||||
rights_bp = Blueprint('rights', __name__, url_prefix='/rights')
|
||||
|
||||
from . import routes
|
||||
|
||||
|
||||
def register_rights_view(app: Flask):
|
||||
app.register_blueprint(rights_bp)
|
||||
@@ -0,0 +1,20 @@
|
||||
# 渲染配置
|
||||
from flask import jsonify
|
||||
from flask_login import login_required
|
||||
|
||||
from . import rights_bp
|
||||
from ...common.admin import index_curd
|
||||
|
||||
|
||||
@rights_bp.get('/configs')
|
||||
@login_required
|
||||
def configs():
|
||||
return index_curd.get_render_config()
|
||||
|
||||
|
||||
# 菜单
|
||||
@rights_bp.get('/menu')
|
||||
@login_required
|
||||
def menu():
|
||||
menu_tree = index_curd.make_menu_tree()
|
||||
return jsonify(menu_tree)
|
||||
Reference in New Issue
Block a user