SystemRole.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\model\system;
  8. use plugin\saiadmin\basic\BaseModel;
  9. /**
  10. * 角色模型
  11. */
  12. class SystemRole extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 数据表完整名称
  21. * @var string
  22. */
  23. protected $table = 'sa_system_role';
  24. /**
  25. * 权限范围
  26. */
  27. public function scopeAuth($query, $value)
  28. {
  29. $id = $value['id'];
  30. $roles = $value['roles'];
  31. if ($id > 1) {
  32. $ids = [];
  33. foreach ($roles as $item) {
  34. $ids[] = $item['id'];
  35. $temp = static::whereRaw('FIND_IN_SET("'.$item['id'].'", level) > 0')->column('id');
  36. $ids = array_merge($ids, $temp);
  37. }
  38. $query->where('id', 'in', array_unique($ids));
  39. }
  40. }
  41. /**
  42. * 通过中间表获取菜单
  43. */
  44. public function menus()
  45. {
  46. return $this->belongsToMany(SystemMenu::class, SystemRoleMenu::class, 'menu_id', 'role_id');
  47. }
  48. /**
  49. * 通过中间表获取部门
  50. */
  51. public function depts()
  52. {
  53. return $this->belongsToMany(SystemDept::class, SystemRoleDept::class, 'dept_id', 'role_id');
  54. }
  55. }