SystemRole.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 身份管理 model
  7. * Class SystemRole
  8. * @package app\admin\model\system
  9. */
  10. class SystemRole extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'system_role';
  22. use ModelTrait;
  23. public static function setRulesAttr($value)
  24. {
  25. return is_array($value) ? implode(',', $value) : $value;
  26. }
  27. /**
  28. * 选择管理员身份
  29. * @param int $level
  30. * @return array
  31. */
  32. public static function getRole($level = 0)
  33. {
  34. return self::where('status', 1)->where('level', $level)->column('role_name', 'id');
  35. }
  36. public static function rolesByAuth($rules)
  37. {
  38. if (empty($rules)) return [];
  39. $rules = self::where('id', 'IN', $rules)->where('status', '1')->column('rules', 'id');
  40. $rules = array_unique(explode(',', implode(',', $rules)));
  41. $_auth = SystemMenus::all(function ($query) use ($rules) {
  42. $query->where('id', 'IN', $rules)
  43. ->where('controller|action', '<>', '')
  44. ->field('id,module,controller,action,params');
  45. });
  46. return self::tidyAuth($_auth ?: []);
  47. }
  48. public static function getAllAuth()
  49. {
  50. static $auth = null;
  51. $auth === null && ($auth = self::tidyAuth(SystemMenus::all(function ($query) {
  52. $query->where('controller|action', '<>', '')->field('id,module,controller,action,params');
  53. }) ?: []));
  54. return $auth;
  55. }
  56. protected static function tidyAuth($_auth)
  57. {
  58. $auth = [];
  59. foreach ($_auth as $k => $val) {
  60. $auth[$val['id']] = SystemMenus::getAuthName($val['action'], $val['controller'], $val['module'], $val['params']);
  61. }
  62. return $auth;
  63. }
  64. public static function systemPage($where)
  65. {
  66. $model = new self;
  67. if (strlen(trim($where['role_name']))) $model = $model->where('role_name', 'LIKE', "%$where[role_name]%");
  68. if (strlen(trim($where['status']))) $model = $model->where('status', $where['status']);
  69. $model = $model->where('level', bcadd($where['level'], 1, 0));
  70. return self::page($model, (function ($item) {
  71. $item->rules = SystemMenus::where('id', 'IN', $item->rules)->column('menu_name', 'id');
  72. }), $where);
  73. }
  74. }