From 3a9831fc8c5fdfbd09fcd392684ecac97cca9d24 Mon Sep 17 00:00:00 2001 From: linwenxiang <991154416@qq.com> Date: Thu, 3 Sep 2020 11:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=A7=92=E8=89=B2=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6=E6=9C=89=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/system/role.go | 2 +- models/role.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/apis/system/role.go b/apis/system/role.go index 3187b2f..bc10dce 100644 --- a/apis/system/role.go +++ b/apis/system/role.go @@ -145,6 +145,6 @@ func DeleteRole(c *gin.Context) { IDS := tools.IdsStrToIdsIntGroup("roleId", c) _, err := Role.BatchDelete(IDS) - tools.HasError(err, "删除失败1", -1) + tools.HasError(err, "删除失败", -1) app.OK(c, "", "删除成功") } diff --git a/models/role.go b/models/role.go index 57703d9..2ea0915 100644 --- a/models/role.go +++ b/models/role.go @@ -191,6 +191,16 @@ func (role *SysRole) BatchDelete(id []int) (Result bool, err error) { return false, err } + var count int64 + if err := tx.Table("sys_user").Where("role_id in (?)", id).Count(&count).Error; err != nil { + tx.Rollback() + return false, err + } + if count > 0 { + tx.Rollback() + return false, errors.New("存在绑定用户,请解绑后重试") + } + // 删除角色 if err = tx.Table(role.TableName()).Where("role_id in (?)", id).Unscoped().Delete(&SysRole{}).Error; err != nil { tx.Rollback()