RouterController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Http\Admin;
  3. use Txj\Elastic\Facades\ES;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Request;
  6. class RouterController extends AdminBaseController
  7. {
  8. protected string $table = 'web_router';
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->setWebsite(__NAMESPACE__);
  13. $this->isLoginJson();
  14. }
  15. /**
  16. * @param Request $request
  17. * @return JsonResponse
  18. */
  19. public function showList(Request $request)
  20. {
  21. $currentPage = $request->post('page', 1);
  22. $size = $request->post('size', 200);
  23. $find = ES::table($this->table);
  24. $find->where(['is_delete' => 0])->orderBy(['weight' => 'asc', 'id' => 'asc']);
  25. $total = $find->count();
  26. $list = $find->offset(($currentPage - 1) * $size)->limit($size)->search();
  27. // 转化为父子关系
  28. $list = generateToTree($list);
  29. return responseMessage(1002, 'success', ['list' => $list, 'total' => $total]);
  30. }
  31. /**
  32. * @param Request $request
  33. * @return JsonResponse
  34. */
  35. public function info(Request $request)
  36. {
  37. $id = $request->post('id');
  38. if (empty($id)) {
  39. return responseMessage(2001, '参数错误!');
  40. }
  41. $info = ES::table($this->table)->find($id);
  42. if ($info) {
  43. return responseMessage(1002, '', $info);
  44. } else {
  45. return responseMessage(2001, '数据不存在,请重试!');
  46. }
  47. }
  48. /**
  49. * @param Request $request
  50. * @return JsonResponse
  51. */
  52. public function save(Request $request)
  53. {
  54. $data = $request->only('id', 'type', 'parent_id', 'title', 'router_path', 'file_path', 'is_redirect', 'redirect_name', 'icon', 'weight');
  55. if (!isset($data['type']) || empty($data['type'])) {
  56. return responseMessage(2000, '参数错误type!');
  57. }
  58. if ($id = $request->post('id')) {
  59. // 获取信息
  60. $info = ES::table($this->table)->find($id);
  61. if ($info) {
  62. $isExist = ES::table($this->table)->where('router_path', $data['router_path'])->mustNot(function ($query) use ($id) {
  63. $query->where('id', $id);
  64. })->count();
  65. if ($isExist) {
  66. return responseMessage(2003, '该路由名称[' . $data['router_path'] . ']已经存在!');
  67. }
  68. $isSuccess = ES::table($this->table)->toRefresh()->updateEntityById($id, $data);
  69. } else {
  70. return responseMessage(2004, '数据不存在,请重试!');
  71. }
  72. } else {
  73. // 判断路由名称不允许重复
  74. $isExist = ES::table($this->table)->where('router_path', $data['router_path'])->count();
  75. if ($isExist) {
  76. return responseMessage(2005, '该路由名称[' . $data['router_path'] . ']已经存在!');
  77. }
  78. if (!isset($data['parent_id']) || empty($data['parent_id'])) {
  79. $data['parent_id'] = 0;
  80. $data['level'] = 1;
  81. } else {
  82. // 获取父级信息
  83. $parentInfo = ES::table($this->table)->find($data['parent_id']);
  84. $data['level'] = $parentInfo['level'] + 1;
  85. if ($data['level'] > 2) {
  86. return responseMessage(2006, '最多只允许为三级分类!');
  87. }
  88. }
  89. $isSuccess = ES::table($this->table)->toRefresh()->insert($data);
  90. }
  91. if ($isSuccess) {
  92. return responseMessage(1001, '操作成功!');
  93. } else {
  94. return responseMessage(2001, '操作失败!');
  95. }
  96. }
  97. /**
  98. * 获取菜单路由
  99. * 转换为vue router的格式
  100. */
  101. public function getRouters(): JsonResponse
  102. {
  103. if ($this->userId == 1) {
  104. $list = ES::table($this->table)->where('is_delete', 0)->orderBy(['weight' => 'asc', 'id' => 'asc'])->searchAll();
  105. } else {
  106. $roleInfo = ES::table('admin_role')->where(['id' => $this->userInfo['role_id']])->first();
  107. $privilegesArr = $roleInfo['privileges'];
  108. if ($privilegesArr) {
  109. $list = ES::table($this->table)->where('is_delete', 0)->orderBy(['weight' => 'asc', 'id' => 'asc'])->mget($privilegesArr);
  110. } else {
  111. $list = [];
  112. }
  113. }
  114. // 数据为空
  115. if (empty($list)) {
  116. return responseMessage(1002, '', []);
  117. }
  118. $list = generateToTree($list);
  119. $result = [];
  120. foreach ($list as $row) {
  121. $info = [
  122. 'path' => '/' . trim($row['router_path'], '/'),
  123. 'name' => $row['title'],
  124. 'meta' => [
  125. "auth" => true
  126. ],
  127. 'component' => 'BasicLayout',
  128. ];
  129. if (isset($row['is_redirect']) && $row['is_redirect']) {
  130. $info['redirect'] = [
  131. 'name' => $row['redirect_name']
  132. ];
  133. }
  134. $childrenResult = [];
  135. if (isset($row['children']) && !empty($row['children'])) {
  136. $children = $row['children'];
  137. foreach ($children as $item) {
  138. $childrenResult[] = [
  139. 'path' => trim($item['router_path']),
  140. 'name' => trim($item['router_path']),
  141. 'meta' => [
  142. "auth" => true,
  143. 'title' => $item['title']
  144. ],
  145. 'component' => trim($item['file_path'], '/'),
  146. ];
  147. }
  148. }
  149. if ($childrenResult) {
  150. $info['children'] = $childrenResult;
  151. }
  152. $result[] = $info;
  153. }
  154. return responseMessage(1002, '', $result);
  155. }
  156. }