SystemRole.php 2.3 KB

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