UseScopeBaseModel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Model;
  3. use App\Service\RangeService;
  4. use Illuminate\Database\Eloquent\Model;
  5. class UseScopeBaseModel extends Model
  6. {
  7. //可见范围
  8. const range_function = '';
  9. const is_check_function = '';
  10. public function __construct(array $attributes = [])
  11. {
  12. parent::__construct($attributes);
  13. }
  14. //顶级部门过滤
  15. public function scopeTopClear($query, $user, $search)
  16. {
  17. //是否所有部门
  18. $is_all_depart = $user['is_all_depart'] ?? 0;
  19. //权限范围内的部门
  20. $depart_range = $user['depart_range'] ?? [];
  21. //顶级部门
  22. $search_depart_id = $search['top_depart_id'] ?? 0;
  23. if(empty($search_depart_id)){
  24. //默认进来 自身顶级公司
  25. $top_depart_id = $user['depart_top'][0] ?? [];
  26. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  27. }else{
  28. //查询 顶级公司
  29. $top_depart_id = $search_depart_id;
  30. }
  31. if($is_all_depart){
  32. //所有部门
  33. if(empty($search_depart_id)){
  34. //全部
  35. $query->whereIn('top_depart_id', $depart_range);
  36. }else{
  37. //查看某个分社
  38. $query->where('top_depart_id', $top_depart_id);
  39. }
  40. }else{
  41. //某个分社全部
  42. $query->where('top_depart_id', $top_depart_id);
  43. }
  44. //获取当前门店下
  45. if(! empty($search['get_my_top_depart_data'])){
  46. $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
  47. $depart_id = $depart['depart_id'] ?? 0;
  48. $query->where('top_depart_id', $depart_id);
  49. }
  50. return $query;
  51. }
  52. //
  53. public function scopeKqClear($query, $user, $search)
  54. {
  55. //权限范围内的人
  56. $man_range = $user['man_range'] ?? [];
  57. $man_number = array_unique(array_column($man_range,'number'));
  58. //个人部门所有
  59. $auth_type = $this->getQx($search,$user);
  60. if($auth_type <= 0){
  61. //不需要分个人部门所有的数据
  62. if($auth_type == 0) $query->whereIn('number', $man_number);
  63. }else{
  64. if($auth_type == 1) { //我的
  65. $query->where('number',$user['number']);
  66. }elseif ($auth_type == 2 || $auth_type == 3){
  67. //自己权限范围内的部门 或 所有
  68. $query->whereIn('number', $man_number);
  69. }
  70. }
  71. }
  72. public function getQx($data, $user){
  73. if(empty($data['menu_authority_id'])) return 0;
  74. if($user['id'] == Employee::SPECIAL_ADMIN) return -1;
  75. if(! empty($user['role_authority'][$data['menu_authority_id']])) {
  76. //指定菜单 显示对应权限
  77. return $user['role_authority'][$data['menu_authority_id']];
  78. }else{
  79. return 1;
  80. }
  81. }
  82. function hasMethod($class, $methodName)
  83. {
  84. $reflection = new \ReflectionClass($class);
  85. return $reflection->hasMethod($methodName);
  86. }
  87. }