menuService = new MenuService(); } /** * 菜单列表 */ public function index() { //////// if (!Request::isPost()) { $other['title'] = '经销商菜单'; $guide['one']['text'] = '经销商管理'; $guide['two']['text'] = '经销商列表'; View::assign('guide', $guide); View::assign('other', $other); View::assign('viewPath', $this->viewPath); return View::fetch('/menu/index'); } else { $order = 'order_id asc,id asc'; $list = $this->menuService->getMenuList($this->tableName, $order); $result['list'] = $list; return $this->successResponse($result); } } /** * 更新菜单显示状态 */ public function updateShowMenu() { header('Content-Type:text/html;charset=utf-8'); if (!Request::isPost()) { return $this->errorResponse(lang('request_method_incorrect')); } try { $id = input('id'); if (!is_numeric($id)) { throw new Exception('参数错误!', 400); } $showMenu = input('show_menu'); $isUpdate = $this->menuService->upShowMenu($this->tableName, (int)$id, $showMenu); if (!$isUpdate) { throw new Exception('更新失败!', 400); } return $this->successResponse('更新成功'); } catch (Exception $e) { return $this->errorResponse($e->getMessage()); } } /* * 获取菜单详情 */ public function getMenuDetail() { try { if (!Request::isPost()) { throw new Exception(lang('request_method_incorrect')); } $id = input('id'); if (!is_numeric($id)) { throw new Exception('参数错误!', 400); } $menuDetail = $this->menuService->getMenuDetail($this->tableName, (int)$id); if (!$menuDetail) { throw new Exception('菜单不存在!', 400); } $result['info'] = $menuDetail; return $this->successResponse($result); } catch (Exception $e) { return $this->errorResponse($e->getMessage()); } } /** * 保存菜单 * @return array|int[] */ public function saveMenu() { if (!Request::isPost()) { return $this->errorResponse(lang('request_method_incorrect')); } //启动事务 Db::startTrans(); try { $data = Request::post(); $id = $data['id']; if (isset($data['langs']) && $data['langs']) { $data['langs'] = json_encode($data['langs'], JSON_UNESCAPED_UNICODE); } if (is_numeric($id)) { //更新数据 $this->menuService->upMenu($this->tableName, (int)$id, $data); $res['message'] = '菜单编辑成功'; } else { unset($data['id']); $this->menuService->addMenu($this->tableName, (int)$id, $data); $res['message'] = '菜单添加成功'; } ///////执行成功,提交事务/////// Db::commit(); return $this->successResponse($res); } catch (Exception $e) { // 回滚事务 Db::rollback(); return $this->errorResponse($e->getMessage()); } } }