diff --git a/applications/api/department.py b/applications/api/department.py index 9319478..34119cb 100644 --- a/applications/api/department.py +++ b/applications/api/department.py @@ -11,9 +11,19 @@ from applications.common.serialization import dept_fields dept_api = Api(api_bp, prefix='/dept') -@dept_api.resource('/add') +@dept_api.resource('/departments') class AddDepartment(Resource): + @authorize("admin:dept:main", log=True) + def get(self): + dept_data = Dept.query.order_by(Dept.sort).all() + # TODO dtree 需要返回状态信息 + res = { + "status": {"code": 200, "message": "默认"}, + "data": marshal(dept_data, dept_fields) + } + return jsonify(res) + @authorize("admin:dept:add", log=True) def post(self): parser = reqparse.RequestParser() @@ -44,7 +54,7 @@ class AddDepartment(Resource): return success_api(msg="成功") -@dept_api.resource('/edit/') +@dept_api.resource('/department/') class DeptURD(Resource): @authorize("admin:dept:edit", log=True) def get(self, dept_id): @@ -99,43 +109,14 @@ class DeptURD(Resource): return fail_api(msg="删除失败") -@dept_api.resource('/data') -class DeptData(Resource): - - @authorize("admin:dept:main", log=True) - def get(self): - dept_data = Dept.query.order_by(Dept.sort).all() - res = { - "data": marshal(dept_data, dept_fields) - } - return jsonify(res) - - -@dept_api.resource('/tree') -class DeptTree(Resource): - - @authorize("admin:dept:main", log=True) - def get(self): - dept_data = Dept.query.order_by(Dept.sort).all() - res = { - "status": {"code": 200, "message": "默认"}, - "data": marshal(dept_data, dept_fields) - } - return jsonify(res) - - -@dept_api.resource('/enable') +@dept_api.resource('/department//status') class DeptEnable(Resource): @authorize("admin:dept:edit", log=True) - def put(self): - parser = reqparse.RequestParser() - parser.add_argument('deptId', type=int, dest='dept_id', required=True) - parser.add_argument('operate', type=int, choices=[0, 1], required=True) - - res = parser.parse_args() - d = Dept.query.filter_by(id=res.dept_id).update({"status": res.operate}) + def put(self, dept_id): + d = Dept.query.get(dept_id) if d: + d.status = not d.status db.session.commit() - message = '启用成功' if res.operate else '禁用成功' + message = '修改成功' return success_api(msg=message) return fail_api(msg="出错啦") diff --git a/applications/templates/department/main.html b/applications/templates/department/main.html index 1c57652..73d9d1b 100644 --- a/applications/templates/department/main.html +++ b/applications/templates/department/main.html @@ -253,7 +253,12 @@ let dtree = layui.dtree {#定义模块访问路径#} - let MODULE_PATH = '/api/v1/dept/' + let MODULE_PATH = '/api/v1/dept' + let DEPARTMENT_API = { + 'DEPARTMENTS': () => `${MODULE_PATH}/departments`, + 'DEPARTMENT': (dept_id) => `${MODULE_PATH}/department/${dept_id}`, + 'DEPARTMENT_STATUS': (dept_id) => `${MODULE_PATH}/department/${dept_id}/status` + } {#定义表格渲染函数#} window.render = function () { @@ -268,7 +273,7 @@ treeDefaultClose: false, toolbar: '#dept_toolbar', /*表格的工具栏*/ elem: '#dept_table', /*表格挂载的对象*/ - url: MODULE_PATH + '/data', /*数据来源接口*/ + url: DEPARTMENT_API.DEPARTMENTS(), /*数据来源接口*/ page: false, cols: [ [ @@ -355,7 +360,7 @@ } let loading = layer.load() $.ajax({ - url: MODULE_PATH + 'enable', + url: DEPARTMENT_API.DEPARTMENT_STATUS(this.value), data: JSON.stringify({ deptId: this.value, operate: operate }), dataType: 'json', contentType: 'application/json', @@ -384,7 +389,7 @@ window.table_line_edit = function (obj) { $.ajax({ - url: MODULE_PATH + 'edit/' + obj.data['deptId'], + url: DEPARTMENT_API.DEPARTMENT(obj.data['deptId']), dataType: 'json', contentType: 'application/json', type: 'get', @@ -420,7 +425,7 @@ layer.close(index) let loading = layer.load() $.ajax({ - url: MODULE_PATH + 'edit/' + obj.data['deptId'], + url: DEPARTMENT_API.DEPARTMENT(obj.data['deptId']), dataType: 'json', type: 'delete', success: function (result) { @@ -440,7 +445,7 @@ // 修改数据 当点击更新之后,获取数据 form.on('submit(dept-update)', function (data) { $.ajax({ - url: MODULE_PATH + 'edit/' + data.field.deptId, + url: DEPARTMENT_API.DEPARTMENT(data.field.deptId), data: JSON.stringify(data.field), dataType: 'json', contentType: 'application/json', @@ -462,7 +467,7 @@ /*添加数据 渲染下拉选择组件*/ dtree.renderSelect({ elem: '#selectParent', - url: MODULE_PATH + 'tree', + url: DEPARTMENT_API.DEPARTMENTS(), method: 'get', selectInputName: { nodeId: 'parentId', context: 'parentName' }, skin: 'layui', @@ -474,7 +479,7 @@ /*添加数据 表单组件,提交按钮之后触发的事件*/ form.on('submit(dept-save)', function (data) { $.ajax({ - url: MODULE_PATH + 'add', + url: DEPARTMENT_API.DEPARTMENTS(), data: JSON.stringify(data.field), dataType: 'json', contentType: 'application/json',