SysMenuFuncService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 SysMenuFuncService extends CommonBaseService
  17. {
  18. protected $cache = true;
  19. protected $cacheBucket = 'SysMenuFunc:';
  20. protected $tokenBucket = 'Token:';
  21. protected $activeBucket = "Active:";
  22. /**
  23. * 保存菜单
  24. * */
  25. public function saveMenuFunc($params)
  26. {
  27. $ret = ['code' => 0, 'data' => 0];
  28. $data = array();
  29. $data['menu_func_name'] = $params['menu_func_name'];
  30. $data['p_id'] = empty($params['p_id']) ? 0 : $params['p_id'];
  31. $data['type'] = empty($params['type']) ? 0 : $params['type'];
  32. $data['route_path'] = empty($params['route_path']) ? '' : $params['route_path'];
  33. $data['update_time'] = nowTime();
  34. $checkRet = $this->checkMenuFunc($params);
  35. if ($checkRet['code'] === 0) {
  36. if (!empty($params['id'])) {
  37. $id = $params['id'];
  38. $data['id'] = $id;
  39. $this->model->where('id', $id)->update($data);
  40. } else {
  41. $data['create_time'] = nowTime();
  42. $id = $this->model->insertGetId($data);
  43. }
  44. $ret['data'] = $id;
  45. $allPath=$this->getMenuFuncAllPath($id);
  46. if($allPath){
  47. $this->model->where('id', $id)->update(['path'=>$allPath['path'],'path_name'=>$allPath['path_name']]);
  48. $this->updateSubAllPath($id);
  49. }
  50. } else {
  51. $ret['code'] = $checkRet['code'];
  52. }
  53. return $ret;
  54. }
  55. /**
  56. * 修改菜单状态
  57. * */
  58. public function setStatus($id, $status)
  59. {
  60. $data = [];
  61. $data['update_time'] = nowTime();
  62. $data['status'] = $status;
  63. $ret = $this->model->where('id', $id)->update($data);
  64. return $ret;
  65. }
  66. /**
  67. * 获取后台菜单列表
  68. * */
  69. public function getMenuFuncList($params)
  70. {
  71. $pageSize = empty($params['page_size']) ? 10 : $params['page_size'];
  72. $page = empty($params['page']) ? 1 : $params['page'];
  73. $skip = ($page - 1) * $pageSize; //页面记录的开始位置,即偏移量
  74. $where = [];
  75. $where[] = ['a.status', '<', '2'];
  76. $query = $this->model->alias('a')->where($where);
  77. if (!empty($params['keyword'])) {
  78. $keyword = $params['keyword'];
  79. $query->where('a.menu_func_name', 'like', "%" . $keyword . "%");
  80. }
  81. if(isset($params['type'])){
  82. $type=empty($params)?0:$params['type'];
  83. $query->where('a.type','=',$type);
  84. }
  85. $totalCount = $query->count();
  86. $list = $query->skip($skip)
  87. ->limit($pageSize)
  88. ->get()->toArray();
  89. $results = buildPage($list, $skip, $page, $pageSize, $totalCount);
  90. return $results;
  91. }
  92. /**
  93. * 角色保存验证 验证通过返回 true 失败返回 false
  94. * return array
  95. * */
  96. private function checkMenuFunc($params)
  97. {
  98. $ret = ['code' => 0];
  99. $routePath = $params['route_path'];
  100. if (!empty($params['id'])) {
  101. $id = $params['id'];
  102. $query = $this->model
  103. ->where('id', '<>', $id)
  104. ->where('status', '<', 2)
  105. ->where('route_path', '=', $routePath);
  106. $info = $query->first();
  107. } else {
  108. $query = $this->model
  109. ->where('status', '<', 2)
  110. ->where('route_path', '=', $routePath);
  111. $info = $query->first();
  112. }
  113. if ($info) {
  114. $info = $info->toArray();
  115. if ($info['route_path'] == $routePath) {
  116. $ret['code'] = 11011;
  117. }
  118. }
  119. return $ret;
  120. }
  121. /**
  122. * 获取功能/菜单的全路径
  123. * */
  124. public function getMenuFuncAllPath($id){
  125. $ret=['path'=>'','path_name'=>''];
  126. $info= $this->model->alias('a')
  127. ->leftJoin($this->model->getTable().' as b', 'a.p_id', '=', 'b.id')
  128. ->where('a.id',$id)
  129. ->where('a.status',0)
  130. ->selectRaw("b.id as p_id,b.menu_func_name as 'p_name',a.id,a.menu_func_name")
  131. ->first();
  132. if($info){
  133. $info=$info->toArray();
  134. if(!empty($info['p_id'])){
  135. $pRet=$this->getMenuFuncAllPath($info['p_id']);
  136. $ret['path']=$pRet['path'].','.$info['id'];
  137. $ret['path_name']=$pRet['path_name'].','.$info['menu_func_name'];
  138. }else{
  139. $ret['path']= $info['id'];
  140. $ret['path_name']= $info['menu_func_name'];
  141. }
  142. }
  143. return $ret;
  144. }
  145. /**
  146. * 更新子集的全路径
  147. * */
  148. public function updateSubAllPath($id){
  149. if($id>0){
  150. $list= $this->model->alias('a')
  151. ->where('a.p_id',$id)
  152. ->where('a.status',0)
  153. ->get();
  154. if(!empty($list)){
  155. $list=$list->toArray();
  156. foreach ($list as $value){
  157. $allPath=$this->getMenuFuncAllPath($value['id']);
  158. if($allPath){
  159. $this->model->where('id', $value['id'])->update(['path'=>$allPath['path'],'path_name'=>$allPath['path_name']]);
  160. $this->updateSubAllPath($value['id']);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }