MenuService.php 5.9 KB

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