统一table,成功,失败返回响应,封装模型序列化函数

This commit is contained in:
mkg
2021-04-29 19:01:18 +08:00
parent 3d5e4b9e9b
commit e111f4a763
15 changed files with 167 additions and 170 deletions
+6 -23
View File
@@ -2,6 +2,8 @@ from flask import Blueprint, request, render_template, jsonify
from flask_login import login_required
from sqlalchemy import desc
from applications.models.admin_log import AdminLog, LogSchema
from applications.service.common.curd import model_to_dicts
from applications.service.common.response import table_api
admin_log = Blueprint('adminLog', __name__, url_prefix='/admin/log')
@@ -31,18 +33,9 @@ def loginLog():
per_page=limit,
error_out=False)
count = AdminLog.query.filter_by(url='/admin/login').count()
role_schema = LogSchema(many=True)
output = role_schema.dump(log.items)
res = {
'msg': "",
'code': 0,
'data': output,
'count': count,
'limit': "10"
data = model_to_dicts(Schema=LogSchema, model=log.items)
}
return jsonify(res)
return table_api(data=data, count=count)
# ==========================================================
@@ -59,15 +52,5 @@ def operateLog():
per_page=limit,
error_out=False)
count = AdminLog.query.filter(AdminLog.url != '/admin/login').count()
role_schema = LogSchema(many=True)
output = role_schema.dump(log.items)
res = {
'msg': "",
'code': 0,
'data': output,
'count': count,
'limit': "10"
}
return jsonify(res)
data = model_to_dicts(Schema=LogSchema,model=log.items)
return table_api(data=data,count=count)
+28 -40
View File
@@ -4,6 +4,7 @@ from applications.models.admin_dict import DictType, DictData
from applications.service.admin.dict import get_dict_type, get_dict_data, save_dict_type, save_dict_data, \
delete_type_by_id, delete_data_by_id, update_dict_type, enable_dict_type_status, disable_dict_type_status, \
enable_dict_data_status, disable_dict_data_status, update_dict_data
from applications.service.common.response import table_api, success_api, fail_api
from applications.service.route_auth import authorize_and_log
admin_dict = Blueprint('adminDict', __name__, url_prefix='/admin/dict')
@@ -22,13 +23,8 @@ def dictType_data():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
type_name = request.args.get('typeName', type=str)
dict_data, count = get_dict_type(page=page, limit=limit, type_name=type_name)
res = {
"data": dict_data,
"count": count,
"code": 0,
}
return jsonify(res)
data, count = get_dict_type(page=page, limit=limit, type_name=type_name)
return table_api(data=data,count=count)
@admin_dict.route('/dictType/add')
@@ -38,13 +34,13 @@ def dictType_add():
@admin_dict.route('/dictType/save', methods=['POST'])
@authorize_and_log("admin:power:add")
@authorize_and_log("admin:dict:add")
def dictType_save():
req_json = request.json
res = save_dict_type(req_json=req_json)
if res == None:
return jsonify(success=False, msg="增加失败")
return jsonify(success=True, msg="增加成功")
return fail_api(msg="增加失败")
return success_api(msg="增加成功")
# 编辑字典类型
@@ -62,7 +58,7 @@ def dictType_edit():
def dictType_update():
req_json = request.json
update_dict_type(req_json)
return jsonify(success=True, msg="更新成功")
return success_api(msg="更新成功")
# 启用字典
@@ -70,13 +66,12 @@ def dictType_update():
@authorize_and_log("admin:dict:edit")
def dictType_enable():
id = request.json.get('id')
print(id)
if id:
res = enable_dict_type_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api("启动成功")
return fail_api(msg="数据错误")
# 禁用字典
@@ -84,13 +79,12 @@ def dictType_enable():
@authorize_and_log("admin:dict:edit")
def dictType_disenable():
id = request.json.get('id')
print(id)
if id:
res = disable_dict_type_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api("禁用成功")
return fail_api(msg="数据错误")
# 删除字典类型
@@ -99,8 +93,8 @@ def dictType_disenable():
def dictType_delete(id):
res = delete_type_by_id(id)
if not res:
return jsonify(msg="删除失败", success=False)
return jsonify(msg="删除成功", success=True)
return fail_api(msg="删除失败")
return success_api(msg="删除成功")
@admin_dict.route('/dictData/data')
@@ -109,18 +103,13 @@ def dictCode_data():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
type_code = request.args.get('typeCode', type=str)
dict_data, count = get_dict_data(page=page, limit=limit, type_code=type_code)
res = {
"data": dict_data,
"count": count,
"code": 0,
}
return jsonify(res)
data, count = get_dict_data(page=page, limit=limit, type_code=type_code)
return table_api(data=data,count=count)
# 增加字典数据
@admin_dict.route('/dictData/add')
# @authorize_and_log("admin:power:add")
@authorize_and_log("admin:dict:add")
def dictData_add():
type_code = request.args.get('typeCode', type=str)
return render_template('admin/dict/data/add.html', type_code=type_code)
@@ -128,7 +117,7 @@ def dictData_add():
# 增加字典数据
@admin_dict.route('/dictData/save', methods=['POST'])
# @authorize_and_log("admin:power:main")
@authorize_and_log("admin:dict:add")
def dictData_save():
req_json = request.json
res = save_dict_data(req_json=req_json)
@@ -152,7 +141,7 @@ def dictData_edit():
def dictData_update():
req_json = request.json
update_dict_data(req_json)
return jsonify(success=True, msg="更新成功")
return success_api(msg="更新成功")
# 启用字典数据
@@ -160,13 +149,12 @@ def dictData_update():
@authorize_and_log("admin:dict:edit")
def dictData_enable():
id = request.json.get('dataId')
print(id)
if id:
res = enable_dict_data_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="启动成功")
return fail_api(msg="数据错误")
# 禁用字典数据
@@ -177,9 +165,9 @@ def dictData_disenable():
if id:
res = disable_dict_data_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="禁用成功")
return fail_api(msg="数据错误")
# 删除字典类型
@@ -188,5 +176,5 @@ def dictData_disenable():
def dictData_delete(id):
res = delete_data_by_id(id)
if not res:
return jsonify(msg="删除失败", success=False)
return jsonify(msg="删除成功", success=True)
return fail_api(msg="删除失败")
return success_api(msg="删除成功")
+6 -15
View File
@@ -1,9 +1,9 @@
import os
from flask import Blueprint, request, render_template, jsonify, current_app
from flask_login import login_required
from applications.models import db
from applications.models.admin_photo import Photo
from applications.service.admin.file import get_photo, upload_one, delete_photo_by_id
from applications.service.common.response import table_api, success_api, fail_api
from applications.service.route_auth import authorize_and_log
admin_file = Blueprint('adminFile', __name__, url_prefix='/admin/file')
@@ -23,15 +23,7 @@ def table():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
data, count = get_photo(page=page, limit=limit)
res = {
'msg': "",
'code': 0,
'data': data,
'count': count,
'limit': "10"
}
return jsonify(res)
return table_api(data=data, count=count)
# 上传接口
@@ -61,9 +53,9 @@ def delete():
id = request.form.get('id')
res = delete_photo_by_id(id)
if res:
return jsonify(msg="删除成功", code=200)
return success_api(msg="删除成功")
else:
return jsonify(msg="删除失败", code=999)
return fail_api(msg="删除失败")
# 图片批量删除
@@ -78,7 +70,6 @@ def batchRemove():
photo = Photo.query.filter(Photo.id.in_(ids)).delete(synchronize_session=False)
db.session.commit()
if photo:
return jsonify(msg="删除成功", code=200)
return success_api(msg="删除成功")
else:
res = {"msg": "删除失败", "code": 999}
return res
return fail_api(msg="删除失败")
+9 -9
View File
@@ -3,6 +3,7 @@ from flask_login import login_user, login_required, logout_user, current_user
from applications.service.admin.index import add_auth_session, make_menu_tree, get_captcha
from applications.service.admin_log import login_log
from applications.models.admin_user import User
from applications.service.common.response import fail_api, success_api
admin_index = Blueprint('adminIndex', __name__, url_prefix='/admin')
@@ -33,21 +34,21 @@ def login():
code = req.get('captcha')
if not username or not password or not code:
return jsonify(msg="用户名或密码没有输入", code=0,success=False)
return fail_api(msg="用户名或密码没有输入")
s_code = session.get("code", None)
if not all([code, s_code]):
return jsonify(msg="参数错误", code=0,success=False)
return fail_api(msg="参数错误")
if code != s_code:
return jsonify(msg="验证码错误", code=0,success=False)
return fail_api(msg="验证码错误")
user = User.query.filter_by(username=username).first()
if user is None:
return jsonify(msg="不存在的用户", code=0,success=False)
return fail_api(msg="不存在的用户")
if user.enable is 0:
return jsonify(msg="用户被暂停使用", code=0,success=False)
return fail_api(msg="用户被暂停使用")
if username == user.username and user.validate_password(password):
# 登录
@@ -56,10 +57,9 @@ def login():
login_log(request, uid=user.id, is_access=True)
# 存入权限
add_auth_session()
return jsonify(msg="登录成功", code=1,success=True)
return success_api(msg="登录成功")
login_log(request,uid=user.id, is_access=False)
return jsonify(msg="用户名或密码错误", code=0,success=False)
print(current_user.is_authenticated)
return fail_api(msg="用户名或密码错误")
if current_user.is_authenticated:
return redirect(url_for('adminIndex.index'))
return render_template('admin/login.html')
@@ -71,7 +71,7 @@ def login():
def logout():
logout_user()
session.pop('permissions')
return jsonify(msg="注销成功", success=True)
return success_api(msg="注销成功")
# 菜单
+13 -13
View File
@@ -1,7 +1,7 @@
from flask import Blueprint, render_template, request, jsonify
from flask_login import login_required
from applications.service.admin.power import get_power_dict, select_parent, save_power, remove_power, get_power_by_id, \
update_power, enable_status, disable_status, batch_remove
from applications.service.common.response import success_api, fail_api
from applications.service.route_auth import authorize_and_log
admin_power = Blueprint('adminPower', __name__, url_prefix='/admin/power')
@@ -47,7 +47,7 @@ def selectParent():
def save():
req = request.json
save_power(req)
return jsonify(msg="成功", success=True)
return success_api(msg="成功")
# 权限编辑
@@ -69,8 +69,8 @@ def edit(id):
def update():
res = update_power(request.json)
if not res:
return jsonify(success=False, msg="更新权限失败")
return jsonify(success=True, msg="更新权限成功")
return fail_api(msg="更新权限失败")
return success_api(msg="更新权限成功")
# 启用用户
@@ -82,9 +82,9 @@ def enable():
if id:
res = enable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="启动成功")
return fail_api(msg="数据错误")
# 禁用用户
@@ -96,9 +96,9 @@ def disenable():
if id:
res = disable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="禁用成功")
return fail_api(msg="数据错误")
# 权限删除
@@ -107,9 +107,9 @@ def disenable():
def remove(id):
r = remove_power(id)
if r:
return jsonify(success=True, msg="删除成功")
return success_api(msg="删除成功")
else:
return jsonify(success=False, msg="删除失败")
return fail_api(msg="删除失败")
# 批量删除
@@ -118,4 +118,4 @@ def remove(id):
def batchRemove():
ids = request.form.getlist('ids[]')
batch_remove(ids)
return jsonify(success=True, msg="批量删除成功")
return success_api(msg="批量删除成功")
+16 -23
View File
@@ -3,6 +3,7 @@ from flask_login import login_required
from applications.service.admin.role import get_role_data_dict, add_role, get_role_power, update_role_power, \
get_role_by_id, update_role, remove_role, batch_remove, enable_status, disable_status
from applications.service.common.response import table_api, success_api, fail_api
from applications.service.route_auth import authorize_and_log
admin_role = Blueprint('adminRole', __name__, url_prefix='/admin/role')
@@ -29,15 +30,7 @@ def table():
if roleCode:
filters["code"] = ('%' + roleCode + '%')
data, count = get_role_data_dict(page=page, limit=limit, filters=filters)
res = {
'msg': "",
'code': 0,
'data': data,
'count': count,
'limit': "10"
}
return jsonify(res)
return table_api(data=data, count=count)
# 角色增加
@@ -54,7 +47,7 @@ def add():
def save():
req = request.json
add_role(req=req)
return jsonify(msg="成功", success=True)
return success_api(msg="成功")
# 角色授权
@@ -85,7 +78,7 @@ def saveRolePower():
power_list = powerIds.split(',')
roleId = req_form.get("roleId")
update_role_power(id=roleId, power_list=power_list)
return jsonify(success=True, msg="授权成功")
return success_api(msg="授权成功")
# 角色编辑
@@ -102,8 +95,9 @@ def edit(id):
def update():
res = update_role(request.json)
if not res:
return jsonify(success=False, msg="更新角色失败")
return jsonify(success=True, msg="更新角色成功")
return fail_api(msg="更新角色失败")
return success_api(msg="更新角色成功")
# 启用用户
@admin_role.route('/enable', methods=['PUT'])
@@ -114,9 +108,9 @@ def enable():
if id:
res = enable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="启动成功")
return fail_api(msg="数据错误")
# 禁用用户
@@ -124,13 +118,12 @@ def enable():
@authorize_and_log("admin:role:edit")
def disenable():
id = request.json.get('roleId')
print(id)
if id:
res = disable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="禁用成功")
return fail_api(msg="数据错误")
# 角色删除
@@ -139,8 +132,8 @@ def disenable():
def remove(id):
res = remove_role(id)
if not res:
return jsonify(success=False, msg="角色删除失败")
return jsonify(success=True, msg="角色删除成功")
return fail_api(msg="角色删除失败")
return success_api(msg="角色删除成功")
# 批量删除
@@ -150,4 +143,4 @@ def remove(id):
def batchRemove():
ids = request.form.getlist('ids[]')
batch_remove(ids)
return jsonify(success=True,msg="批量删除成功")
return success_api(msg="批量删除成功")
+20 -28
View File
@@ -5,6 +5,7 @@ from applications.models.admin_role import Role
from applications.service.admin.user import get_user_data_dict, add_user, delete_by_id, \
batch_remove, update_user, update_user_role, is_user_exists, add_user_role, enable_status, disable_status, \
edit_password, get_current_user_logs, update_current_user_info, update_avatar
from applications.service.common.response import table_api, fail_api, success_api
from applications.service.route_auth import authorize_and_log
admin_user = Blueprint('adminUser', __name__, url_prefix='/admin/user')
@@ -30,16 +31,8 @@ def data():
filters["realname"] = ('%' + realName + '%')
if username:
filters["username"] = ('%' + username + '%')
data, count = get_user_data_dict(page=page, limit=limit, filters=filters)
res = {
'msg': "",
'code': 0,
'data': data,
'count': count,
'limit': "10"
}
return jsonify(res)
user_data, count = get_user_data_dict(page=page, limit=limit, filters=filters)
return table_api(data=user_data, count=count)
# 用户增加
@@ -61,15 +54,15 @@ def save():
role_ids = a.split(',')
if not username or not realName or not password:
return jsonify(success=False, msg="账号姓名密码不得为空")
return fail_api(msg="账号姓名密码不得为空")
if is_user_exists(username):
return jsonify(success=False, msg="用户已经存在")
return fail_api(msg="用户已经存在")
id = add_user(username, realName, password)
add_user_role(id, role_ids)
return jsonify(success=True, msg="增加成功")
return success_api(msg="增加成功")
# 删除用户
@@ -78,8 +71,8 @@ def save():
def delete(id):
res = delete_by_id(id)
if not res:
return jsonify(msg="删除失败", success=False)
return jsonify(msg="删除成功", success=True)
return fail_api(msg="删除失败")
return success_api(msg="删除成功")
# 编辑用户
@@ -106,7 +99,7 @@ def update():
role_ids = a.split(',')
update_user(id, username, realName)
update_user_role(id, role_ids)
return jsonify(success=True, msg="更新成功")
return success_api(msg="更新成功")
# 个人中心
@@ -131,8 +124,8 @@ def profile():
def updateAvatar():
url = request.json.get("avatar").get("src")
if not update_avatar(url):
return jsonify(msg="出错啦", success=False)
return jsonify(msg="修改成功", success=True)
return fail_api(msg="出错啦")
return success_api(msg="修改成功")
# 修改当前用户信息
@@ -141,8 +134,8 @@ def updateAvatar():
def updateInfo():
res_json = request.json
if not update_current_user_info(req_json=res_json):
return jsonify(msg="出错啦", success=False)
return jsonify(msg="更新成功", success=True)
return fail_api(msg="出错啦")
return success_api(msg="更新成功")
# 修改当前用户密码
@@ -169,9 +162,9 @@ def enable():
if id:
res = enable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="启动成功")
return fail_api(msg="数据错误")
# 禁用用户
@@ -179,13 +172,12 @@ def enable():
@authorize_and_log("admin:user:edit")
def disenable():
id = request.json.get('userId')
print(id)
if id:
res = disable_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
return fail_api(msg="出错啦")
return success_api(msg="禁用成功")
return fail_api(msg="数据错误")
# 批量删除
@@ -194,4 +186,4 @@ def disenable():
def batchRemove():
ids = request.form.getlist('ids[]')
batch_remove(ids)
return jsonify(success=True, msg="批量删除成功")
return success_api(msg="批量删除成功")