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

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

View File

@ -19,6 +19,18 @@ return [
'admin/Menu/changeStatus',
['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'],
],
];