数据范围查询

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,7 +18,6 @@ class Roles extends CatchModel
public const DEPARTMENT_DATA = 4; // 部门数据
public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据
protected $field = [
'id', //
'role_name', // 角色名
@ -50,41 +49,4 @@ class Roles extends CatchModel
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;
}
}

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