数据范围查询

This commit is contained in:
JaguarJack 2020-06-06 18:57:58 +08:00
parent ac56a7b80d
commit ffeec69333
6 changed files with 70 additions and 42 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace catchAdmin\permissions\model;
trait DataRangScopeTrait
{
/**
* 数据范围查询
*
* @param $roles
* @return mixed
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
protected function scopeDataRange($roles)
{
return $this->whereIn($this->aliasField('creator_id'), $this->getDepartmentUserIdsBy($roles));
}
/**
* 获取部门IDs
*
* @param $roles
* @return array
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
public function getDepartmentUserIdsBy($roles)
{
$userIds = [];
$isAll = false;
$user = request()->user();
foreach ($roles as $role) {
switch ($role->data_range) {
case Roles::ALL_DATA:
$isAll = true;
break;
case Roles::SELF_CHOOSE:
$departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
$userIds = array_merge($userIds, Users::getUserIdsByDepartmentIds($departmentIds));
break;
case Roles::SELF_DATA:
$userIds[] = $user->id;
break;
case Roles::DEPARTMENT_DOWN_DATA:
case Roles::DEPARTMENT_DATA:
$userIds = array_merge($userIds, Users::getUserIdsByDepartmentIds([$user->department_id]));
break;
default:
break;
}
// 如果有全部数据 直接跳出
if ($isAll) {
break;
}
}
return $userIds;
}
}

View File

@ -18,13 +18,12 @@ class Roles extends CatchModel
public const DEPARTMENT_DATA = 4; // 部门数据 public const DEPARTMENT_DATA = 4; // 部门数据
public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据 public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据
protected $field = [ protected $field = [
'id', // 'id', //
'role_name', // 角色名 'role_name', // 角色名
'parent_id', // 父级ID 'parent_id', // 父级ID
'creator_id', // 创建者 'creator_id', // 创建者
'data_range', // 数据范围 'data_range', // 数据范围
'description', // 角色备注 'description', // 角色备注
'created_at', // 创建时间 'created_at', // 创建时间
'updated_at', // 更新时间 'updated_at', // 更新时间
@ -50,41 +49,4 @@ class Roles extends CatchModel
return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id'); return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
} }
public static function getDepartmentUserIdsBy($roles)
{
$uids = [];
$isAll = false;
$user = request()->user();
foreach ($roles as $role) {
switch ($role->data_range) {
case self::ALL_DATA:
$isAll = true;
break;
case self::SELF_CHOOSE:
$departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
$uids = array_merge($uids, Users::getUserIdsByDepartmentIds($departmentIds));
break;
case self::SELF_DATA:
$uids[] = $user->id;
break;
case self::DEPARTMENT_DOWN_DATA:
case self::DEPARTMENT_DATA:
$uids = array_merge($uids, Users::getUserIdsByDepartmentIds([$user->department_id]));
break;
default:
break;
}
// 如果有全部数据 直接跳出
if ($isAll) {
break;
}
}
return $uids;
}
} }

View File

@ -18,8 +18,8 @@ class Users extends CatchModel
'username', // 用户名 'username', // 用户名
'password', // 用户密码 'password', // 用户密码
'email', // 邮箱 登录 'email', // 邮箱 登录
'creator_id', // 创建者ID 'creator_id', // 创建者ID
'department_id', // 部门ID 'department_id', // 部门ID
'status', // 用户状态 1 正常 2 禁用 'status', // 用户状态 1 正常 2 禁用
'last_login_ip', // 最后登录IP 'last_login_ip', // 最后登录IP
'last_login_time', // 最后登录时间 'last_login_time', // 最后登录时间

1
catch/wechat/Menus.php Normal file
View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1 @@
<?php

1
catch/wechat/Users.php Normal file
View File

@ -0,0 +1 @@
<?php