Roles.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace catchAdmin\permissions\model;
  3. use catchAdmin\permissions\model\search\RolesSearch;
  4. use catchAdmin\permissions\model\DataRangScopeTrait;
  5. use catcher\base\CatchModel;
  6. use think\facade\Db;
  7. use catcher\Utils;
  8. class Roles extends CatchModel
  9. {
  10. use HasDepartmentsTrait;
  11. use HasPermissionsTrait;
  12. use RolesSearch;
  13. use DataRangScopeTrait;
  14. protected $name = 'roles';
  15. public const ALL_DATA = 1; // 全部数据
  16. public const SELF_CHOOSE = 2; // 自定义数据
  17. public const SELF_DATA = 3; // 本人数据
  18. public const DEPARTMENT_DATA = 4; // 部门数据
  19. public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据
  20. protected $field = [
  21. 'id', //
  22. 'role_name', // 角色名
  23. 'identify', // 身份标识
  24. 'parent_id', // 父级ID
  25. 'creator_id', // 创建者
  26. 'data_range', // 数据范围
  27. 'description', // 角色备注
  28. 'created_at', // 创建时间
  29. 'updated_at', // 更新时间
  30. 'deleted_at', // 删除状态,0未删除 >0 已删除
  31. ];
  32. public function getList()
  33. {
  34. $where = array();
  35. if (!Utils::isSuperAdmin()) {
  36. $user = request()->user();
  37. /* $role_id=Db::table("user_has_roles")->where('uid',$user->id)->value('role_id');
  38. $parentiIds = $this->getParentRoleIds($role_id);
  39. $childIds = $this->getChildrenRoleIds($role_id);
  40. $roleIds = array_merge($parentiIds,$childIds);
  41. $where[] = ['id','in',$roleIds]; */
  42. $role_ids = Db::table("user_has_roles")->where('uid',$user->id)->column('role_id');
  43. $childIds = $this->getChildrenRoleIds($role_ids);
  44. $roleIds = array_merge($role_ids,$childIds);
  45. if (!empty($roleIds)) {
  46. $where[] = ['id','in',$roleIds];
  47. }
  48. }
  49. // $no_display_roles = $this->whereIn('identify','student,teacher,parents,schoolmaster')->column('id');
  50. // var_dump($this->getLastSql());
  51. return $this->where($where)
  52. // ->whereNotIn('id',$no_display_roles)
  53. ->catchSearch()
  54. ->order('id', 'desc')
  55. ->where('id','<>',1) //超级管理员账号不显示
  56. ->select()
  57. ->toTree();
  58. }
  59. /**
  60. * 判断是否是全部数据权限
  61. * @time 2021年09月17日
  62. * @return datarange
  63. */
  64. public function isAllDataRange(){
  65. $user = request()->user();
  66. if (empty($roles)) {
  67. $roles = $user->getRoles();
  68. }
  69. //判断是否
  70. foreach ($roles as $role) {
  71. // 如果有全部数据 直接跳出
  72. if ($role->data_range == Roles::ALL_DATA) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. //用户管理获取角色(除了家长、学生、教师、班主任、校长等)
  79. public function getPartList()
  80. {
  81. $where = array();
  82. if (!Utils::isSuperAdmin()) {
  83. $user = request()->user();
  84. $roleIds = Db::table("user_has_roles")->where('uid',$user->id)->column('role_id');
  85. if (!empty($roleIds)) {
  86. $where[] = ['id','in',$roleIds];
  87. }
  88. }
  89. // 获取学校角色
  90. $no_display_roles = static::getDeviceUserRoleids();
  91. return $this->where($where)
  92. ->whereNotIn('id',$no_display_roles)
  93. ->catchSearch()
  94. ->order('id', 'desc')
  95. ->select()
  96. ->toTree();
  97. }
  98. /**
  99. *
  100. * @time 2019年12月08日
  101. * @return \think\model\relation\BelongsToMany
  102. */
  103. public function users(): \think\model\relation\BelongsToMany
  104. {
  105. return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
  106. }
  107. /**
  108. * 获取子角色IDS
  109. *
  110. * @time 2020年11月04日
  111. * @param $id
  112. * @throws DbException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. * @return mixed
  116. */
  117. public static function getChildrenRoleIds($roleIds)
  118. {
  119. $roleIds = Roles::field(['id', 'parent_id'])->select()->getAllChildrenIds($roleIds);
  120. // $roleIds[] = $id;
  121. return $roleIds;
  122. }
  123. /**
  124. * 获取上级至顶级角色ids
  125. */
  126. public function getParentRoleIds($id)
  127. {
  128. $ids = [];
  129. $pid = Roles::where('id', $id)->value('parent_id');
  130. if ($pid > 0) {
  131. $ids[] = $pid;
  132. $parentId = $this->getParentRoleIds($pid);
  133. $ids = array_merge($ids, $parentId);
  134. }
  135. return $ids;
  136. }
  137. /**
  138. * 获取设备用户角色id
  139. */
  140. public static function getDeviceUserRoleids()
  141. {
  142. return static::whereIn('identify','personal,group_card_user,group_badge_user')->column('id');
  143. }
  144. }