From fa7e62b12b4b4ad049aa35e34984c6835acf65f5 Mon Sep 17 00:00:00 2001 From: wojiaoyishang Date: Tue, 21 Jan 2025 12:07:37 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=EF=BC=88=E6=AD=A5=E9=AA=A4=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=EF=BC=89=E9=83=A8=E9=97=A8=E7=AE=A1=E7=90=86=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/view/system/dept.py | 49 ++++++++---- applications/view/system/power.py | 1 + templates/system/dept/main.html | 122 ++++++++++++++++++++++++------ templates/system/power/main.html | 3 +- 4 files changed, 135 insertions(+), 40 deletions(-) diff --git a/applications/view/system/dept.py b/applications/view/system/dept.py index 13082e1..eaf3108 100644 --- a/applications/view/system/dept.py +++ b/applications/view/system/dept.py @@ -11,8 +11,6 @@ from applications.schemas import DeptSchema bp = Blueprint('dept', __name__, url_prefix='/dept') -# Todo: 部门管理后端 - @bp.get('/') @authorize("system:dept:main", log=True) def main(): @@ -79,7 +77,7 @@ def save(): ) r = db.session.add(dept) db.session.commit() - return success_api(msg="成功") + return success_api(msg="添加部门成功") @bp.get('/edit') @@ -100,7 +98,7 @@ def enable(): d = Dept.query.filter_by(id=id).update({"status": enable}) if d: db.session.commit() - return success_api(msg="启用成功") + return success_api(msg="启用部门成功") return fail_api(msg="出错啦") return fail_api(msg="数据错误") @@ -115,7 +113,7 @@ def dis_enable(): d = Dept.query.filter_by(id=id).update({"status": enable}) if d: db.session.commit() - return success_api(msg="禁用成功") + return success_api(msg="禁用部门成功") return fail_api(msg="出错啦") return fail_api(msg="数据错误") @@ -137,20 +135,45 @@ def update(): } d = Dept.query.filter_by(id=id).update(data) if not d: - return fail_api(msg="更新失败") + return fail_api(msg="更新部门失败") db.session.commit() - return success_api(msg="更新成功") + return success_api(msg="更新部门成功") @bp.delete('/remove/') @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}) + return fail_api(msg="删除部门失败") + + 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="删除失败") + + return success_api(msg="删除部门成功") + +# 批量删除 +@bp.delete('/batchRemove') +@authorize("system:dept:remove", log=True) +def batch_remove(): + ids = request.form.getlist('ids[]') + + if not ids: + return fail_api(msg="未提供删除 ID") + + for id in ids: + + if not id.isdigit(): + db.session.rollback() + return fail_api(msg="参数提供错误") + + d = Dept.query.filter_by(id=id).delete() + + if not d: + return fail_api(msg="删除部门失败") + + User.query.filter_by(dept_id=id).update({"dept_id": None}) + + db.session.commit() + return success_api(msg="删除部门成功") \ No newline at end of file diff --git a/applications/view/system/power.py b/applications/view/system/power.py index fa838cb..9f0e03c 100644 --- a/applications/view/system/power.py +++ b/applications/view/system/power.py @@ -167,6 +167,7 @@ def remove(id): r = Power.query.filter_by(id=id).delete() db.session.commit() + if r: return success_api(msg="删除成功") else: diff --git a/templates/system/dept/main.html b/templates/system/dept/main.html index 1c1f164..6358b56 100644 --- a/templates/system/dept/main.html +++ b/templates/system/dept/main.html @@ -102,36 +102,66 @@ {field: 'sort', title: '排序', width: 50}, {title: '操作', templet: '#dept-bar', width: 150, align: 'center'} ] - ] + ], + done: function (res, curr, count, origin) { + treeTable.expandAll('dept-table', true) + } }) } form.on('submit(dept-query)', function (data) { - var keyword = data.field.deptName - var $tds = $('#dept-table').next('.treeTable').find('.layui-table-body tbody tr td') - if (!keyword) { - $tds.css('background-color', 'transparent') - layer.msg('请输入关键字', {icon: 5}) - return + var keyword = data.field.deptName; + + if (!keyword) { + layer.msg('搜索内容为空', { + icon: 3, + time: 1000 + }) + return false; } - var searchCount = 0 - $tds.each(function () { - $(this).css('background-color', 'transparent') - if ($(this).text().indexOf(keyword) >= 0) { - $(this).css('background-color', 'rgba(250,230,160,0.5)') - if (searchCount === 0) { - $('body,html').stop(true) - $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500) + + treeTable.checkAllNodes('dept-table', false); + + var expandNotes = []; + + treeTable.getNodesByFilter('dept-table', function (item) { + if (item.dept_name.indexOf(keyword) !== -1) { + treeTable.setRowChecked('dept-table', {index: item, checked: true}); + + // 遍历其全部父元素 + var parent_id = item.parent_id; + + while (parent_id !== 0) { + + if (!expandNotes.includes(parent_id)) { + expandNotes.push(item.LAY_DATA_INDEX); + } + + item = treeTable.getNodeById('dept-table', item.parent_id).data; + parent_id = item.parent_id; } - searchCount++ + + + if (!expandNotes.includes(parent_id)) { + expandNotes.push(item.LAY_DATA_INDEX); + } + + return true; } + + }) - if (searchCount === 0) { - layer.msg('没有匹配结果', {icon: 5}) - } else { - treeTable.expandAll('#dept-table') - } - return false + + treeTable.expandAll('dept-table', false); + + expandNotes.forEach(function (note_id) { + treeTable.expandNode('dept-table', { + index: note_id, + expandFlag: true + }); + }) + + return false; }) render() @@ -150,8 +180,7 @@ } else if (obj.event === 'refresh') { window.refresh() } else if (obj.event === 'batchRemove') { - {#window.batchRemove(obj)#} - console.log(table.checkStatus(obj.config.id).data) + window.batchRemove(obj) } }) @@ -212,7 +241,7 @@ layer.close(loading) if (result.success) { popup.success(result.msg, function () { - obj.del() + treeTable.removeNode('dept-table', obj.data.LAY_DATA_INDEX) }) } else { popup.failure(result.msg) @@ -221,6 +250,49 @@ }) }) } + + window.batchRemove = function (obj) { + let data = treeTable.checkStatus(obj.config.id).data + if (data.length === 0) { + layer.msg('未选中数据', { + icon: 3, + time: 1000 + }) + return false + } + var ids = [] + var hasCheck = treeTable.checkStatus('dept-table') + var hasCheckData = hasCheck.data + if (hasCheckData.length > 0) { + $.each(hasCheckData, function (index, element) { + ids.push(element.id) + }) + } + layer.confirm('确定要删除选中部门', { + icon: 3, + title: '提示' + }, function (index) { + layer.close(index) + let loading = layer.load() + $.ajax({ + url: MODULE_PATH + 'batchRemove', + data: {ids: ids}, + dataType: 'json', + type: 'delete', + success: function (result) { + layer.close(loading) + if (result.success) { + popup.success(result.msg, function () { + treeTable.reload('dept-table'); + }) + } else { + popup.failure(result.msg) + } + } + }) + }) + } + }) \ No newline at end of file diff --git a/templates/system/power/main.html b/templates/system/power/main.html index b8acda3..af96ea6 100644 --- a/templates/system/power/main.html +++ b/templates/system/power/main.html @@ -248,8 +248,7 @@ layer.close(loading) if (result.success) { popup.success(result.msg, function () { - console.log(obj) - obj.del() + treeTable.removeNode('power-table', obj.data.LAY_DATA_INDEX) }) } else { popup.failure(result.msg)