|
@@ -0,0 +1,235 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\admin\controller\sys_manage;
|
|
|
+
|
|
|
+use app\admin\validate\sys_manage\MenuValidate;
|
|
|
+use app\common\Tree;
|
|
|
+use app\controller\Curd;
|
|
|
+use app\model\SysMenu;
|
|
|
+use support\Db;
|
|
|
+use support\exception\BusinessException;
|
|
|
+use support\Request;
|
|
|
+use support\Response;
|
|
|
+
|
|
|
+class MenuController extends Curd
|
|
|
+{
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new SysMenu();
|
|
|
+ $this->validate = true;
|
|
|
+ $this->validateClass = new MenuValidate();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc 列表
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/3/12 14:07
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ * @throws \support\exception\BusinessException
|
|
|
+ */
|
|
|
+ public function select(Request $request): Response
|
|
|
+ {
|
|
|
+ [$where, $format, $limit, $field, $order] = $this->selectInput($request);
|
|
|
+ $format = $request->get('format', 'menu_tree');
|
|
|
+ $order = $request->get('order', 'desc');
|
|
|
+ $field = $field ?? 'menu_sort';
|
|
|
+ $query = $this->doSelect($where, $field, $order);
|
|
|
+ return $this->doFormat($query, $format, $limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function save(Request $request)
|
|
|
+ {
|
|
|
+ $params = $request->post();
|
|
|
+ $data = [
|
|
|
+ 'menu_pid' => $params['parentId'],
|
|
|
+ 'menu_icon' => $params['meta']['icon'] ?? '',
|
|
|
+ 'menu_name' => $params['name'],
|
|
|
+ 'menu_title' => $params['meta']['title'],
|
|
|
+ 'menu_uri' => $params['redirect'] ?? '',
|
|
|
+ 'menu_route' => $params['path'],
|
|
|
+ 'menu_component' => $params['component'] ?? '',
|
|
|
+ 'menu_sort' => $params['meta']['sort'] ?? 0,
|
|
|
+ 'menu_is_show' => isset($params['meta']['hidden']) && $params['meta']['hidden'] ? 2 : 1,
|
|
|
+ 'menu_is_menu' => $params['meta']['type'] == 'menu' ? 1 : 2,
|
|
|
+ 'menu_status' => $params['meta']['status'] ?? 2,
|
|
|
+// 'level' => $params['level'],
|
|
|
+ ];
|
|
|
+ $operate = $params['operate'];
|
|
|
+ if ($operate == 'add') {
|
|
|
+ if ($this->validate && !$this->validateClass->scene('add')->check($data)) {
|
|
|
+ return json_fail($this->validateClass->getError());
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $data = $this->inputFilter($data);
|
|
|
+ $this->doInsert($data);
|
|
|
+ } catch (BusinessException $customException) {
|
|
|
+ return json_fail($customException->getMessage());
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ return json_fail('数据写入失败');
|
|
|
+ }
|
|
|
+ return json_success('success');
|
|
|
+ } else if ($operate == 'update') {
|
|
|
+ $data['goods_id'] = $params['id'];
|
|
|
+ $goodsId = $data['goods_id'];
|
|
|
+ if ($this->validate && !$this->validateClass->scene('update')->check($data)) {
|
|
|
+ return json_fail($this->validateClass->getError());
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $data = $this->inputFilter($data);
|
|
|
+ $this->doUpdate($goodsId, $data);
|
|
|
+ } catch (BusinessException $customException) {
|
|
|
+ return json_fail($customException->getMessage());
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return json_fail('数据更新失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('success');
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_fail('请求失败~');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc 执行插入
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/3/12 14:15
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException\
|
|
|
+ */
|
|
|
+ protected function doInsert(array $data)
|
|
|
+ {
|
|
|
+ Db::beginTransaction();
|
|
|
+ try {
|
|
|
+ $primary_key = $this->model->getKeyName();
|
|
|
+ $model_class = get_class($this->model);
|
|
|
+ $model = new $model_class;
|
|
|
+ foreach ($data as $key => $val) {
|
|
|
+ $model->{$key} = $val;
|
|
|
+ }
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $prePath = '/0/';
|
|
|
+ if ($model->menu_pid != 0) {
|
|
|
+ $parentMenu = SysMenu::getParent($model->menu_pid);
|
|
|
+ $prePath = $parentMenu->menu_path;
|
|
|
+ }
|
|
|
+ $model->menu_path = $prePath . $model->menu_id . '/';
|
|
|
+ $model->save();
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollBack();
|
|
|
+ throw new BusinessException('数据写入失败~');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function doFormat($query, $format, $limit): Response
|
|
|
+ {
|
|
|
+ $methods = [
|
|
|
+ 'select' => 'formatSelect',
|
|
|
+ 'tree' => 'formatTree',
|
|
|
+ 'menu_tree' => 'formatMenuTree',
|
|
|
+ 'table_tree' => 'formatTableTree',
|
|
|
+ 'normal' => 'formatNormal',
|
|
|
+ ];
|
|
|
+ $paginator = $query->paginate($limit);
|
|
|
+ $total = $paginator->total();
|
|
|
+ $items = $paginator->items();
|
|
|
+ if (method_exists($this, "afterQuery")) {
|
|
|
+ $items = call_user_func([$this, "afterQuery"], $items);
|
|
|
+ }
|
|
|
+ $format_function = $methods[$format] ?? 'formatNormal';
|
|
|
+ return call_user_func([$this, $format_function], $items, $total);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc 树形输出
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/3/12 14:22
|
|
|
+ *
|
|
|
+ * @param $items
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ protected function formatTree($items): Response
|
|
|
+ {
|
|
|
+ $format_items = [];
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $format_items[] = [
|
|
|
+ 'id' => $item->menu_id,
|
|
|
+ 'pid' => $item->menu_pid,
|
|
|
+ 'name' => $item->menu_name,
|
|
|
+ 'path' => $item->menu_route,
|
|
|
+ 'component' => $item->menu_component,
|
|
|
+ 'meta' => [
|
|
|
+ 'icon' => $item->menu_icon,
|
|
|
+ 'title' => $item->menu_title,
|
|
|
+ 'type' => $item->menu_is_menu == 1 ? 'menu' : 'permission',
|
|
|
+ 'status' => $item->menu_status == 1 ? true : false,
|
|
|
+ 'sort' => $item->menu_sort,
|
|
|
+ 'hidden' => $item->menu_is_show == 1 ? false : true,
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $tree = new Tree($format_items);
|
|
|
+
|
|
|
+ $menu = $tree->getTree();
|
|
|
+ foreach ($menu as &$item) {
|
|
|
+ unset($item['component']);
|
|
|
+ unset($item['redirect']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'dashboardGrid' => ["welcome", "member", "sysManage", "about"],
|
|
|
+ 'menu' => $menu,
|
|
|
+ 'permissions' => ['list.add']
|
|
|
+ ];
|
|
|
+
|
|
|
+ return json_success('success', $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function formatMenuTree($items): Response
|
|
|
+ {
|
|
|
+ $format_items = [];
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $format_items[] = [
|
|
|
+ 'id' => $item->menu_id,
|
|
|
+ 'pid' => $item->menu_pid,
|
|
|
+ 'name' => $item->menu_name,
|
|
|
+ 'path' => $item->menu_route,
|
|
|
+ 'component' => $item->menu_component,
|
|
|
+ 'meta' => [
|
|
|
+ 'icon' => $item->menu_icon,
|
|
|
+ 'title' => $item->menu_title,
|
|
|
+ 'type' => $item->menu_is_menu == 1 ? 'menu' : 'permission',
|
|
|
+ 'status' => $item->menu_status == 1 ? true : false,
|
|
|
+ 'sort' => $item->menu_sort,
|
|
|
+ 'hidden' => $item->menu_is_show == 1 ? false : true,
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $tree = new Tree($format_items);
|
|
|
+
|
|
|
+ $menu = $tree->getTree();
|
|
|
+ foreach ($menu as &$item) {
|
|
|
+ unset($item['component']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('success', $menu);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function myMenu(Request $request)
|
|
|
+ {
|
|
|
+ [$where, $format, $limit, $field, $order] = $this->selectInput($request);
|
|
|
+ $format = $request->get('format', 'tree');
|
|
|
+ $order = $request->get('order', 'desc');
|
|
|
+ $field = $field ?? 'menu_sort';
|
|
|
+ $where['menu_is_show'] = 1;
|
|
|
+ $query = $this->doSelect($where, $field, $order);
|
|
|
+ return $this->doFormat($query, $format, $limit);
|
|
|
+ }
|
|
|
+}
|