diff --git a/application/admin/behavior/ApiAuth.php b/application/admin/behavior/ApiAuth.php index 6c30638..bcde6f7 100644 --- a/application/admin/behavior/ApiAuth.php +++ b/application/admin/behavior/ApiAuth.php @@ -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 - * @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' => []]); } } diff --git a/application/admin/behavior/BuildResponse.php b/application/admin/behavior/BuildResponse.php index 0c768c9..d1416fb 100644 --- a/application/admin/behavior/BuildResponse.php +++ b/application/admin/behavior/BuildResponse.php @@ -12,9 +12,8 @@ class BuildResponse { /** * 返回参数过滤(主要是将返回参数的数据类型给规范) - * @param $response \think\Response + * @param $response * @author zhaoxiang - * @throws \think\exception\DbException */ public function run($response) { $header['Access-Control-Allow-Origin'] = '*'; diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php new file mode 100644 index 0000000..fe857bb --- /dev/null +++ b/application/admin/controller/Menu.php @@ -0,0 +1,119 @@ + + */ + +namespace app\admin\controller; + + +use app\model\ApiMenu; + +class Menu extends Base { + + /** + * 获取菜单列表 + * @author zhaoxiang + */ + public function index() { + $list = ApiMenu::all([]); + $list = json_decode(json_encode($list), true); + $list = formatTree(listToTree($list)); + return $this->buildSuccess($list, '登录成功'); + } + + /** + * 新增菜单 + * @author zhaoxiang + */ + 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 + */ + 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 + */ + 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 + */ + 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('编辑成功'); + } + } + +} diff --git a/application/adminRoute.php b/application/adminRoute.php index 93871ac..50c2216 100644 --- a/application/adminRoute.php +++ b/application/adminRoute.php @@ -1,8 +1,9 @@ [ - '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'], ], ]; diff --git a/application/common.php b/application/common.php index 55d22f2..90de30f 100644 --- a/application/common.php +++ b/application/common.php @@ -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; +} \ No newline at end of file diff --git a/application/model/ApiMenu.php b/application/model/ApiMenu.php new file mode 100644 index 0000000..c6bda67 --- /dev/null +++ b/application/model/ApiMenu.php @@ -0,0 +1,8 @@ +