去除首页面,优化命名,优化schema
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
from flask import Flask, Blueprint
|
||||
|
||||
from applications.view.system.dict import bp as dict_bp
|
||||
from applications.view.system.file import bp as file_bp
|
||||
from applications.view.system.index import bp as index_bp
|
||||
from applications.view.system.log import bp as log_bp
|
||||
from applications.view.system.mail import bp as mail_bp
|
||||
from applications.view.system.monitor import bp as monitor_bp
|
||||
from applications.view.system.passport import bp as passport_bp
|
||||
from applications.view.system.power import bp as power_bp
|
||||
from applications.view.system.rights import bp as right_bp
|
||||
from applications.view.system.role import bp as role_bp
|
||||
from applications.view.system.user import bp as user_bp
|
||||
from applications.view.system.dept import bp as dept_bp
|
||||
|
||||
# 创建sys
|
||||
system_bp = Blueprint('system', __name__, url_prefix='/system')
|
||||
|
||||
|
||||
def register_system_bps(app: Flask):
|
||||
# 在admin_bp下注册子蓝图
|
||||
system_bp.register_blueprint(user_bp)
|
||||
system_bp.register_blueprint(file_bp)
|
||||
system_bp.register_blueprint(monitor_bp)
|
||||
system_bp.register_blueprint(log_bp)
|
||||
system_bp.register_blueprint(power_bp)
|
||||
system_bp.register_blueprint(role_bp)
|
||||
system_bp.register_blueprint(dict_bp)
|
||||
system_bp.register_blueprint(mail_bp)
|
||||
system_bp.register_blueprint(passport_bp)
|
||||
system_bp.register_blueprint(right_bp)
|
||||
system_bp.register_blueprint(dept_bp)
|
||||
app.register_blueprint(index_bp)
|
||||
app.register_blueprint(system_bp)
|
||||
@@ -0,0 +1,139 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.utils import validate
|
||||
from applications.common.utils.http import success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Dept, User
|
||||
from applications.schemas import DeptSchema
|
||||
|
||||
bp = Blueprint('dept', __name__, url_prefix='/dept')
|
||||
|
||||
|
||||
@bp.get('/')
|
||||
@authorize("system:dept:main", log=True)
|
||||
def main():
|
||||
return render_template('system/dept/main.html')
|
||||
|
||||
|
||||
@bp.post('/data')
|
||||
@authorize("system:dept:main", log=True)
|
||||
def data():
|
||||
data = Dept.query.order_by(Dept.sort).all()
|
||||
res = {
|
||||
"data": DeptSchema(many=True).dump(data)
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@bp.get('/add')
|
||||
@authorize("system:dept:add", log=True)
|
||||
def add():
|
||||
return render_template('system/dept/add.html')
|
||||
|
||||
|
||||
@bp.get('/tree')
|
||||
@authorize("system:dept:main", log=True)
|
||||
def tree():
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
power_data = curd.model_to_dicts(schema=DeptSchema, data=dept)
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@bp.post('/save')
|
||||
@authorize("system:dept:add", log=True)
|
||||
def save():
|
||||
req_json = request.get_json(force=True)
|
||||
dept = Dept(
|
||||
parent_id=req_json.get('parentId'),
|
||||
dept_name=str_escape(req_json.get('deptName')),
|
||||
sort=str_escape(req_json.get('sort')),
|
||||
leader=str_escape(req_json.get('leader')),
|
||||
phone=str_escape(req_json.get('phone')),
|
||||
email=str_escape(req_json.get('email')),
|
||||
status=str_escape(req_json.get('status')),
|
||||
address=str_escape(req_json.get('address'))
|
||||
)
|
||||
r = db.session.add(dept)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@bp.get('/edit')
|
||||
@authorize("system:dept:edit", log=True)
|
||||
def edit():
|
||||
_id = request.args.get("deptId")
|
||||
dept = curd.get_one_by_id(model=Dept, id=_id)
|
||||
return render_template('system/dept/edit.html', dept=dept)
|
||||
|
||||
|
||||
# 启用
|
||||
@bp.put('/enable')
|
||||
@authorize("system:dept:edit", log=True)
|
||||
def enable():
|
||||
id = request.get_json(force=True).get('deptId')
|
||||
if id:
|
||||
enable = 1
|
||||
d = Dept.query.filter_by(id=id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="出错啦")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用
|
||||
@bp.put('/disable')
|
||||
@authorize("system:dept:edit", log=True)
|
||||
def dis_enable():
|
||||
id = request.get_json(force=True).get('deptId')
|
||||
if id:
|
||||
enable = 0
|
||||
d = Dept.query.filter_by(id=id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="出错啦")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@bp.put('/update')
|
||||
@authorize("system:dept:edit", log=True)
|
||||
def update():
|
||||
json = request.get_json(force=True)
|
||||
id = json.get("deptId"),
|
||||
data = {
|
||||
"dept_name": validate.str_escape(json.get("deptName")),
|
||||
"sort": validate.str_escape(json.get("sort")),
|
||||
"leader": validate.str_escape(json.get("leader")),
|
||||
"phone": validate.str_escape(json.get("phone")),
|
||||
"email": validate.str_escape(json.get("email")),
|
||||
"status": validate.str_escape(json.get("status")),
|
||||
"address": validate.str_escape(json.get("address"))
|
||||
}
|
||||
d = Dept.query.filter_by(id=id).update(data)
|
||||
if not d:
|
||||
return fail_api(msg="更新失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
@bp.delete('/remove/<int:_id>')
|
||||
@authorize("system:dept:remove", log=True)
|
||||
def remove(_id):
|
||||
d = Dept.query.filter_by(id=_id).delete()
|
||||
if not d:
|
||||
return fail_api(msg="删除失败")
|
||||
res = User.query.filter_by(dept_id=_id).update({"dept_id": None})
|
||||
db.session.commit()
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -0,0 +1,221 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.common.utils.http import table_api, success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import DictType, DictData
|
||||
from applications.schemas import DictTypeOutSchema, DictDataOutSchema
|
||||
|
||||
bp = Blueprint('dict', __name__, url_prefix='/dict')
|
||||
|
||||
|
||||
# 数据字典
|
||||
@bp.get('/')
|
||||
@authorize("system:dict:main")
|
||||
def main():
|
||||
return render_template('system/dict/main.html')
|
||||
|
||||
|
||||
@bp.get('/dictType/data')
|
||||
@authorize("system:dict:main")
|
||||
def dict_type_data():
|
||||
# 获取请求参数
|
||||
type_name = str_escape(request.args.get('typeName', type=str))
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if type_name:
|
||||
mf.vague(field_name="type_name", value=type_name)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
dict_all = DictType.query.filter(mf.get_filter(DictType)).layui_paginate()
|
||||
count = dict_all.total
|
||||
data = curd.model_to_dicts(schema=DictTypeOutSchema, data=dict_all.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
@bp.get('/dictType/add')
|
||||
@authorize("system:dict:add", log=True)
|
||||
def dict_type_add():
|
||||
return render_template('system/dict/add.html')
|
||||
|
||||
|
||||
@bp.post('/dictType/save')
|
||||
@authorize("system:dict:add", log=True)
|
||||
def dict_type_save():
|
||||
req_json = request.get_json(force=True)
|
||||
description = str_escape(req_json.get("description"))
|
||||
enable = str_escape(req_json.get("enable"))
|
||||
type_code = str_escape(req_json.get("typeCode"))
|
||||
type_name = str_escape(req_json.get("typeName"))
|
||||
d = DictType(type_name=type_name, type_code=type_code, enable=enable, description=description)
|
||||
db.session.add(d)
|
||||
db.session.commit()
|
||||
if d.id is None:
|
||||
return fail_api(msg="增加失败")
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
|
||||
# 编辑字典类型
|
||||
@bp.get('/dictType/edit')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_type_edit():
|
||||
_id = request.args.get('dictTypeId', type=int)
|
||||
dict_type = DictType.query.filter_by(id=_id).first()
|
||||
return render_template('system/dict/edit.html', dict_type=dict_type)
|
||||
|
||||
|
||||
# 编辑字典类型
|
||||
@bp.put('/dictType/update')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_type_update():
|
||||
req_json = request.get_json(force=True)
|
||||
id = str_escape(req_json.get("id"))
|
||||
description = str_escape(req_json.get("description"))
|
||||
enable = str_escape(req_json.get("enable"))
|
||||
type_code = str_escape(req_json.get("typeCode"))
|
||||
type_name = str_escape(req_json.get("typeName"))
|
||||
DictType.query.filter_by(id=id).update({
|
||||
"description": description,
|
||||
"enable": enable,
|
||||
"type_code": type_code,
|
||||
"type_name": type_name
|
||||
})
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 启用字典
|
||||
@bp.put('/dictType/enable')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_type_enable():
|
||||
_id = request.get_json(force=True).get('id')
|
||||
if id:
|
||||
res = curd.enable_status(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api("启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用字典
|
||||
@bp.put('/dictType/disable')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_type_dis_enable():
|
||||
_id = request.get_json(force=True).get('id')
|
||||
if id:
|
||||
res = curd.disable_status(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api("禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 删除字典类型
|
||||
@bp.delete('/dictType/remove/<int:_id>')
|
||||
@authorize("system:dict:remove", log=True)
|
||||
def dict_type_delete(_id):
|
||||
res = curd.delete_one_by_id(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
@bp.get('/dictData/data')
|
||||
@authorize("system:dict:main", log=True)
|
||||
def dict_code_data():
|
||||
type_code = str_escape(request.args.get('typeCode', type=str))
|
||||
dict_data = DictData.query.filter_by(type_code=type_code).layui_paginate()
|
||||
count = dict_data.total
|
||||
data = curd.model_to_dicts(schema=DictDataOutSchema, data=dict_data.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
# 增加字典数据
|
||||
@bp.get('/dictData/add')
|
||||
@authorize("system:dict:add", log=True)
|
||||
def dict_data_add():
|
||||
type_code = request.args.get('typeCode', type=str)
|
||||
return render_template('system/dict/data/add.html', type_code=type_code)
|
||||
|
||||
|
||||
# 增加字典数据
|
||||
@bp.post('/dictData/save')
|
||||
@authorize("system:dict:add", log=True)
|
||||
def dict_data_save():
|
||||
req_json = request.get_json(force=True)
|
||||
data_label = str_escape(req_json.get("dataLabel"))
|
||||
data_value = str_escape(req_json.get("dataValue"))
|
||||
enable = str_escape(req_json.get("enable"))
|
||||
remark = str_escape(req_json.get("remark"))
|
||||
type_code = str_escape(req_json.get("typeCode"))
|
||||
d = DictData(data_label=data_label, data_value=data_value, enable=enable, remark=remark, type_code=type_code)
|
||||
db.session.add(d)
|
||||
db.session.commit()
|
||||
if not d.id:
|
||||
return jsonify(success=False, msg="增加失败")
|
||||
return jsonify(success=True, msg="增加成功")
|
||||
|
||||
|
||||
# 编辑字典数据
|
||||
@bp.get('/dictData/edit')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_data_edit():
|
||||
_id = request.args.get('dataId', type=str)
|
||||
dict_data = curd.get_one_by_id(DictData, _id)
|
||||
return render_template('system/dict/data/edit.html', dict_data=dict_data)
|
||||
|
||||
|
||||
# 编辑字典数据
|
||||
@bp.put('/dictData/update')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_data_update():
|
||||
req_json = request.get_json(force=True)
|
||||
id = req_json.get("dataId")
|
||||
DictData.query.filter_by(id=id).update({
|
||||
"data_label": str_escape(req_json.get("dataLabel")),
|
||||
"data_value": str_escape(req_json.get("dataValue")),
|
||||
"enable": str_escape(req_json.get("enable")),
|
||||
"remark": str_escape(req_json.get("remark")),
|
||||
"type_code": str_escape(req_json.get("typeCode"))
|
||||
})
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 启用字典数据
|
||||
@bp.put('/dictData/enable')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_data_enable():
|
||||
_id = request.get_json(force=True).get('dataId')
|
||||
if _id:
|
||||
res = curd.enable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用字典数据
|
||||
@bp.put('/dictData/disable')
|
||||
@authorize("system:dict:edit", log=True)
|
||||
def dict_data_disenable():
|
||||
_id = request.get_json(force=True).get('dataId')
|
||||
if _id:
|
||||
res = curd.disable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 删除字典类型
|
||||
@bp.delete('dictData/remove/<int:id>')
|
||||
@authorize("system:dict:remove", log=True)
|
||||
def dict_data_delete(id):
|
||||
res = curd.delete_one_by_id(model=DictData, id=id)
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
@@ -0,0 +1,83 @@
|
||||
import os
|
||||
from flask import Blueprint, request, render_template, jsonify, current_app
|
||||
|
||||
from applications.common.utils.http import fail_api, success_api, table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.extensions import db
|
||||
from applications.models import Photo
|
||||
from applications.common.utils import upload as upload_curd
|
||||
|
||||
bp = Blueprint('adminFile', __name__, url_prefix='/file')
|
||||
|
||||
|
||||
# 图片管理
|
||||
@bp.get('/')
|
||||
@authorize("system:file:main")
|
||||
def index():
|
||||
return render_template('system/photo/photo.html')
|
||||
|
||||
|
||||
# 图片数据
|
||||
@bp.get('/table')
|
||||
@authorize("system:file:main")
|
||||
def table():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
data, count = upload_curd.get_photo(page=page, limit=limit)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
# 上传
|
||||
@bp.get('/upload')
|
||||
@authorize("system:file:add", log=True)
|
||||
def upload():
|
||||
return render_template('system/photo/photo_add.html')
|
||||
|
||||
|
||||
# 上传接口
|
||||
@bp.post('/upload')
|
||||
@authorize("system:file:add", log=True)
|
||||
def upload_api():
|
||||
if 'file' in request.files:
|
||||
photo = request.files['file']
|
||||
mime = request.files['file'].content_type
|
||||
|
||||
file_url = upload_curd.upload_one(photo=photo, mime=mime)
|
||||
res = {
|
||||
"msg": "上传成功",
|
||||
"code": 0,
|
||||
"success": True,
|
||||
"data":
|
||||
{"src": file_url}
|
||||
}
|
||||
return jsonify(res)
|
||||
return fail_api()
|
||||
|
||||
|
||||
# 图片删除
|
||||
@bp.route('/delete', methods=['GET', 'POST'])
|
||||
@authorize("system:file:delete", log=True)
|
||||
def delete():
|
||||
_id = request.form.get('id')
|
||||
res = upload_curd.delete_photo_by_id(_id)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
|
||||
|
||||
# 图片批量删除
|
||||
@bp.route('/batchRemove', methods=['GET', 'POST'])
|
||||
@authorize("system:file:delete", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
photo_name = Photo.query.filter(Photo.id.in_(ids)).all()
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
for p in photo_name:
|
||||
os.remove(upload_url + '/' + p.name)
|
||||
photo = Photo.query.filter(Photo.id.in_(ids)).delete(synchronize_session=False)
|
||||
db.session.commit()
|
||||
if photo:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -0,0 +1,14 @@
|
||||
from flask import Blueprint, render_template
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
bp = Blueprint('index', __name__, url_prefix='/')
|
||||
|
||||
|
||||
# 首页
|
||||
@bp.get('/')
|
||||
@login_required
|
||||
def index():
|
||||
user = current_user
|
||||
return render_template('system/index.html', user=user)
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from flask import Blueprint, request, render_template
|
||||
from sqlalchemy import desc
|
||||
from applications.common.utils.http import table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.models import AdminLog
|
||||
from applications.schemas import LogOutSchema
|
||||
from applications.common.curd import model_to_dicts
|
||||
|
||||
bp = Blueprint('log', __name__, url_prefix='/log')
|
||||
|
||||
|
||||
# 日志管理
|
||||
@bp.get('/')
|
||||
@authorize("system:log:main")
|
||||
def index():
|
||||
return render_template('system/admin_log/main.html')
|
||||
|
||||
|
||||
# 登录日志
|
||||
@bp.get('/loginLog')
|
||||
@authorize("system:log:main")
|
||||
def login_log():
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
log = AdminLog.query.filter_by(url='/passport/login').order_by(desc(AdminLog.create_time)).layui_paginate()
|
||||
count = log.total
|
||||
return table_api(data= model_to_dicts(schema=LogOutSchema, data=log.items), count=count)
|
||||
|
||||
|
||||
# 操作日志
|
||||
@bp.get('/operateLog')
|
||||
@authorize("system:log:main")
|
||||
def operate_log():
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
log = AdminLog.query.filter(
|
||||
AdminLog.url != '/passport/login').order_by(
|
||||
desc(AdminLog.create_time)).layui_paginate()
|
||||
count = log.total
|
||||
return table_api(data=model_to_dicts(schema=LogOutSchema, data=log.items), count=count)
|
||||
@@ -0,0 +1,98 @@
|
||||
from flask import Blueprint, render_template, request, current_app
|
||||
from flask_login import current_user
|
||||
from flask_mail import Message
|
||||
from applications.common.curd import model_to_dicts
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.common.utils.http import table_api, fail_api, success_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db, flask_mail
|
||||
from applications.models import Mail
|
||||
from applications.schemas import MailOutSchema
|
||||
|
||||
bp = Blueprint('adminMail', __name__, url_prefix='/mail')
|
||||
|
||||
|
||||
# 用户管理
|
||||
@bp.get('/')
|
||||
@authorize("system:mail:main")
|
||||
def main():
|
||||
return render_template('system/mail/main.html')
|
||||
|
||||
|
||||
# 用户分页查询
|
||||
@bp.get('/data')
|
||||
@authorize("system:mail:main")
|
||||
def data():
|
||||
# 获取请求参数
|
||||
receiver = str_escape(request.args.get("receiver"))
|
||||
subject = str_escape(request.args.get('subject'))
|
||||
content = str_escape(request.args.get('content'))
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if receiver:
|
||||
mf.contains(field_name="receiver", value=receiver)
|
||||
if subject:
|
||||
mf.contains(field_name="subject", value=subject)
|
||||
if content:
|
||||
mf.exact(field_name="content", value=content)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
mail = Mail.query.filter(mf.get_filter(Mail)).layui_paginate()
|
||||
count = mail.total
|
||||
# 返回api
|
||||
return table_api(data=model_to_dicts(schema=MailOutSchema, data=mail.items), count=count)
|
||||
|
||||
|
||||
# 用户增加
|
||||
@bp.get('/add')
|
||||
@authorize("system:mail:add", log=True)
|
||||
def add():
|
||||
return render_template('system/mail/add.html')
|
||||
|
||||
|
||||
@bp.post('/save')
|
||||
@authorize("system:mail:add", log=True)
|
||||
def save():
|
||||
req_json = request.get_json(force=True)
|
||||
receiver = str_escape(req_json.get("receiver"))
|
||||
subject = str_escape(req_json.get('subject'))
|
||||
content = str_escape(req_json.get('content'))
|
||||
user_id = current_user.id
|
||||
|
||||
try:
|
||||
msg = Message(subject=subject, recipients=receiver.split(";"), body=content)
|
||||
flask_mail.send(msg)
|
||||
except Exception as e:
|
||||
current_app.log_exception(e)
|
||||
return fail_api(msg="发送失败,请检查邮件配置或发送人邮箱是否写错")
|
||||
|
||||
mail = Mail(receiver=receiver, subject=subject, content=content, user_id=user_id)
|
||||
|
||||
db.session.add(mail)
|
||||
db.session.commit()
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
|
||||
# 删除用户
|
||||
@bp.delete('/remove/<int:id>')
|
||||
@authorize("system:mail:remove", log=True)
|
||||
def delete(id):
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
# 批量删除
|
||||
@bp.delete('/batchRemove')
|
||||
@authorize("system:mail:remove", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
for id in ids:
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return fail_api(msg="批量删除失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="批量删除成功")
|
||||
@@ -0,0 +1,99 @@
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import psutil
|
||||
import platform
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, render_template, jsonify
|
||||
from applications.common.utils.rights import authorize
|
||||
|
||||
bp = Blueprint('adminMonitor', __name__, url_prefix='/monitor')
|
||||
|
||||
|
||||
# 系统监控
|
||||
@bp.get('/')
|
||||
@authorize("system:monitor:main")
|
||||
def main():
|
||||
# 主机名称
|
||||
hostname = platform.node()
|
||||
# 系统版本
|
||||
system_version = platform.platform()
|
||||
# python版本
|
||||
python_version = platform.python_version()
|
||||
# 逻辑cpu数量
|
||||
cpu_count = psutil.cpu_count()
|
||||
# cpu使用率
|
||||
cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率
|
||||
# 内存
|
||||
memory_information = psutil.virtual_memory()
|
||||
# 内存使用率
|
||||
memory_usage = memory_information.percent
|
||||
memory_used = str(round(memory_information.used / 1024 / 1024))
|
||||
memory_total = str(round(memory_information.total / 1024 / 1024))
|
||||
memory_free = str(round(memory_information.free / 1024 / 1024))
|
||||
# 磁盘信息
|
||||
|
||||
disk_partitions_list = []
|
||||
# 判断是否在容器中
|
||||
if not os.path.exists('/.dockerenv'):
|
||||
disk_partitions = psutil.disk_partitions()
|
||||
for i in disk_partitions:
|
||||
a = psutil.disk_usage(i.device)
|
||||
disk_partitions_dict = {
|
||||
'device': i.device,
|
||||
'fstype': i.fstype,
|
||||
'total': str(round(a.total / 1024 / 1024)),
|
||||
'used': str(round(a.used / 1024 / 1024)),
|
||||
'free': str(round(a.free / 1024 / 1024)),
|
||||
'percent': a.percent
|
||||
}
|
||||
disk_partitions_list.append(disk_partitions_dict)
|
||||
|
||||
# 开机时间
|
||||
boot_time = datetime.fromtimestamp(psutil.boot_time()).replace(microsecond=0)
|
||||
up_time = datetime.now().replace(microsecond=0) - boot_time
|
||||
up_time_list = re.split(r':', str(up_time))
|
||||
up_time_format = " {} 小时{} 分钟{} 秒".format(up_time_list[0], up_time_list[1], up_time_list[2])
|
||||
|
||||
# 当前时间
|
||||
time_now = time.strftime('%H:%M:%S ', time.localtime(time.time()))
|
||||
return render_template('system/monitor.html',
|
||||
hostname=hostname,
|
||||
system_version=system_version,
|
||||
python_version=python_version,
|
||||
cpus_percent=cpus_percent,
|
||||
memory_usage=memory_usage,
|
||||
cpu_count=cpu_count,
|
||||
memory_used=memory_used,
|
||||
memory_total=memory_total,
|
||||
memory_free=memory_free,
|
||||
boot_time=boot_time,
|
||||
up_time_format=up_time_format,
|
||||
disk_partitions_list=disk_partitions_list,
|
||||
time_now=time_now
|
||||
|
||||
)
|
||||
|
||||
|
||||
# 图表 api
|
||||
@bp.get('/polling')
|
||||
@authorize("system:monitor:main")
|
||||
def ajax_polling():
|
||||
# 获取cpu使用率
|
||||
cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率
|
||||
# 获取内存使用率
|
||||
memory_information = psutil.virtual_memory()
|
||||
memory_usage = memory_information.percent
|
||||
time_now = time.strftime('%H:%M:%S ', time.localtime(time.time()))
|
||||
return jsonify(cups_percent=cpus_percent, memory_used=memory_usage, time_now=time_now)
|
||||
|
||||
|
||||
# 关闭程序
|
||||
@bp.get('/kill')
|
||||
@authorize("system:monitor:main")
|
||||
def kill():
|
||||
for proc in psutil.process_iter():
|
||||
if proc.pid == os.getpid():
|
||||
proc.kill()
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,89 @@
|
||||
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 import admin as 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
|
||||
|
||||
bp = Blueprint('passport', __name__, url_prefix='/passport')
|
||||
|
||||
|
||||
|
||||
|
||||
# 获取验证码
|
||||
@bp.get('/getCaptcha')
|
||||
def get_captcha():
|
||||
resp, code = index_curd.get_captcha()
|
||||
session["code"] = code
|
||||
return resp
|
||||
|
||||
|
||||
# 登录
|
||||
@bp.get('/login')
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('system.index'))
|
||||
return render_template('system/login.html')
|
||||
|
||||
|
||||
# 登录
|
||||
@bp.post('/login')
|
||||
def login_post():
|
||||
req = request.form
|
||||
username = req.get('username')
|
||||
password = req.get('password')
|
||||
code = req.get('captcha').__str__().lower()
|
||||
|
||||
if not username or not password or not code:
|
||||
return fail_api(msg="用户名或密码没有输入")
|
||||
s_code = session.get("code", None)
|
||||
session["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 not user:
|
||||
return fail_api(msg="不存在的用户")
|
||||
|
||||
if user.enable == 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)
|
||||
# 授权路由存入session
|
||||
role = current_user.role
|
||||
user_power = []
|
||||
for i in role:
|
||||
if i.enable == 0:
|
||||
continue
|
||||
for p in i.power:
|
||||
if p.enable == 0:
|
||||
continue
|
||||
user_power.append(p.code)
|
||||
session['permissions'] = user_power
|
||||
# # 角色存入session
|
||||
# roles = []
|
||||
# for role in current_user.role.all():
|
||||
# roles.append(role.id)
|
||||
# session['role'] = [roles]
|
||||
|
||||
return success_api(msg="登录成功")
|
||||
login_log(request, uid=user.id, is_access=False)
|
||||
return fail_api(msg="用户名或密码错误")
|
||||
|
||||
|
||||
# 退出登录
|
||||
@bp.post('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
session.pop('permissions')
|
||||
return success_api(msg="注销成功")
|
||||
@@ -0,0 +1,154 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.utils.http import success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Power
|
||||
from applications.schemas import PowerOutSchema2
|
||||
from applications.schemas.admin_power import PowerSchema
|
||||
|
||||
bp = Blueprint('power', __name__, url_prefix='/power')
|
||||
|
||||
|
||||
@bp.get('/')
|
||||
@authorize("system:power:main")
|
||||
def index():
|
||||
return render_template('system/power/main.html')
|
||||
|
||||
|
||||
@bp.post('/data')
|
||||
@authorize("system:power:main")
|
||||
def data():
|
||||
power = Power.query.all()
|
||||
res = {
|
||||
"data": PowerSchema(many=True).dump(power)
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@bp.get('/add')
|
||||
@authorize("system:power:add", log=True)
|
||||
def add():
|
||||
return render_template('system/power/add.html')
|
||||
|
||||
|
||||
@bp.get('/selectParent')
|
||||
@authorize("system:power:main", log=True)
|
||||
def select_parent():
|
||||
power = Power.query.all()
|
||||
res = curd.model_to_dicts(schema=PowerOutSchema2, data=power)
|
||||
res.append({"powerId": 0, "powerName": "顶级权限", "parentId": -1})
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": res
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
# 增加
|
||||
@bp.post('/save')
|
||||
@authorize("system:power:add", log=True)
|
||||
def save():
|
||||
req = request.get_json(force=True)
|
||||
icon = str_escape(req.get("icon"))
|
||||
openType = str_escape(req.get("openType"))
|
||||
parentId = str_escape(req.get("parentId"))
|
||||
powerCode = str_escape(req.get("powerCode"))
|
||||
powerName = str_escape(req.get("powerName"))
|
||||
powerType = str_escape(req.get("powerType"))
|
||||
powerUrl = str_escape(req.get("powerUrl"))
|
||||
sort = str_escape(req.get("sort"))
|
||||
power = Power(
|
||||
icon=icon,
|
||||
open_type=openType,
|
||||
parent_id=parentId,
|
||||
code=powerCode,
|
||||
name=powerName,
|
||||
type=powerType,
|
||||
url=powerUrl,
|
||||
sort=sort,
|
||||
enable=1
|
||||
)
|
||||
r = db.session.add(power)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
# 权限编辑
|
||||
@bp.get('/edit/<int:_id>')
|
||||
@authorize("system:power:edit", log=True)
|
||||
def edit(_id):
|
||||
power = curd.get_one_by_id(Power, _id)
|
||||
icon = str(power.icon).split()
|
||||
if len(icon) == 2:
|
||||
icon = icon[1]
|
||||
else:
|
||||
icon = None
|
||||
return render_template('system/power/edit.html', power=power, icon=icon)
|
||||
|
||||
|
||||
# 权限更新
|
||||
@bp.put('/update')
|
||||
@authorize("system:power:edit", log=True)
|
||||
def update():
|
||||
req_json = request.get_json(force=True)
|
||||
id = request.get_json(force=True).get("powerId")
|
||||
data = {
|
||||
"icon": str_escape(req_json.get("icon")),
|
||||
"open_type": str_escape(req_json.get("openType")),
|
||||
"parent_id": str_escape(req_json.get("parentId")),
|
||||
"code": str_escape(req_json.get("powerCode")),
|
||||
"name": str_escape(req_json.get("powerName")),
|
||||
"type": str_escape(req_json.get("powerType")),
|
||||
"url": str_escape(req_json.get("powerUrl")),
|
||||
"sort": str_escape(req_json.get("sort"))
|
||||
}
|
||||
res = Power.query.filter_by(id=id).update(data)
|
||||
db.session.commit()
|
||||
if not res:
|
||||
return fail_api(msg="更新权限失败")
|
||||
return success_api(msg="更新权限成功")
|
||||
|
||||
|
||||
# 启用权限
|
||||
@bp.put('/enable')
|
||||
@authorize("system:power:edit", log=True)
|
||||
def enable():
|
||||
_id = request.get_json(force=True).get('powerId')
|
||||
if id:
|
||||
res = curd.enable_status(Power, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用权限
|
||||
@bp.put('/disable')
|
||||
@authorize("system:power:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.get_json(force=True).get('powerId')
|
||||
if id:
|
||||
res = curd.disable_status(Power, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 权限删除
|
||||
@bp.delete('/remove/<int:id>')
|
||||
@authorize("system:power:remove", log=True)
|
||||
def remove(id):
|
||||
power = Power.query.filter_by(id=id).first()
|
||||
power.role = []
|
||||
|
||||
r = Power.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if r:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -0,0 +1,154 @@
|
||||
import copy
|
||||
from collections import OrderedDict
|
||||
|
||||
from flask import jsonify, current_app, Blueprint, render_template
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from ...models import Power
|
||||
from ...schemas import PowerOutSchema
|
||||
|
||||
bp = Blueprint('rights', __name__, url_prefix='/rights')
|
||||
|
||||
|
||||
# 渲染配置
|
||||
@bp.get('/configs')
|
||||
@login_required
|
||||
def configs():
|
||||
# 网站配置
|
||||
config = dict(logo={
|
||||
# 网站名称
|
||||
"title": current_app.config.get("SYSTEM_NAME"),
|
||||
# 网站图标
|
||||
"image": "/static/system/admin/images/logo.png"
|
||||
# 菜单配置
|
||||
}, menu={
|
||||
# 菜单数据来源
|
||||
"data": "/system/rights/menu",
|
||||
"collaspe": False,
|
||||
# 是否同时只打开一个菜单目录
|
||||
"accordion": True,
|
||||
"method": "GET",
|
||||
# 是否开启多系统菜单模式
|
||||
"control": False,
|
||||
# 顶部菜单宽度 PX
|
||||
"controlWidth": 500,
|
||||
# 默认选中的菜单项
|
||||
"select": "0",
|
||||
# 是否开启异步菜单,false 时 data 属性设置为菜单数据,false 时为 json 文件或后端接口
|
||||
"async": True
|
||||
}, tab={
|
||||
# 是否开启多选项卡
|
||||
"enable": True,
|
||||
# 切换选项卡时,是否刷新页面状态
|
||||
"keepState": True,
|
||||
# 是否开启 Tab 记忆
|
||||
"session": True,
|
||||
# 最大可打开的选项卡数量
|
||||
"max": 30,
|
||||
"index": {
|
||||
# 标识 ID , 建议与菜单项中的 ID 一致
|
||||
"id": "10",
|
||||
# 页面地址
|
||||
"href": "/system/rights/welcome",
|
||||
# 标题
|
||||
"title": "首页"
|
||||
}
|
||||
}, theme={
|
||||
# 默认主题色,对应 colors 配置中的 ID 标识
|
||||
"defaultColor": "2",
|
||||
# 默认的菜单主题 dark-theme 黑 / light-theme 白
|
||||
"defaultMenu": "dark-theme",
|
||||
# 是否允许用户切换主题,false 时关闭自定义主题面板
|
||||
"allowCustom": True
|
||||
}, colors=[{
|
||||
"id": "1",
|
||||
"color": "#2d8cf0"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"color": "#5FB878"
|
||||
},
|
||||
{
|
||||
"id": "3",
|
||||
"color": "#1E9FFF"
|
||||
}, {
|
||||
"id": "4",
|
||||
"color": "#FFB800"
|
||||
}, {
|
||||
"id": "5",
|
||||
"color": "darkgray"
|
||||
}
|
||||
], links=current_app.config.get("SYSTEM_PANEL_LINKS"), other={
|
||||
# 主页动画时长
|
||||
"keepLoad": 0,
|
||||
# 布局顶部主题
|
||||
"autoHead": False
|
||||
}, header=False)
|
||||
return jsonify(config)
|
||||
|
||||
|
||||
# 菜单
|
||||
@bp.get('/menu')
|
||||
@login_required
|
||||
def menu():
|
||||
if current_user.username != current_app.config.get("SUPERADMIN"):
|
||||
role = current_user.role
|
||||
powers = []
|
||||
for i in role:
|
||||
# 如果角色没有被启用就直接跳过
|
||||
if i.enable == 0:
|
||||
continue
|
||||
# 变量角色用户的权限
|
||||
for p in i.power:
|
||||
# 如果权限关闭了就直接跳过
|
||||
if p.enable == 0:
|
||||
continue
|
||||
# 一二级菜单
|
||||
if int(p.type) in [0, 1] and p not in powers:
|
||||
powers.append(p)
|
||||
|
||||
power_schema = PowerOutSchema(many=True) # 用已继承 ma.ModelSchema 类的自定制类生成序列化类
|
||||
power_dict = power_schema.dump(powers) # 生成可序列化对象
|
||||
power_dict.sort(key=lambda x: (x['parent_id'], x['id']), reverse=True)
|
||||
|
||||
menu_dict = OrderedDict()
|
||||
for _dict in power_dict:
|
||||
if _dict['id'] in menu_dict:
|
||||
# 当前节点添加子节点
|
||||
_dict['children'] = copy.deepcopy(menu_dict[_dict['id']])
|
||||
_dict['children'].sort(key=lambda item: item['sort'])
|
||||
# 删除子节点
|
||||
del menu_dict[_dict['id']]
|
||||
|
||||
if _dict['parent_id'] not in menu_dict:
|
||||
|
||||
menu_dict[_dict['parent_id']] = [_dict]
|
||||
else:
|
||||
menu_dict[_dict['parent_id']].append(_dict)
|
||||
return jsonify(sorted(menu_dict.get(0), key=lambda item: item['sort']))
|
||||
else:
|
||||
powers = Power.query.all()
|
||||
power_schema = PowerOutSchema(many=True) # 用已继承 ma.ModelSchema 类的自定制类生成序列化类
|
||||
power_dict = power_schema.dump(powers) # 生成可序列化对象
|
||||
power_dict.sort(key=lambda x: (x['parent_id'], x['id']), reverse=True)
|
||||
|
||||
menu_dict = OrderedDict()
|
||||
for _dict in power_dict:
|
||||
if _dict['id'] in menu_dict:
|
||||
# 当前节点添加子节点
|
||||
_dict['children'] = copy.deepcopy(menu_dict[_dict['id']])
|
||||
_dict['children'].sort(key=lambda item: item['sort'])
|
||||
# 删除子节点
|
||||
del menu_dict[_dict['id']]
|
||||
|
||||
if _dict['parent_id'] not in menu_dict:
|
||||
menu_dict[_dict['parent_id']] = [_dict]
|
||||
else:
|
||||
menu_dict[_dict['parent_id']].append(_dict)
|
||||
return jsonify(sorted(menu_dict.get(0), key=lambda item: item['sort']))
|
||||
|
||||
# 控制台页面
|
||||
@bp.get('/welcome')
|
||||
@login_required
|
||||
def welcome():
|
||||
return render_template('system/console/console.html')
|
||||
@@ -0,0 +1,180 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
from flask_login import login_required
|
||||
|
||||
from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id
|
||||
from applications.common.utils.http import table_api, success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Role, Power, User
|
||||
from applications.schemas import RoleOutSchema, PowerOutSchema2
|
||||
|
||||
bp = Blueprint('role', __name__, url_prefix='/role')
|
||||
|
||||
# 用户管理
|
||||
@bp.get('/')
|
||||
@authorize("system:role:main")
|
||||
def main():
|
||||
return render_template('system/role/main.html')
|
||||
|
||||
|
||||
# 表格数据
|
||||
@bp.get('/data')
|
||||
@authorize("system:role:main")
|
||||
def table():
|
||||
role_name = str_escape(request.args.get('roleName', type=str))
|
||||
role_code = str_escape(request.args.get('roleCode', type=str))
|
||||
filters = []
|
||||
if role_name:
|
||||
filters.append(Role.name.contains(role_name))
|
||||
if role_code:
|
||||
filters.append(Role.code.contains(role_code))
|
||||
roles = Role.query.filter(*filters).layui_paginate()
|
||||
return table_api(data=RoleOutSchema(many=True).dump(roles), count=roles.total)
|
||||
|
||||
|
||||
# 角色增加
|
||||
@bp.get('/add')
|
||||
@authorize("system:role:add", log=True)
|
||||
def add():
|
||||
return render_template('system/role/add.html')
|
||||
|
||||
|
||||
# 角色增加
|
||||
@bp.post('/save')
|
||||
@authorize("system:role:add", log=True)
|
||||
def save():
|
||||
req = request.get_json(force=True)
|
||||
details = str_escape(req.get("details"))
|
||||
enable = str_escape(req.get("enable"))
|
||||
roleCode = str_escape(req.get("roleCode"))
|
||||
roleName = str_escape(req.get("roleName"))
|
||||
sort = str_escape(req.get("sort"))
|
||||
role = Role(
|
||||
details=details,
|
||||
enable=enable,
|
||||
code=roleCode,
|
||||
name=roleName,
|
||||
sort=sort
|
||||
)
|
||||
db.session.add(role)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
# 角色授权
|
||||
@bp.get('/power/<int:_id>')
|
||||
@authorize("system:role:power", log=True)
|
||||
def power(_id):
|
||||
return render_template('system/role/power.html', id=_id)
|
||||
|
||||
|
||||
# 获取角色权限
|
||||
@bp.get('/getRolePower/<int:id>')
|
||||
@authorize("system:role:main", log=True)
|
||||
def get_role_power(id):
|
||||
role = Role.query.filter_by(id=id).first()
|
||||
check_powers = role.power
|
||||
check_powers_list = []
|
||||
for cp in check_powers:
|
||||
check_powers_list.append(cp.id)
|
||||
powers = Power.query.all()
|
||||
power_schema = PowerOutSchema2(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = power_schema.dump(powers) # 生成可序列化对象
|
||||
for i in output:
|
||||
if int(i.get("powerId")) in check_powers_list:
|
||||
i["checkArr"] = "1"
|
||||
else:
|
||||
i["checkArr"] = "0"
|
||||
res = {
|
||||
"data": output,
|
||||
"status": {"code": 200, "message": "默认"}
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
# 保存角色权限
|
||||
@bp.put('/saveRolePower')
|
||||
@authorize("system:role:edit", log=True)
|
||||
def save_role_power():
|
||||
req_form = request.form
|
||||
power_ids = req_form.get("powerIds")
|
||||
power_list = power_ids.split(',')
|
||||
role_id = req_form.get("roleId")
|
||||
role = Role.query.filter_by(id=role_id).first()
|
||||
|
||||
powers = Power.query.filter(Power.id.in_(power_list)).all()
|
||||
role.power = powers
|
||||
|
||||
db.session.commit()
|
||||
return success_api(msg="授权成功")
|
||||
|
||||
|
||||
# 角色编辑
|
||||
@bp.get('/edit/<int:id>')
|
||||
@authorize("system:role:edit", log=True)
|
||||
def edit(id):
|
||||
r = get_one_by_id(model=Role, id=id)
|
||||
return render_template('system/role/edit.html', role=r)
|
||||
|
||||
|
||||
# 更新角色
|
||||
@bp.put('/update')
|
||||
@authorize("system:role:edit", log=True)
|
||||
def update():
|
||||
req_json = request.get_json(force=True)
|
||||
id = req_json.get("roleId")
|
||||
data = {
|
||||
"code": str_escape(req_json.get("roleCode")),
|
||||
"name": str_escape(req_json.get("roleName")),
|
||||
"sort": str_escape(req_json.get("sort")),
|
||||
"enable": str_escape(req_json.get("enable")),
|
||||
"details": str_escape(req_json.get("details"))
|
||||
}
|
||||
role = Role.query.filter_by(id=id).update(data)
|
||||
db.session.commit()
|
||||
if not role:
|
||||
return fail_api(msg="更新角色失败")
|
||||
return success_api(msg="更新角色成功")
|
||||
|
||||
|
||||
# 启用用户
|
||||
@bp.put('/enable')
|
||||
@authorize("system:role:edit", log=True)
|
||||
def enable():
|
||||
id = request.get_json(force=True).get('roleId')
|
||||
if id:
|
||||
res = enable_status(Role, id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用用户
|
||||
@bp.put('/disable')
|
||||
@authorize("system:role:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.get_json(force=True).get('roleId')
|
||||
if _id:
|
||||
res = disable_status(Role, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 角色删除
|
||||
@bp.delete('/remove/<int:id>')
|
||||
@authorize("system:role:remove", log=True)
|
||||
def remove(id):
|
||||
role = Role.query.filter_by(id=id).first()
|
||||
# 删除该角色的权限和用户
|
||||
role.power = []
|
||||
role.user = []
|
||||
|
||||
r = Role.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="角色删除失败")
|
||||
return success_api(msg="角色删除成功")
|
||||
@@ -0,0 +1,233 @@
|
||||
from flask import Blueprint, render_template, request
|
||||
from flask_login import login_required, current_user
|
||||
from sqlalchemy import desc
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.curd import enable_status, disable_status
|
||||
from applications.common.utils.http import table_api, fail_api, success_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import str_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Role, Dept
|
||||
from applications.models import User, AdminLog
|
||||
|
||||
bp = Blueprint('user', __name__, url_prefix='/user')
|
||||
|
||||
|
||||
# 用户管理
|
||||
@bp.get('/')
|
||||
@authorize("system:user:main")
|
||||
def main():
|
||||
return render_template('system/user/main.html')
|
||||
|
||||
|
||||
# 用户分页查询
|
||||
@bp.get('/data')
|
||||
@authorize("system:user:main")
|
||||
def data():
|
||||
# 获取请求参数
|
||||
real_name = str_escape(request.args.get('realname', type=str))
|
||||
|
||||
username = str_escape(request.args.get('username', type=str))
|
||||
dept_id = request.args.get('deptId', type=int)
|
||||
|
||||
filters = []
|
||||
if real_name:
|
||||
filters.append(User.realname.contains(real_name))
|
||||
if username:
|
||||
filters.append(User.username.contains(username))
|
||||
if dept_id:
|
||||
filters.append(User.dept_id == dept_id)
|
||||
|
||||
# print(*filters)
|
||||
query = db.session.query(
|
||||
User,
|
||||
Dept
|
||||
).filter(*filters).outerjoin(Dept, User.dept_id == Dept.id).layui_paginate()
|
||||
|
||||
return table_api(
|
||||
data=[{
|
||||
'id': user.id,
|
||||
'username': user.username,
|
||||
'realname': user.realname,
|
||||
'enable': user.enable,
|
||||
'create_at': user.create_at,
|
||||
'update_at': user.update_at,
|
||||
'dept_name': dept.dept_name if dept else None
|
||||
} for user, dept in query.items],
|
||||
count=query.total)
|
||||
|
||||
# 用户增加
|
||||
|
||||
|
||||
@bp.get('/add')
|
||||
@authorize("system:user:add", log=True)
|
||||
def add():
|
||||
roles = Role.query.all()
|
||||
return render_template('system/user/add.html', roles=roles)
|
||||
|
||||
|
||||
@bp.post('/save')
|
||||
@authorize("system:user:add", log=True)
|
||||
def save():
|
||||
req_json = request.get_json(force=True)
|
||||
a = req_json.get("roleIds")
|
||||
username = str_escape(req_json.get('username'))
|
||||
real_name = str_escape(req_json.get('realName'))
|
||||
password = str_escape(req_json.get('password'))
|
||||
role_ids = a.split(',')
|
||||
|
||||
if not username or not real_name or not password:
|
||||
return fail_api(msg="账号姓名密码不得为空")
|
||||
|
||||
if bool(User.query.filter_by(username=username).count()):
|
||||
return fail_api(msg="用户已经存在")
|
||||
user = User(username=username, realname=real_name,enable=1)
|
||||
user.set_password(password)
|
||||
db.session.add(user)
|
||||
roles = Role.query.filter(Role.id.in_(role_ids)).all()
|
||||
for r in roles:
|
||||
user.role.append(r)
|
||||
db.session.commit()
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
|
||||
# 删除用户
|
||||
@bp.delete('/remove/<int:id>')
|
||||
@authorize("system:user:remove", log=True)
|
||||
def delete(id):
|
||||
user = User.query.filter_by(id=id).first()
|
||||
user.role = []
|
||||
|
||||
res = User.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
# 编辑用户
|
||||
@bp.get('/edit/<int:id>')
|
||||
@authorize("system:user:edit", log=True)
|
||||
def edit(id):
|
||||
user = curd.get_one_by_id(User, id)
|
||||
roles = Role.query.all()
|
||||
checked_roles = []
|
||||
for r in user.role:
|
||||
checked_roles.append(r.id)
|
||||
return render_template('system/user/edit.html', user=user, roles=roles, checked_roles=checked_roles)
|
||||
|
||||
|
||||
# 编辑用户
|
||||
@bp.put('/update')
|
||||
@authorize("system:user:edit", log=True)
|
||||
def update():
|
||||
req_json = request.get_json(force=True)
|
||||
a = str_escape(req_json.get("roleIds"))
|
||||
id = str_escape(req_json.get("userId"))
|
||||
username = str_escape(req_json.get('username'))
|
||||
real_name = str_escape(req_json.get('realName'))
|
||||
dept_id = str_escape(req_json.get('deptId'))
|
||||
role_ids = a.split(',')
|
||||
User.query.filter_by(id=id).update({'username': username, 'realname': real_name, 'dept_id': dept_id})
|
||||
u = User.query.filter_by(id=id).first()
|
||||
|
||||
roles = Role.query.filter(Role.id.in_(role_ids)).all()
|
||||
u.role = roles
|
||||
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 个人中心
|
||||
@bp.get('/center')
|
||||
@login_required
|
||||
def center():
|
||||
user_info = current_user
|
||||
user_logs = AdminLog.query.filter_by(url='/passport/login').filter_by(uid=current_user.id).order_by(
|
||||
desc(AdminLog.create_time)).limit(10)
|
||||
return render_template('system/user/center.html', user_info=user_info, user_logs=user_logs)
|
||||
|
||||
|
||||
# 修改头像
|
||||
@bp.get('/profile')
|
||||
@login_required
|
||||
def profile():
|
||||
return render_template('system/user/profile.html')
|
||||
|
||||
|
||||
# 修改头像
|
||||
@bp.put('/updateAvatar')
|
||||
@login_required
|
||||
def update_avatar():
|
||||
url = request.get_json(force=True).get("avatar").get("src")
|
||||
r = User.query.filter_by(id=current_user.id).update({"avatar": url})
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="修改成功")
|
||||
|
||||
|
||||
# 修改当前用户信息
|
||||
@bp.put('/updateInfo')
|
||||
@login_required
|
||||
def update_info():
|
||||
req_json = request.get_json(force=True)
|
||||
r = User.query.filter_by(id=current_user.id).update(
|
||||
{"realname": req_json.get("realName"), "remark": req_json.get("details")})
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 修改当前用户密码
|
||||
@bp.get('/editPassword')
|
||||
@login_required
|
||||
def edit_password():
|
||||
return render_template('system/user/edit_password.html')
|
||||
|
||||
|
||||
# 修改当前用户密码
|
||||
@bp.put('/editPassword')
|
||||
@login_required
|
||||
def edit_password_put():
|
||||
res_json = request.get_json(force=True)
|
||||
if res_json.get("newPassword") == '':
|
||||
return fail_api("新密码不得为空")
|
||||
if res_json.get("newPassword") != res_json.get("confirmPassword"):
|
||||
return fail_api("俩次密码不一样")
|
||||
user = current_user
|
||||
is_right = user.validate_password(res_json.get("oldPassword"))
|
||||
if not is_right:
|
||||
return fail_api("旧密码错误")
|
||||
user.set_password(res_json.get("newPassword"))
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return success_api("更改成功")
|
||||
|
||||
|
||||
# 启用用户
|
||||
@bp.put('/enable')
|
||||
@authorize("system:user:edit", log=True)
|
||||
def enable():
|
||||
_id = request.get_json(force=True).get('userId')
|
||||
if _id:
|
||||
res = enable_status(model=User, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用用户
|
||||
@bp.put('/disable')
|
||||
@authorize("system:user:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.get_json(force=True).get('userId')
|
||||
if _id:
|
||||
res = disable_status(model=User, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
Reference in New Issue
Block a user