123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- namespace App\Model;
- use App\Scopes\TopDepartmentScope;
- use App\Service\RangeService;
- use Illuminate\Database\Eloquent\Model;
- class UseScopeBaseModel extends Model
- {
- //可见范围
- const range_function = '';
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- }
- //顶级部门过滤
- public function scopeTopClear($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0;
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $user['depart_top'][0] ?? [];
- $top_depart_id = $top_depart_id['depart_id'] ?? 0;
- }else{
- //查询 顶级公司
- $top_depart_id = $search_depart_id;
- }
- if($is_all_depart){
- //所有部门
- if(empty($search_depart_id)){
- //全部
- $query->whereIn('top_depart_id', $depart_range);
- }else{
- //查看某个分社
- $query->where('top_depart_id', $top_depart_id);
- }
- }else{
- //某个分社全部
- $query->where('top_depart_id', $top_depart_id);
- }
- //获取当前门店下
- if(! empty($search['get_my_top_depart_data'])){
- $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
- $depart_id = $depart['depart_id'] ?? 0;
- $query->where('top_depart_id', $depart_id);
- }
- return $query;
- }
- //部门和顶级部门(公司)过滤
- public function scopeClear($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //我可见的
- $is_see = $search['is_see'] ?? 0;
- //可见范围方法
- $model = $query->getModel(); // 获取模型的实例
- $range_function = $model::range_function ?? ""; // 访问静态属性
- $is_function_range = $this->hasMethod(new RangeService(),$range_function);
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $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($user,$search);
- //个人部门所有
- $auth_type = $this->getQx($search,$user);
- if($is_all_depart){
- //所有权限
- if(empty($search_depart_id)){
- if(! $is_see){
- if(! $auth_type){
- //全部
- $query->whereIn('depart_id', $depart_range);
- }else{
- if($auth_type == 1) { //我创建的 且加上可见
- $query->where('crt_id',$user['id'])
- ->orWhereIn('id', $id);
- }elseif ($auth_type == 2 || $auth_type == 3){
- //自己权限范围内的部门 或 所有
- $query->whereIn('depart_id', $depart_range);
- }
- }
- }else{
- //可见
- $query->whereIn('id', $id);
- }
- }else{
- if(! $is_see){
- if(! $auth_type){
- //查看指定公司
- $query->where('top_depart_id', $top_depart_id);
- }else{
- if($auth_type == 1) { //指定公司下 且 我创建的 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->where('crt_id',$user['id'])
- ->orWhereIn('id', $id);
- }elseif ($auth_type == 2 || $auth_type == 3){
- // (指定公司下的 且 自己权限范围内的部门 或 所有) 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->whereIn('depart_id', $depart_range)
- ->orWhereIn('id', $id);
- }
- }
- }else{
- //查看指定公司 且 我可见
- $query->whereIn('id', $id);
- }
- }
- }else{
- //非所有权限
- if(! $is_see){
- if(! $auth_type){
- //指定公司下全部 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->whereIn('depart_id', $depart_range)
- ->orWhereIn('id', $id);
- }else{
- if($auth_type == 1) {
- //指定公司下 且 我创建的 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->where('crt_id',$user['id'])
- ->orWhereIn('id', $id);
- }elseif ($auth_type == 2) {
- //指定公司下 且 自己权限范围内的部门 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->whereIn('depart_id', $depart_range)
- ->orWhereIn('id', $id);
- }elseif ($auth_type == 3) {
- // 指定公司下所有 且加上可见
- $query->where('top_depart_id', $top_depart_id)
- ->orWhereIn('id', $id);
- }
- }
- }else{
- //某个分社可见
- $query->whereIn('id', $id);
- }
- }
- if(! empty($search['get_my_top_depart_data'])){
- $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
- $depart_id = $depart['depart_id'] ?? 0;
- $query->where('top_depart_id', $depart_id);
- }
- }
- //部门和顶级部门(公司)过滤 取别名a
- public function scopeAClear($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //我可见的
- $is_see = $search['is_see'] ?? 0;
- //可见范围方法
- $model = $query->getModel(); // 获取模型的实例
- $range_function = $model::range_function ?? ""; // 访问静态属性
- $is_function_range = $this->hasMethod(new RangeService(),$range_function);
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $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($user,$search);
- //个人部门所有
- $auth_type = $this->getQx($search,$user);
- if($is_all_depart){
- //所有权限
- if(empty($search_depart_id)){
- if(! $is_see){
- if(! $auth_type){
- //全部
- $query->whereIn('a.depart_id', $depart_range);
- }else{
- if($auth_type == 1) { //我创建的 且加上可见
- $query->where('a.crt_id',$user['id'])
- ->orWhereIn('a.id', $id);
- }elseif ($auth_type == 2 || $auth_type == 3){
- //自己权限范围内的部门 或 所有
- $query->whereIn('a.depart_id', $depart_range);
- }
- }
- }else{
- //可见
- $query->whereIn('a.id', $id);
- }
- }else{
- if(! $is_see){
- if(! $auth_type){
- //查看指定公司
- $query->where('a.top_depart_id', $top_depart_id);
- }else{
- if($auth_type == 1) { //指定公司下 且 我创建的 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->where('a.crt_id',$user['id'])
- ->orWhereIn('a.id', $id);
- }elseif ($auth_type == 2 || $auth_type == 3){
- // (指定公司下的 且 自己权限范围内的部门 或 所有) 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->whereIn('a.depart_id', $depart_range)
- ->orWhereIn('a.id', $id);
- }
- }
- }else{
- //查看指定公司 且 我可见
- $query->whereIn('a.id', $id);
- }
- }
- }else{
- //非所有权限
- if(! $is_see){
- if(! $auth_type){
- //指定公司下全部 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->whereIn('a.depart_id', $depart_range)
- ->orWhereIn('a.id', $id);
- }else{
- if($auth_type == 1) {
- //指定公司下 且 我创建的 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->where('a.crt_id',$user['id'])
- ->orWhereIn('a.id', $id);
- }elseif ($auth_type == 2) {
- //指定公司下 且 自己权限范围内的部门 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->whereIn('a.depart_id', $depart_range)
- ->orWhereIn('a.id', $id);
- }elseif ($auth_type == 3) {
- // 指定公司下所有 且加上可见
- $query->where('a.top_depart_id', $top_depart_id)
- ->orWhereIn('a.id', $id);
- }
- }
- }else{
- //某个分社可见
- $query->whereIn('a.id', $id);
- }
- }
- }
- //部门和顶级部门(公司)过滤 Old
- public function scopeClear1($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //我可见的
- $is_see = $search['is_see'] ?? 0;
- //可见范围方法
- $model = $query->getModel(); // 获取模型的实例
- $range_function = $model::range_function ?? ""; // 访问静态属性
- $is_function_range = $this->hasMethod(new RangeService(),$range_function);
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $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($user,$search);
- if($is_all_depart){
- //所有部门
- if(empty($search_depart_id)){
- if(! $is_see){
- //全部
- $query->whereIn('depart_id', $depart_range);
- }else{
- //可见
- $query->whereIn('id', $id);
- }
- }else{
- if(! $is_see){
- //查看某个分社
- $query->where('top_depart_id', $top_depart_id);
- }else{
- //查看某个分社可见
- $query->whereIn('id', $id);
- }
- }
- }else{
- //某个分社
- if(! $is_see){
- //某个分社全部
- $query->where('top_depart_id', $top_depart_id)
- ->whereIn('depart_id', $depart_range)
- ->orWhereIn('id', $id);
- }else{
- //某个分社可见
- $query->whereIn('id', $id);
- }
- }
- }
- //顶级部门过滤 取别名a
- public function scopeATopClear($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0;
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $user['depart_top'][0] ?? [];
- $top_depart_id = $top_depart_id['depart_id'] ?? 0;
- }else{
- //查询 顶级公司
- $top_depart_id = $search_depart_id;
- }
- if($is_all_depart){
- //所有部门
- if(empty($search_depart_id)){
- //全部
- $query->whereIn('a.top_depart_id', $depart_range);
- }else{
- //查看某个分社
- $query->where('a.top_depart_id', $top_depart_id);
- }
- }else{
- //某个分社全部
- $query->where('a.top_depart_id', $top_depart_id);
- }
- return $query;
- }
- //产品不可见 部门和顶级部门(公司)过滤
- public function scopeProductClear($query, $user, $search)
- {
- //是否所有部门
- $is_all_depart = $user['is_all_depart'] ?? 0;
- //权限范围内的部门
- $depart_range = $user['depart_range'] ?? [];
- //总社id
- $top_depart_id = $user['head'] ?? [];
- $top_depart = $top_depart_id['id'] ?? 0;
- //可见范围方法
- $model = $query->getModel(); // 获取模型的实例
- $range_function = $model::range_function ?? ""; // 访问静态属性
- $is_function_range = $this->hasMethod(new RangeService(),$range_function);
- //顶级部门
- $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
- if(empty($search_depart_id)){
- //默认进来 自身顶级公司
- $top_depart_id = $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($user,$search);
- if($is_all_depart){
- //所有部门
- if(empty($search_depart_id)){
- //全部
- $query->whereIn('depart_id', $depart_range);
- }else{
- //查看某个分社
- $query->where('top_depart_id', $top_depart_id);
- }
- }else{
- //某个分社全部 去掉不可见数
- $query->where('top_depart_id', $top_depart_id)
- ->whereIn('depart_id', $depart_range)
- ->orWhere('top_depart_id',$top_depart)
- ->whereNotIn('id', $id);
- }
- }
- public function getQx($data, $user){
- if(empty($data['menu_id'])) return 0;
- if($user['id'] == Employee::SPECIAL_ADMIN) return 0;
- if(! empty($user['role_authority'][$data['menu_id']])) {
- //指定菜单 显示对应权限
- return $user['role_authority'][$data['menu_id']];
- }else{
- return 0;
- }
- }
- function hasMethod($class, $methodName)
- {
- $reflection = new \ReflectionClass($class);
- return $reflection->hasMethod($methodName);
- }
- }
|