DepartmentScope.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Scopes;
  3. use App\Service\EmployeeService;
  4. use App\Service\RangeService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Scope;
  8. //判断所属部门和顶级部门
  9. class DepartmentScope implements Scope
  10. {
  11. public $user = [];
  12. public $search = [];
  13. public function __construct($user = [], $search = [])
  14. {
  15. $this->user = $user;
  16. $this->search = $search;
  17. }
  18. public function apply(Builder $builder, Model $model)
  19. {
  20. //权限范围内的部门
  21. $depart_range = $this->user['depart_range'] ?? [];
  22. //可见范围方法
  23. $range_function = $this->user['range_function'] ?? "";
  24. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  25. //确认顶级部门
  26. $search_depart_id = $this->search['top_depart_id'] ?? 0; //顶级公司
  27. if(empty($search_depart_id)){
  28. //默认进来 只显示自己公司下的 自己权限范围下的部门数据
  29. $top_depart_id = $this->user['depart_top'][0] ?? [];
  30. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  31. }else{
  32. //查询给的公司下的 自己权限范围下的部门数据
  33. $top_depart_id = $search_depart_id;
  34. }
  35. //确认顶级部门 结束 =》 $top_depart_id
  36. //顶级部门下 权限部门内所有数据
  37. $builder->where('top_depart_id', $top_depart_id)
  38. ->whereIn('depart_id', $depart_range);
  39. $is_see = $this->search['is_see'] ?? 0; //我可见的
  40. //可见范围 部门 人 过滤后返回的数据id
  41. $id = [];
  42. if($is_see && $is_function_range) $id = RangeService::$range_function($this->user);
  43. $builder->orWhereIn('id', $id);
  44. }
  45. function hasMethod($class, $methodName)
  46. {
  47. $reflection = new \ReflectionClass($class);
  48. return $reflection->hasMethod($methodName);
  49. }
  50. }