| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 | <?phpnamespace App\Scopes;use App\Service\RangeService;use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Scope;//判断所属部门和顶级部门(公司)class DepartmentScope implements Scope{    public $user = [];    public $search = [];    public function __construct($user = [], $search = [])    {        $this->user = $user;        $this->search = $search;    }    public function apply(Builder $builder, Model $model)    {        //是否所有部门        $is_all_depart = $this->user['is_all_depart'] ?? 0;        //权限范围内的部门        $depart_range = $this->user['depart_range'] ?? [];        //我可见的        $is_see = $this->search['is_see'] ?? 0;        //可见范围方法        $range_function = $this->user['range_function'] ?? "";        $is_function_range = $this->hasMethod(new RangeService(),$range_function);        //顶级部门        $search_depart_id = $this->search['top_depart_id'] ?? 0; //顶级公司        if(empty($search_depart_id)){            //默认进来 自身顶级公司            $top_depart_id = $this->user['depart_top'][0] ?? [];            $top_depart_id = $top_depart_id['depart_id'] ?? 0;        }else{            //查询 顶级公司            $top_depart_id = $search_depart_id;        }        $id = [];        //可见范围 以及单据里面填写人员        if($is_function_range) $id = RangeService::$range_function($this->user,$this->search);        if($is_all_depart){            //所有部门            if(empty($search_depart_id)){                if(! $is_see){                    //全部                    $builder->whereIn('depart_id', $depart_range);                }else{                    //可见                    $builder->whereIn('id', $id);                }            }else{                if(! $is_see){                    //查看某个分社                    $builder->where('top_depart_id', $top_depart_id);                }else{                    //查看某个分社可见                    $builder->whereIn('id', $id);                }            }        }else{            //分社            if(! $is_see){                //某个分社全部                $builder->where('top_depart_id', $top_depart_id)                    ->whereIn('depart_id', $depart_range)                    ->orWhereIn('id', $id);            }else{                //某个分社可见                $builder->whereIn('id', $id);            }        }    }    function hasMethod($class, $methodName)    {        $reflection = new \ReflectionClass($class);        return $reflection->hasMethod($methodName);    }}
 |