From 550f99e46881fffa149a525fff29a362f53a8782 Mon Sep 17 00:00:00 2001 From: zhengxinonly Date: Sun, 12 Sep 2021 18:16:02 +0800 Subject: [PATCH] =?UTF-8?q?'wip(semantic):=E8=B0=83=E6=95=B4=E6=A8=A1?= =?UTF-8?q?=E6=9D=BFapi=E6=8E=A5=E5=8F=A3'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/api/rights/department.py | 16 ++-- applications/api/rights/rights.py | 20 ++--- applications/api/rights/roles.py | 46 +++++------ applications/api/system/file.py | 4 +- applications/api/system/passport.py | 11 ++- applications/api/users/profile.py | 20 ++--- applications/api/users/user.py | 25 +++--- applications/common/utils/http.py | 48 ++++++------ applications/view/logs_view.py | 8 +- applications/view/passport.py | 2 +- static/api/http.js | 31 +++++--- templates/admin/department/dept.html | 61 +++++++-------- templates/admin/department/dept_add.html | 4 +- templates/admin/department/dept_edit.html | 4 +- templates/admin/file/photo.html | 7 +- templates/admin/file/photo_add.html | 4 +- templates/admin/logs_temp/main.html | 12 ++- templates/admin/rights/rights.html | 94 +++++++++++------------ templates/admin/rights/rights_add.html | 4 +- templates/admin/rights/rights_edit.html | 4 +- templates/admin/roles/roles.html | 19 ++--- templates/admin/roles/roles_add.html | 6 +- templates/admin/roles/roles_edit.html | 4 +- templates/admin/roles/roles_power.html | 4 +- templates/admin/users/profile.html | 4 +- templates/admin/users/users.html | 19 ++--- templates/admin/users/users_add.html | 6 +- templates/admin/users/users_edit.html | 6 +- templates/index/admin_index.html | 2 +- templates/index/login.html | 4 +- 30 files changed, 259 insertions(+), 240 deletions(-) diff --git a/applications/api/rights/department.py b/applications/api/rights/department.py index 59793c7..d287d4d 100644 --- a/applications/api/rights/department.py +++ b/applications/api/rights/department.py @@ -49,7 +49,7 @@ class Departments(Resource): db.session.add(dept) db.session.commit() - return success_api(msg="成功") + return success_api(message="成功") class Department(Resource): @@ -67,7 +67,7 @@ class Department(Resource): 'address': dept.address, } # return make_response(render_template('department/edit.html', dept=dept)) - return jsonify(success=True, msg='ok', dept=dept_data) + return dict(success=True, message='ok', dept=dept_data) @authorize("admin:dept:edit", log=True) def put(self, dept_id): @@ -92,9 +92,9 @@ class Department(Resource): } res = CompanyDepartment.query.filter_by(id=dept_id).update(data) if not res: - return fail_api(msg="更新失败") + return fail_api(message="更新失败") db.session.commit() - return success_api(msg="更新成功") + return success_api(message="更新成功") @authorize("admin:dept:remove", log=True) def delete(self, dept_id): @@ -102,8 +102,8 @@ class Department(Resource): CompanyUser.query.filter_by(dept_id=dept_id).update({"dept_id": None}) db.session.commit() if ret: - return success_api(msg="删除成功") - return fail_api(msg="删除失败") + return success_api(message="删除成功") + return fail_api(message="删除失败") class DeptEnable(Resource): @@ -114,5 +114,5 @@ class DeptEnable(Resource): d.status = not d.status db.session.commit() message = '修改成功' - return success_api(msg=message) - return fail_api(msg="出错啦") + return success_api(message=message) + return fail_api(message="出错啦") diff --git a/applications/api/rights/rights.py b/applications/api/rights/rights.py index 160e0d7..dc3d9e2 100644 --- a/applications/api/rights/rights.py +++ b/applications/api/rights/rights.py @@ -166,13 +166,13 @@ class RightRights(Resource): "data": power_data } - return jsonify(res) + return res @authorize("admin:power:remove", log=True) def delete(self): ids = request.form.getlist('ids[]') batch_remove_power(ids) - return success_api(msg="批量删除成功") + return success_api(message="批量删除成功") class RightPower(Resource): @@ -196,9 +196,9 @@ class RightPower(Resource): db.session.commit() except Exception as e: print(e) - return fail_api(msg='数据提交失败') + return fail_api(message='数据提交失败') - return success_api(msg="成功") + return success_api(message="成功") @authorize("admin:power:remove", log=True) def delete(self, power_id): @@ -215,9 +215,9 @@ class RightPower(Resource): db.session.commit() if r: - return success_api(msg="删除成功") + return success_api(message="删除成功") else: - return fail_api(msg="删除失败") + return fail_api(message="删除失败") @authorize("admin:power:edit", log=True) def put(self, power_id): @@ -237,8 +237,8 @@ class RightPower(Resource): db.session.commit() if not power: - return fail_api(msg="更新权限失败") - return success_api(msg="更新权限成功") + return fail_api(message="更新权限失败") + return success_api(message="更新权限成功") class RightPowerEnable(Resource): @@ -250,9 +250,9 @@ class RightPowerEnable(Resource): power.enable = not power.enable db.session.commit() message = "修改成功" - return success_api(msg=message) + return success_api(message=message) else: - return fail_api(msg="出错啦") + return fail_api(message="出错啦") class AdminConfigs(Resource): diff --git a/applications/api/rights/roles.py b/applications/api/rights/roles.py index 9d65903..25bf9a7 100644 --- a/applications/api/rights/roles.py +++ b/applications/api/rights/roles.py @@ -57,18 +57,16 @@ class RoleRoles(Resource): paginate = RightsRole.query.filter(*filters).paginate(page=res.page, per_page=res.limit, error_out=False) - return table_api(data=[ - { - 'id': item.id, - 'roleName': item.name, - 'roleCode': item.code, - 'enable': item.enable, - 'remark': item.remark, - 'details': item.details, - 'sort': item.sort, - 'create_at': item.create_at, - } for item in paginate.items - ], count=paginate.total, code=0) + return table_api(result={'items': [{'id': item.id, + 'roleName': item.name, + 'roleCode': item.code, + 'enable': item.enable, + 'remark': item.remark, + 'details': item.details, + 'sort': item.sort, + 'create_at': str(item.create_at), } for item in paginate.items], + 'total': paginate.total} + , code=0) @authorize("admin:role:remove", log=True) @login_required @@ -79,7 +77,7 @@ class RoleRoles(Resource): res = parser.parse_args() batch_remove_role(res.ids) - return success_api(msg="批量删除成功") + return success_api(message="批量删除成功") class RoleRole(Resource): @@ -104,7 +102,7 @@ class RoleRole(Resource): ) db.session.add(role) db.session.commit() - return success_api(msg="成功") + return success_api(message="成功") # 更新角色 @authorize("admin:role:edit", log=True) @@ -130,8 +128,8 @@ class RoleRole(Resource): role = RightsRole.query.filter_by(id=role_id).update(data) db.session.commit() if not role: - return fail_api(msg="更新角色失败") - return success_api(msg="更新角色成功") + return fail_api(message="更新角色失败") + return success_api(message="更新角色成功") class RoleEnable(Resource): @@ -145,8 +143,8 @@ class RoleEnable(Resource): message = "修改成功" if not ret: - return fail_api(msg="出错啦") - return success_api(msg=message) + return fail_api(message="出错啦") + return success_api(message=message) class RolePower(Resource): @@ -158,20 +156,16 @@ class RolePower(Resource): # 获取权限列表的 id check_powers_list = [rp.id for rp in role.power] powers = RightsPower.query.all() # 获取所有的权限 - # power_schema = PowerSchema2(many=True) # 用已继承 ma.ModelSchema 类的自定制类生成序列化类 - # 将所有的权限生产可序列化对象 json - # powers = power_schema.dump(powers) # 生成可序列化对象 powers = marshal(powers, power_fields) for i in powers: if int(i.get("powerId")) in check_powers_list: i["checkArr"] = "1" else: i["checkArr"] = "0" - res = { + return { "data": powers, "status": {"code": 200, "message": "默认"} } - return jsonify(res) # 保存角色权限 @authorize("admin:role:edit", log=True) @@ -195,7 +189,7 @@ class RolePower(Resource): for p in powers: role.power.append(p) db.session.commit() - return success_api(msg="授权成功") + return success_api(message="授权成功") # 角色删除 @authorize("admin:role:remove", log=True) @@ -204,5 +198,5 @@ class RolePower(Resource): res = remove_role(role_id) print(res) if not res: - return fail_api(msg="角色删除失败") - return success_api(msg="角色删除成功") + return fail_api(message="角色删除失败") + return success_api(message="角色删除成功") diff --git a/applications/api/system/file.py b/applications/api/system/file.py index 47176ff..a7109cd 100644 --- a/applications/api/system/file.py +++ b/applications/api/system/file.py @@ -21,7 +21,9 @@ class FilePhotos(Resource): photo_paginate = FilePhoto.query.order_by(desc(FilePhoto.create_at)).paginate(page=page, per_page=limit, error_out=False) data = marshal(photo_paginate.items, photo_fields) - return table_api(data=data, count=photo_paginate.total, code=0) + return table_api(result={'items': data, + 'total': photo_paginate.total, }, + code=0) @authorize("admin:file:add", log=True) def post(self): diff --git a/applications/api/system/passport.py b/applications/api/system/passport.py index 46d0de1..7375be8 100644 --- a/applications/api/system/passport.py +++ b/applications/api/system/passport.py @@ -18,7 +18,6 @@ class Login(Resource): def get(self): if current_user.is_authenticated: return redirect(url_for('admin.index')) - # TODO 分离视图操作 最终实现接口登录与视图登录两套逻辑 return make_response(render_template('index/login.html')) def post(self): @@ -28,14 +27,14 @@ class Login(Resource): s_code = session.get("code", None) if req.captcha != s_code: - return fail_api(msg="验证码错误") + return fail_api(message="验证码错误") user = CompanyUser.query.filter_by(username=req.username).first() if user is None: - return fail_api(msg="不存在的用户") + return fail_api(message="不存在的用户") if user.enable == 0: - return fail_api(msg="用户被暂停使用") + return fail_api(message="用户被暂停使用") if user.validate_password(req.password): # 登录 @@ -45,6 +44,6 @@ class Login(Resource): # 存入权限 add_auth_session() - return success_api(msg="登录成功") + return success_api(message="登录成功") record_logging() - return fail_api(msg="用户名或密码错误") + return fail_api(message="用户名或密码错误") diff --git a/applications/api/users/profile.py b/applications/api/users/profile.py index 2836a59..e1ed843 100644 --- a/applications/api/users/profile.py +++ b/applications/api/users/profile.py @@ -21,14 +21,14 @@ class UserStatus(Resource): if res.operate == 1: user = CompanyUser.query.get(user_id) user.enable = res.operate - message = success_api(msg="启动成功") + message = success_api(message="启动成功") else: user = CompanyUser.query.filter_by(id=res.user_id).update({"enable": res.operate}) - message = success_api(msg="禁用成功") + message = success_api(message="禁用成功") if user: db.session.commit() else: - return fail_api(msg="出错啦") + return fail_api(message="出错啦") return message @@ -41,8 +41,8 @@ class UserAvatar(Resource): ret.avatar = url db.session.commit() if not ret: - return fail_api(msg="出错啦") - return success_api(msg="修改成功") + return fail_api(message="出错啦") + return success_api(message="修改成功") class UserInfo(Resource): @@ -63,8 +63,8 @@ class UserInfo(Resource): ret.remark = res.details db.session.commit() if not ret: - return fail_api(msg="出错啦") - return success_api(msg="更新成功") + return fail_api(message="出错啦") + return success_api(message="更新成功") class UserPassword(Resource): @@ -80,15 +80,15 @@ class UserPassword(Resource): res = parser.parse_args() if res.newPassword != res.confirmPassword: - return fail_api(msg='确认密码不一致') + return fail_api(message='确认密码不一致') """ 修改当前用户密码 """ user = CompanyUser.query.get(user_id) is_right = user.validate_password(res.oldPassword) if not is_right: - return jsonify(success=False, msg="旧密码错误") + return jsonify(success=False, message="旧密码错误") user.set_password(res.newPassword) db.session.add(user) db.session.commit() - return jsonify(success=True, msg="更改成功") + return jsonify(success=True, message="更改成功") diff --git a/applications/api/users/user.py b/applications/api/users/user.py index 1015f26..dfeb46c 100644 --- a/applications/api/users/user.py +++ b/applications/api/users/user.py @@ -90,12 +90,13 @@ class UserUsers(Resource): 'username': item.username, 'realname': item.realname, 'enable': item.enable, - 'create_at': item.create_at, - 'update_at': item.update_at, + 'create_at': str(item.create_at), + 'update_at': str(item.update_at), 'dept': dept_name(item.dept_id), } for item in paginate.items] - - return table_api(data=user_data, count=paginate.total, code=0) + return table_api(result={'items': user_data, + 'total': paginate.total} + , code=0) @authorize("admin:user:add", log=True) def post(self): @@ -111,7 +112,7 @@ class UserUsers(Resource): role_ids = res.role_ids.split(',') if is_user_exists(res.username): - return fail_api(msg="用户已经存在") + return fail_api(message="用户已经存在") user = CompanyUser() user.username = res.username @@ -127,14 +128,14 @@ class UserUsers(Resource): user.role.append(r) db.session.commit() - return success_api(msg="增加成功", code=0) + return success_api(message="增加成功", code=0) @authorize("admin:user:remove", log=True) def delete(self): """批量删除""" ids = request.form.getlist('ids[]') batch_remove(ids) - return success_api(msg="批量删除成功") + return success_api(message="批量删除成功") class UserUser(Resource): @@ -154,7 +155,7 @@ class UserUser(Resource): role_ids = res.role_ids.split(',') if is_user_exists(res.username): - return fail_api(msg="用户已经存在") + return fail_api(message="用户已经存在") user = CompanyUser() user.username = res.username @@ -170,15 +171,15 @@ class UserUser(Resource): user.role.append(r) db.session.commit() - return success_api(msg="增加成功", code=0) + return success_api(message="增加成功", code=0) @authorize("admin:user:remove", log=True) def delete(self, user_id): # 删除用户 res = delete_by_id(user_id) if not res: - return fail_api(msg="删除失败") - return success_api(msg="删除成功") + return fail_api(message="删除失败") + return success_api(message="删除成功") class UserRole(Resource): @@ -202,4 +203,4 @@ class UserRole(Resource): update_user_role(user_id, role_ids) - return success_api(msg="更新成功") + return success_api(message="更新成功") diff --git a/applications/common/utils/http.py b/applications/common/utils/http.py index 65b9d04..7f8180d 100644 --- a/applications/common/utils/http.py +++ b/applications/common/utils/http.py @@ -1,32 +1,36 @@ -from flask import jsonify - -""" -{ - 'success': True, - 'code': 10002, - 'msg': '提示消息', - 'data':[] or {} -} -""" +import typing as t -def success_api(msg: str = "成功", code=200): +def success_api(message: str = "成功", code: int = 200) -> t.Dict: """ 成功响应 默认值”成功“ """ - return jsonify(success=True, msg=msg, code=200) + return dict(success=True, message=message, code=code) -def fail_api(msg: str = "失败", code=404): +def fail_api(message: str = "失败", code: int = 404) -> t.Dict: """ 失败响应 默认值“失败” """ - return jsonify(success=False, msg=msg, code=404) + return dict(success=False, message=message, code=code) -def table_api(msg: str = "", count=0, data=None, limit=10, code=200): - """ 动态表格渲染响应 """ - res = { - 'msg': msg, +def table_api(success: bool = True, + message: str = "", + result: t.Union[dict, list] = None, + code: int = 0) -> t.Dict: + """ + 动态表格渲染响应 + 此方法返回数据给前端 + { + 'success': True, + 'code': 10002, + 'message': '提示消息', + 'result':{'items':[],'total': 100} + } + + 注:lay_ui 表格数据需要指定 code=0 + """ + ret = { + 'success': success, + 'message': message, 'code': code, - 'data': data, - 'count': count, - 'limit': limit + 'result': result, } - return jsonify(res) + return ret diff --git a/applications/view/logs_view.py b/applications/view/logs_view.py index c9ef724..cc7c68e 100644 --- a/applications/view/logs_view.py +++ b/applications/view/logs_view.py @@ -27,7 +27,9 @@ def login_log(): page=page, per_page=limit, error_out=False) data = marshal(log_paginate.items, log_fields) - return table_api(data=data, count=log_paginate.total, code=0) + return table_api(result={'items': data, + 'total': log_paginate.total, }, + code=0) @logs_bp.get('/access_log') @@ -40,4 +42,6 @@ def operate_log(): desc(LoggingModel.create_at)).paginate( page=page, per_page=limit, error_out=False) data = marshal(log_paginate.items, log_fields) - return table_api(data=data, count=log_paginate.total, code=0) + return table_api(result={'items': data, + 'total': log_paginate.total, }, + code=0) diff --git a/applications/view/passport.py b/applications/view/passport.py index 09023fb..0e4657a 100644 --- a/applications/view/passport.py +++ b/applications/view/passport.py @@ -21,7 +21,7 @@ def get_captcha(): def logout(): logout_user() session.pop('permissions') - return success_api(msg="注销成功") + return success_api(message="注销成功") @index_bp.get('/passport/login') diff --git a/static/api/http.js b/static/api/http.js index ff514d0..e0aad0c 100644 --- a/static/api/http.js +++ b/static/api/http.js @@ -14,7 +14,7 @@ class Department { this.DEPARTMENT_API = { 'DEPARTMENTS': () => `${this.DEPARTMENT_PATH}/departments`, 'DEPARTMENT': (dept_id) => `${this.DEPARTMENT_PATH}/department/${dept_id}`, - 'DEPARTMENT_STATUS': (dept_id) => `${this.DEPARTMENT_PATH}/department/${dept_id}/status` + 'DEPARTMENT_STATUS': (dept_id) => `${this.DEPARTMENT_PATH}/department/${dept_id}/status`, } } } @@ -25,7 +25,7 @@ class Rights { this.RIGHTS_API = { 'RIGHTS': () => `${this.DEPARTMENT_PATH}/rights`, 'POWER': (power_id) => `${this.DEPARTMENT_PATH}/power/${power_id}`, - 'POWER_STATUS': (power_id) => `${this.DEPARTMENT_PATH}/power/${power_id}/status` + 'POWER_STATUS': (power_id) => `${this.DEPARTMENT_PATH}/power/${power_id}/status`, } } } @@ -37,18 +37,16 @@ class Role { 'ROLES': () => `${this.ROLE_PATH}/roles`, 'ROLE': (role_id) => `${this.ROLE_PATH}/role/${role_id}`, 'ROLE_STATUS': (role_id) => `${this.ROLE_PATH}/role/${role_id}/status`, - 'ROLE_POWER': (role_id) => `${this.ROLE_PATH}/role_power/${role_id}` + 'ROLE_POWER': (role_id) => `${this.ROLE_PATH}/role_power/${role_id}`, } } async get_roles () { - return fetch(this.ROLE_API.ROLES()) - .then(function (response) { - return response.json() - }) - .then(function (myJson) { - return myJson - }) + return fetch(this.ROLE_API.ROLES()).then(function (response) { + return response.json() + }).then(function (myJson) { + return myJson + }) } } @@ -83,5 +81,16 @@ export { DEPARTMENT_API, RIGHTS, RIGHTS_API, ROLE, ROLE_API, - USER, USER_API + USER, USER_API, +} + +/******************数据解析********************************/ +export function parserTableData (res) { //res 即为原始返回的数据 + console.log('parserTableData', res) + return { + 'code': res.code, //解析接口状态 + 'msg': res.message, //解析提示文本 + 'count': res.result.total, //解析数据长度 + 'data': res.result.items, //解析数据列表 + } } \ No newline at end of file diff --git a/templates/admin/department/dept.html b/templates/admin/department/dept.html index 1d642a6..d889c3d 100644 --- a/templates/admin/department/dept.html +++ b/templates/admin/department/dept.html @@ -39,15 +39,15 @@ {#表格工具栏,需要在表格中绑定,且需要自定义事件#} - -