added 新增删除菜单的功能

This commit is contained in:
zhaoxiang 2018-02-04 23:58:47 +08:00
parent 65c5d11c63
commit 2f07c37e21
2 changed files with 32 additions and 44 deletions

View File

@ -33,26 +33,11 @@ class Menu extends Base {
*/ */
public function add() { public function add() {
$postData = $this->request->post(); $postData = $this->request->post();
if (IS_POST) { $res = ApiMenu::create($postData);
$data = I('post.');
$data['hide'] = isset($data['hide']) ? 1 : 0;
$res = D('ApiMenu')->add($data);
if ($res === false) { if ($res === false) {
$this->ajaxError('操作失败'); return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else { } else {
$this->ajaxSuccess('添加成功'); return $this->buildSuccess([]);
}
} else {
$originList = D('ApiMenu')->order('sort asc')->select();
$fid = '';
$id = I('get.id');
if (!empty($id)) {
$fid = $id;
}
$options = array_column(formatTree(listToTree($originList)), 'showName', 'id');
$this->assign('options', $options);
$this->assign('fid', $fid);
$this->display();
} }
} }
@ -79,35 +64,26 @@ class Menu extends Base {
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function edit() { public function edit() {
if (IS_GET) { $postData = $this->request->post();
$originList = D('ApiMenu')->order('sort asc')->select(); $res = ApiMenu::update($postData);
$list = $this->buildArrByNewKey($originList);
$listInfo = $list[I('get.id')];
$options = array_column(formatTree(listToTree($originList)), 'showName', 'id');
$this->assign('detail', $listInfo);
$this->assign('options', $options);
$this->display('add');
} elseif (IS_POST) {
$postData = I('post.');
$postData['hide'] = isset($postData['hide']) ? 1 : 0;
$res = D('ApiMenu')->where(array('id' => $postData['id']))->save($postData);
if ($res === false) { if ($res === false) {
$this->ajaxError('操作失败'); return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else { } else {
$this->ajaxSuccess('编辑成功'); return $this->buildSuccess([]);
}
} }
} }
public function del() { public function del() {
$id = I('post.id'); $id = $this->request->get('id');
$childNum = D('ApiMenu')->where(array('fid' => $id))->count(); if (!$id) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
}
$childNum = ApiMenu::where(['fid' => $id])->count();
if ($childNum) { if ($childNum) {
$this->ajaxError("当前菜单存在子菜单,不可以被删除!"); return $this->buildFailed(ReturnCode::INVALID, '当前菜单存在子菜单,不可以被删除!');
} else { } else {
D('ApiMenu')->where(array('id' => $id))->delete(); ApiMenu::destroy($id);
$this->ajaxSuccess('编辑成功'); return $this->buildSuccess([]);
} }
} }

View File

@ -19,6 +19,18 @@ return [
'admin/Menu/changeStatus', 'admin/Menu/changeStatus',
['method' => 'get', 'after_behavior' => $afterBehavior] ['method' => 'get', 'after_behavior' => $afterBehavior]
], ],
'Menu/add' => [
'admin/Menu/add',
['method' => 'post', 'after_behavior' => $afterBehavior]
],
'Menu/edit' => [
'admin/Menu/edit',
['method' => 'post', 'after_behavior' => $afterBehavior]
],
'Menu/del' => [
'admin/Menu/del',
['method' => 'get', 'after_behavior' => $afterBehavior]
],
'__miss__' => ['admin/Miss/index'], '__miss__' => ['admin/Miss/index'],
], ],
]; ];