added 添加菜单数据查询

This commit is contained in:
zhaoxiang 2018-01-16 20:26:27 +08:00
parent 689c4a4624
commit c22990f26a
6 changed files with 196 additions and 14 deletions

View File

@ -8,6 +8,7 @@
namespace app\admin\behavior;
use app\util\ReturnCode;
use think\Request;
class ApiAuth {
@ -16,23 +17,20 @@ class ApiAuth {
/**
* 默认行为函数
* @return \think\response\Json
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return \think\Request
* @throws \think\exception\DbException
*/
public function run() {
$request = Request::instance();
$userToken = $request->header('user-token', '');
if ($this->apiInfo['needLogin']) {
if ($userToken) {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少user-token', 'data' => []]);
}
}
$userToken = $request->header('Authorization', '');
if ($userToken) {
$userInfo = cache($userToken);
if (!is_array($userInfo) || !isset($userInfo['passport_uid'])) {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => 'user-token不匹配', 'data' => []]);
$userInfo = json_decode($userInfo, true);
if (!$userInfo || !isset($userInfo['id'])) {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => 'Authorization不匹配', 'data' => []]);
}
} else {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少Authorization', 'data' => []]);
}
}

View File

@ -12,9 +12,8 @@ class BuildResponse {
/**
* 返回参数过滤(主要是将返回参数的数据类型给规范)
* @param $response \think\Response
* @param $response
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @throws \think\exception\DbException
*/
public function run($response) {
$header['Access-Control-Allow-Origin'] = '*';

View File

@ -0,0 +1,119 @@
<?php
/**
*
* @since 2018-01-16
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
namespace app\admin\controller;
use app\model\ApiMenu;
class Menu extends Base {
/**
* 获取菜单列表
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function index() {
$list = ApiMenu::all([]);
$list = json_decode(json_encode($list), true);
$list = formatTree(listToTree($list));
return $this->buildSuccess($list, '登录成功');
}
/**
* 新增菜单
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function add() {
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('添加成功');
}
} 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();
}
}
/**
* 显示菜单
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function open() {
$id = I('post.id');
$res = D('ApiMenu')->where(array('id' => $id))->save(array('hide' => 0));
if ($res === false) {
$this->ajaxError('操作失败');
} else {
$this->ajaxSuccess('添加成功');
}
}
/**
* 隐藏菜单
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function close() {
$id = I('post.id');
$res = D('ApiMenu')->where(array('id' => $id))->save(array('hide' => 1));
if ($res === false) {
$this->ajaxError('操作失败');
} else {
$this->ajaxSuccess('添加成功');
}
}
/**
* 编辑菜单
* @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('编辑成功');
}
}
}
public function del() {
$id = I('post.id');
$childNum = D('ApiMenu')->where(array('fid' => $id))->count();
if ($childNum) {
$this->ajaxError("当前菜单存在子菜单,不可以被删除!");
} else {
D('ApiMenu')->where(array('id' => $id))->delete();
$this->ajaxSuccess('编辑成功');
}
}
}

View File

@ -1,8 +1,9 @@
<?php
$afterBehavior = ['\app\admin\behavior\ApiAuth', '\app\admin\behavior\ApiPermission'];
return [
'[admin]' => [
'Login/index' => [
'Login/index' => [
'admin/Login/index',
['method' => 'post']
],
@ -10,6 +11,10 @@ return [
'admin/Login/logout',
['method' => 'get']
],
'__miss__' => ['admin/Miss/index'],
'Menu/index' => [
'admin/Menu/index',
['method' => 'get', 'after_behavior' => $afterBehavior]
],
'__miss__' => ['admin/Miss/index'],
],
];

View File

@ -10,3 +10,56 @@
// +----------------------------------------------------------------------
// 应用公共文件
/**
* 把返回的数据集转换成Tree
* @param $list
* @param string $pk
* @param string $pid
* @param string $child
* @param string $root
* @return array
*/
function listToTree($list, $pk='id', $pid = 'fid', $child = '_child', $root = '0') {
$tree = array();
if(is_array($list)) {
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data) {
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = &$list[$key];
}else{
if (isset($refer[$parentId])) {
$parent = &$refer[$parentId];
$parent[$child][] = &$list[$key];
}
}
}
}
return $tree;
}
function formatTree($list, $lv = 0, $title = 'name'){
$formatTree = array();
foreach($list as $key => $val){
$title_prefix = '';
for( $i=0;$i<$lv;$i++ ){
$title_prefix .= "|---";
}
$val['lv'] = $lv;
$val['namePrefix'] = $lv == 0 ? '' : $title_prefix;
$val['showName'] = $lv == 0 ? $val[$title] : $title_prefix.$val[$title];
if(!array_key_exists('_child', $val)){
array_push($formatTree, $val);
}else{
$child = $val['_child'];
unset($val['_child']);
array_push($formatTree, $val);
$middle = formatTree($child, $lv+1, $title); //进行下一层递归
$formatTree = array_merge($formatTree, $middle);
}
}
return $formatTree;
}

View File

@ -0,0 +1,8 @@
<?php
namespace app\model;
class ApiMenu extends Base {
//
}