MenuService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Menu;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\UserService;
  9. use App\Services\RoleUserService;
  10. use App\Services\RoleMenuService;
  11. /**
  12. * 菜单
  13. */
  14. class MenuService extends BaseService
  15. {
  16. /**
  17. * @description: 模型
  18. * @return {string}
  19. */
  20. public static function model(): string
  21. {
  22. return Menu::class;
  23. }
  24. /**
  25. * @description: 枚举
  26. * @return {*}
  27. */
  28. public static function enum(): string
  29. {
  30. return '';
  31. }
  32. /**
  33. * @description: 获取查询条件
  34. * @param {array} $search 查询内容
  35. * @return {array}
  36. */
  37. public static function getWhere(array $search = []): array
  38. {
  39. $where = [];
  40. if (isset($search['id']) && !empty($search['id'])) {
  41. $where[] = ['id', '=', $search['id']];
  42. }
  43. if (isset($search['parent_id']) && !empty($search['parent_id'])) {
  44. $where[] = ['parent_id', '=', $search['parent_id']];
  45. }
  46. if (isset($search['type']) && !empty($search['type'])) {
  47. $where[] = ['type', '=', $search['type']];
  48. }
  49. if (isset($search['uri']) && !empty($search['uri'])) {
  50. $where[] = ['uri', '=', $search['uri']];
  51. }
  52. if (isset($search['status']) && !empty($search['status'])) {
  53. $where[] = ['status', '=', $search['status']];
  54. }
  55. if (isset($search['title']) && !empty($search['title'])) {
  56. $where[] = ['title', 'like', '%'.$search['title'].'%'];
  57. }
  58. return $where;
  59. }
  60. /**
  61. * @description: 查询单条数据
  62. * @param array $search
  63. * @return \App\Models\Coin|null
  64. */
  65. public static function findOne(array $search): ?Menu
  66. {
  67. return self::model()::where(self::getWhere($search))->first();
  68. }
  69. /**
  70. * @description: 查询所有数据
  71. * @param array $search
  72. * @return \Illuminate\Database\Eloquent\Collection
  73. */
  74. public static function findAll(array $search = [])
  75. {
  76. return self::model()::where(self::getWhere($search))->get();
  77. }
  78. /**
  79. * @description: 分页查询
  80. * @param array $search
  81. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  82. */
  83. public static function paginate(array $search = [])
  84. {
  85. $limit = isset($search['limit']) ? $search['limit'] : 15;
  86. $paginator = self::model()::where(self::getWhere($search))
  87. ->orderBy("sort", 'asc')
  88. ->paginate($limit);
  89. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  90. }
  91. /**
  92. * @description:
  93. * @param {*} $params
  94. * @return {*}
  95. */
  96. public static function submit($params = [])
  97. {
  98. $result = false;
  99. $msg['code'] = self::NOT;
  100. $msg['msg'] = '';
  101. // 2. 判断是否是更新
  102. if (!empty($params['id'])) {
  103. // 更新
  104. $info = self::findOne(['id'=>$params['id']] );
  105. if (!$info) {
  106. $msg['msg'] = '菜单不存在!';
  107. }else{
  108. $result = $info->update($params);
  109. }
  110. } else {
  111. // 创建
  112. $result = $info = self::model()::create($params);
  113. }
  114. if($result){
  115. $msg['code'] = self::YES;
  116. $msg['msg'] = '设置成功';
  117. }else{
  118. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  119. }
  120. return $msg;
  121. }
  122. /**
  123. * @description: 获取菜单树
  124. * @return {*}
  125. */
  126. public static function getTree()
  127. {
  128. $list = self::findAll(['status' => self::model()::STATUS_SHOW]);
  129. $tree = self::toTree($list,0,0,'parent_id');
  130. return $tree;
  131. }
  132. /**
  133. * @description:
  134. * @param {*} $userId
  135. * @param {*} $type
  136. * @return {*}
  137. */
  138. public static function getUserMenu($userId ,$type = 1)
  139. {
  140. // 超级管理账号直接查看所有的菜单
  141. if($userId == 1){
  142. $list = self::findAll(['type' => $type ,'status' => self::model()::STATUS_SHOW]);
  143. }else{
  144. $roleIds = RoleUserService::model()::where('user_id',$userId)->pluck('role_id')->toArray();
  145. $roles = RoleService::model()::with(['menus'=> function ($query) use ($type) {
  146. $query->where('type', $type);
  147. }])->whereIn('id',$roleIds)->get();
  148. $allMenus = [];
  149. foreach ($roles as $role) {
  150. if (!empty($role['menus'])) {
  151. $allMenus = array_merge($allMenus, $role['menus']->toArray());
  152. }
  153. }
  154. $list = $allMenus;
  155. }
  156. $tree = self::toTree($list,0,0,'parent_id');
  157. return $tree;
  158. }
  159. /**
  160. * @description: 校验按钮权限
  161. * @param {*} $userId
  162. * @param {*} $uri
  163. * @return {*}
  164. */
  165. public static function checkMenu($userId ,$uri)
  166. {
  167. if($userId == 1){
  168. return true;
  169. }else{
  170. $info = self::findOne(['uri' => $uri ,'status' => self::model()::STATUS_SHOW ,'type' => self::model()::TYPE_BUTTON]);
  171. if($info){
  172. $menuId = $info->id;
  173. $roleIds = RoleUserService::model()::where('user_id',$userId)->pluck('role_id')->toArray();
  174. $result = RoleMenuService::model()::whereIn('role_id', $roleIds)
  175. ->where('menu_id', $menuId)
  176. ->count();
  177. if($result){
  178. return true;
  179. }else{
  180. return false;
  181. }
  182. }else{
  183. return true;
  184. }
  185. }
  186. }
  187. }