123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Http\Admin;
- use Txj\Elastic\Facades\ES;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- class RouterController extends AdminBaseController
- {
- protected string $table = 'web_router';
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- $this->isLoginJson();
- }
- /**
- * @param Request $request
- * @return JsonResponse
- */
- public function showList(Request $request)
- {
- $currentPage = $request->post('page', 1);
- $size = $request->post('size', 200);
- $find = ES::table($this->table);
- $find->where(['is_delete' => 0])->orderBy(['weight' => 'asc', 'id' => 'asc']);
- $total = $find->count();
- $list = $find->offset(($currentPage - 1) * $size)->limit($size)->search();
- // 转化为父子关系
- $list = generateToTree($list);
- return responseMessage(1002, 'success', ['list' => $list, 'total' => $total]);
- }
- /**
- * @param Request $request
- * @return JsonResponse
- */
- public function info(Request $request)
- {
- $id = $request->post('id');
- if (empty($id)) {
- return responseMessage(2001, '参数错误!');
- }
- $info = ES::table($this->table)->find($id);
- if ($info) {
- return responseMessage(1002, '', $info);
- } else {
- return responseMessage(2001, '数据不存在,请重试!');
- }
- }
- /**
- * @param Request $request
- * @return JsonResponse
- */
- public function save(Request $request)
- {
- $data = $request->only('id', 'type', 'parent_id', 'title', 'router_path', 'file_path', 'is_redirect', 'redirect_name', 'icon', 'weight');
- if (!isset($data['type']) || empty($data['type'])) {
- return responseMessage(2000, '参数错误type!');
- }
- if ($id = $request->post('id')) {
- // 获取信息
- $info = ES::table($this->table)->find($id);
- if ($info) {
- $isExist = ES::table($this->table)->where('router_path', $data['router_path'])->mustNot(function ($query) use ($id) {
- $query->where('id', $id);
- })->count();
- if ($isExist) {
- return responseMessage(2003, '该路由名称[' . $data['router_path'] . ']已经存在!');
- }
- $isSuccess = ES::table($this->table)->toRefresh()->updateEntityById($id, $data);
- } else {
- return responseMessage(2004, '数据不存在,请重试!');
- }
- } else {
- // 判断路由名称不允许重复
- $isExist = ES::table($this->table)->where('router_path', $data['router_path'])->count();
- if ($isExist) {
- return responseMessage(2005, '该路由名称[' . $data['router_path'] . ']已经存在!');
- }
- if (!isset($data['parent_id']) || empty($data['parent_id'])) {
- $data['parent_id'] = 0;
- $data['level'] = 1;
- } else {
- // 获取父级信息
- $parentInfo = ES::table($this->table)->find($data['parent_id']);
- $data['level'] = $parentInfo['level'] + 1;
- if ($data['level'] > 2) {
- return responseMessage(2006, '最多只允许为三级分类!');
- }
- }
- $isSuccess = ES::table($this->table)->toRefresh()->insert($data);
- }
- if ($isSuccess) {
- return responseMessage(1001, '操作成功!');
- } else {
- return responseMessage(2001, '操作失败!');
- }
- }
- /**
- * 获取菜单路由
- * 转换为vue router的格式
- */
- public function getRouters(): JsonResponse
- {
- if ($this->userId == 1) {
- $list = ES::table($this->table)->where('is_delete', 0)->orderBy(['weight' => 'asc', 'id' => 'asc'])->searchAll();
- } else {
- $roleInfo = ES::table('admin_role')->where(['id' => $this->userInfo['role_id']])->first();
- $privilegesArr = $roleInfo['privileges'];
- if ($privilegesArr) {
- $list = ES::table($this->table)->where('is_delete', 0)->orderBy(['weight' => 'asc', 'id' => 'asc'])->mget($privilegesArr);
- } else {
- $list = [];
- }
- }
- // 数据为空
- if (empty($list)) {
- return responseMessage(1002, '', []);
- }
- $list = generateToTree($list);
- $result = [];
- foreach ($list as $row) {
- $info = [
- 'path' => '/' . trim($row['router_path'], '/'),
- 'name' => $row['title'],
- 'meta' => [
- "auth" => true
- ],
- 'component' => 'BasicLayout',
- ];
- if (isset($row['is_redirect']) && $row['is_redirect']) {
- $info['redirect'] = [
- 'name' => $row['redirect_name']
- ];
- }
- $childrenResult = [];
- if (isset($row['children']) && !empty($row['children'])) {
- $children = $row['children'];
- foreach ($children as $item) {
- $childrenResult[] = [
- 'path' => trim($item['router_path']),
- 'name' => trim($item['router_path']),
- 'meta' => [
- "auth" => true,
- 'title' => $item['title']
- ],
- 'component' => trim($item['file_path'], '/'),
- ];
- }
- }
- if ($childrenResult) {
- $info['children'] = $childrenResult;
- }
- $result[] = $info;
- }
- return responseMessage(1002, '', $result);
- }
- }
|