SystemRole.php 2.4 KB

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