将权限与部门的逻辑分包

This commit is contained in:
zhengxinonly
2021-06-13 15:07:40 +08:00
parent 5faf88283f
commit 9e8a5357be
19 changed files with 171 additions and 131 deletions
+3 -5
View File
@@ -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)
-108
View File
@@ -1,108 +0,0 @@
from flask import Blueprint, render_template, request, jsonify
from marshmallow import INCLUDE
from applications.common.utils.http import success_api, fail_api
from applications.common.utils.rights import authorize
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')
@admin_dept.get('/')
@authorize("admin:dept:main", log=True)
def main():
return render_template('admin/dept/main.html')
@admin_dept.get('/data')
@authorize("admin:dept:main", log=True)
def data():
power_data = dept_curd.get_dept_dict()
res = {
"data": power_data
}
return jsonify(res)
@admin_dept.get('/add')
@authorize("admin:dept:add", log=True)
def add():
return render_template('admin/dept/add.html')
@admin_dept.get('/tree')
@authorize("admin:dept:main", log=True)
def tree():
power_data = dept_curd.get_dept_dict()
res = {
"status": {"code": 200, "message": "默认"},
"data": power_data
}
return jsonify(res)
@admin_dept.post('/save')
@authorize("admin:dept:add", log=True)
def save():
req = request.json
check_data(DeptSchema(unknown=INCLUDE), req)
dept_curd.save_dept(req)
return success_api(msg="成功")
@admin_dept.get('/edit')
@authorize("admin:dept:edit", log=True)
def edit():
_id = request.args.get("deptId")
dept = dept_curd.get_dept_by_id(_id)
return render_template('admin/dept/edit.html', dept=dept)
# 启用
@admin_dept.put('/enable')
@authorize("admin:dept:edit", log=True)
def enable():
_id = request.json.get('deptId')
if id:
res = dept_curd.enable_status(_id)
if not res:
return fail_api(msg="出错啦")
return success_api(msg="启用成功")
return fail_api(msg="数据错误")
# 禁用
@admin_dept.put('/disable')
@authorize("admin:dept:edit", log=True)
def dis_enable():
_id = request.json.get('deptId')
if id:
res = dept_curd.disable_status(_id)
if not res:
return fail_api(msg="出错啦")
return success_api(msg="禁用成功")
return fail_api(msg="数据错误")
@admin_dept.put('/update')
@authorize("admin:dept:edit", log=True)
def update():
req = request.json
check_data(DeptSchema(unknown=INCLUDE), req)
res = dept_curd.update_dept(req)
if not res:
return fail_api(msg="更新失败")
return success_api(msg="更新成功")
@admin_dept.delete('/remove/<int:_id>')
@authorize("admin:dept:remove", log=True)
def remove(_id):
res = dept_curd.remove_dept(_id)
if res:
return success_api(msg="删除成功")
else:
return fail_api(msg="删除失败")
+5 -86
View File
@@ -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')