// +---------------------------------------------------------------------- namespace app\admin\model; use think\Db; use think\Exception; use think\Model; /** * 用户组模型类 * Class AuthGroupModel */ class AuthGroup extends Model { protected static $roleList = []; public static function init() { self::beforeDelete(function ($row) { if ($row->id == 1) { throw new Exception("超级管理员角色不能被删除!"); } $admin = Db::name('Admin')->where('roleid', $row->id)->find(); if ($admin) { throw new Exception("该角色下有管理员!"); } //子角色列表 $child = explode(',', self::getArrchildid($row->id)); if (count($child) > 1) { throw new Exception("该角色下有子角色,请删除子角色才可以删除!"); } }); } /** * 通过递归的方式获取该角色下的全部子角色 * @param type $id * @return string */ public static function getArrchildid($id) { if (empty(self::$roleList)) { self::$roleList = self::order(["id" => "desc"])->column('*', 'id'); } $arrchildid = $id; if (is_array(self::$roleList)) { foreach (self::$roleList as $k => $cat) { if ($cat['parentid'] && $k != $id && $cat['parentid'] == $id) { $arrchildid .= ',' . self::getArrchildid($k); } } } return $arrchildid; } }