SysRoleService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ywl
  5. * Date: 2017/4/14
  6. * Time: 11:38
  7. */
  8. namespace App\User\Services;
  9. use App\Exceptions\ApiException;
  10. use App\Services\CommonBaseService;
  11. use App\User\Models\SysAdminUserModel;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Mail;
  16. class SysRoleService extends CommonBaseService
  17. {
  18. protected $cache = true;
  19. protected $cacheBucket = 'SysRole:';
  20. protected $tokenBucket = 'Token:';
  21. protected $activeBucket = "Active:";
  22. /**
  23. * 保存角色
  24. * */
  25. public function saveRole($params)
  26. {
  27. $ret = ['code' => 0, 'data' => 0];
  28. $data = array();
  29. $data['role_name'] = $params['role_name'];
  30. $data['update_time'] = nowTime();
  31. $checkRet = $this->checkRole($params);
  32. if ($checkRet['code'] === 0) {
  33. if (!empty($params['id'])) {
  34. $id = $params['id'];
  35. $data['id'] = $id;
  36. $this->model->where('id', $id)->update($data);
  37. } else {
  38. $data['create_time'] = nowTime();
  39. $id = $this->model->insertGetId($data);
  40. }
  41. $ret['data'] = $id;
  42. } else {
  43. $ret['code'] = $checkRet['code'];
  44. }
  45. return $ret;
  46. }
  47. /**
  48. * 修改角色状态
  49. * */
  50. public function setStatus($id, $status)
  51. {
  52. $data = [];
  53. $data['update_time'] = nowTime();
  54. $data['status'] = $status;
  55. $ret = $this->model->where('id', $id)->update($data);
  56. return $ret;
  57. }
  58. /**
  59. * 获取后台角色列表
  60. * */
  61. public function getRoleList($params)
  62. {
  63. $pageSize = empty($params['page_size']) ? 10 : $params['page_size'];
  64. $page = empty($params['page']) ? 1 : $params['page'];
  65. $skip = ($page - 1) * $pageSize; //页面记录的开始位置,即偏移量
  66. $where = [];
  67. $where[] = ['a.status', '<', '2'];
  68. $query = $this->model->alias('a')->where($where);
  69. if (!empty($params['keyword'])) {
  70. $keyword = $params['keyword'];
  71. $query->where('role_name', 'like', "%" . $keyword . "%");
  72. }
  73. $totalCount = $query->count();
  74. $list = $query->skip($skip)
  75. ->limit($pageSize)
  76. ->get()->toArray();
  77. $list=mapByKey($list,'id');
  78. $ids=array_keys($list);
  79. $roleData=[];
  80. if(!empty($ids)){
  81. $roleAuth= $this->model->alias('a')
  82. ->leftJoin('sys_menu_func_auth as b', 'a.id', '=', 'b.role_id')
  83. ->leftJoin('sys_menu_func as c', 'b.menu_func_id', '=', 'c.id')
  84. ->whereIn('a.id',$ids)
  85. ->where('b.status',0)
  86. ->where('c.status',0)
  87. ->selectRaw('b.role_id,b.menu_func_id,c.menu_func_name,path_name,c.type')
  88. ->get();
  89. if(!empty($roleAuth)){
  90. $roleAuth=$roleAuth->toArray();
  91. foreach ($roleAuth as $roleValue){
  92. $roleData[$roleValue['role_id']][]=$roleValue;
  93. }
  94. }
  95. }
  96. foreach ($list as &$value){
  97. if(!empty($roleData[$value['id']])){
  98. $value['auth_item']=$roleData[$value['id']];
  99. }else{
  100. $value['auth_item']=[];
  101. }
  102. }
  103. $list=array_values($list);
  104. $results = buildPage($list, $skip, $page, $pageSize, $totalCount);
  105. return $results;
  106. }
  107. /**
  108. * 角色保存验证 验证通过返回 true 失败返回 false
  109. * return array
  110. * */
  111. private function checkRole($params)
  112. {
  113. $ret = ['code' => 0];
  114. $role_name = $params['role_name'];
  115. if (!empty($params['id'])) {
  116. $id = $params['id'];
  117. $exist = $this->model
  118. ->where('id', '<>', $id)
  119. ->where('role_name', '=', $role_name)
  120. ->count();
  121. } else {
  122. $exist = $this->model
  123. ->where('role_name', '=', $role_name)
  124. ->count();
  125. }
  126. if ($exist) {
  127. $ret['code'] = 11001;
  128. }
  129. return $ret;
  130. }
  131. }