From 62e16b52dcfcd429a72e661caef2dbf083c9d23f Mon Sep 17 00:00:00 2001 From: zhaoxiang Date: Tue, 6 Feb 2018 16:45:40 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E4=BB=A3=E7=A0=81=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/behavior/ApiPermission.php | 6 +- application/admin/controller/Base.php | 15 +++ application/admin/controller/Menu.php | 10 ++ application/admin/controller/User.php | 109 +++++++++++++++++++ application/adminRoute.php | 20 ++++ 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 application/admin/controller/User.php diff --git a/application/admin/behavior/ApiPermission.php b/application/admin/behavior/ApiPermission.php index 6a4705e..c7ebf9f 100644 --- a/application/admin/behavior/ApiPermission.php +++ b/application/admin/behavior/ApiPermission.php @@ -8,6 +8,8 @@ namespace app\admin\behavior; +use think\Request; + class ApiPermission { /** @@ -17,7 +19,9 @@ class ApiPermission { * @throws \think\exception\DbException */ public function run() { - + $request = Request::instance(); + $route = $request->routeInfo(); + $route = $route['route']; } diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index 18893ab..0ce50e4 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -43,6 +43,21 @@ class Base extends Controller { return $return; } + /** + * 将二维数组变成指定key + * @param $array + * @param $keyName + * @author zhaoxiang + * @return array + */ + protected function buildArrByNewKey($array, $keyName = 'id') { + $list = array(); + foreach ($array as $item) { + $list[$item[$keyName]] = $item; + } + return $list; + } + protected function debug($data) { if ($data) { $this->debug[] = $data; diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index ba301e2..bcdb710 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -15,6 +15,8 @@ class Menu extends Base { /** * 获取菜单列表 + * @return array + * @throws \think\exception\DbException * @author zhaoxiang */ public function index() { @@ -29,6 +31,7 @@ class Menu extends Base { /** * 新增菜单 + * @return array * @author zhaoxiang */ public function add() { @@ -43,6 +46,7 @@ class Menu extends Base { /** * 菜单状态编辑 + * @return array * @author zhaoxiang */ public function changeStatus() { @@ -61,6 +65,7 @@ class Menu extends Base { /** * 编辑菜单 + * @return array * @author zhaoxiang */ public function edit() { @@ -73,6 +78,11 @@ class Menu extends Base { } } + /** + * 删除菜单 + * @return array + * @author zhaoxiang + */ public function del() { $id = $this->request->get('id'); if (!$id) { diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php new file mode 100644 index 0000000..347b36f --- /dev/null +++ b/application/admin/controller/User.php @@ -0,0 +1,109 @@ + + */ + +namespace app\admin\controller; + + +use app\model\ApiUser; +use app\model\ApiUserData; + +class User extends Base { + + /** + * 获取用户列表 + * @return array + * @author zhaoxiang + */ + public function index() { + $listInfo = ApiUser::all(); + $userData = ApiUserData::all(); + $userData = $this->buildArrByNewKey($userData, 'uid'); + + foreach ($listInfo as $key => $value) { + if ($userData) { + $listInfo[$key]['lastLoginIp'] = long2ip($userData[$value['id']]['lastLoginIp']); + $listInfo[$key]['loginTimes'] = $userData[$value['id']]['loginTimes']; + $listInfo[$key]['lastLoginTime'] = date('Y-m-d H:i:s', $userData[$value['id']]['lastLoginTime']); + } + } + + return $this->buildSuccess([ + 'list' => $listInfo, + 'count' => $count + ], '登录成功'); + } + + /** + * 新增菜单 + * @return array + * @author zhaoxiang + */ + public function add() { + $postData = $this->request->post(); + $res = ApiMenu::create($postData); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + } else { + return $this->buildSuccess([]); + } + } + + /** + * 菜单状态编辑 + * @return array + * @author zhaoxiang + */ + public function changeStatus() { + $id = $this->request->get('id'); + $status = $this->request->get('status'); + $res = ApiMenu::update([ + 'id' => $id, + 'hide' => $status + ]); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + } else { + return $this->buildSuccess([]); + } + } + + /** + * 编辑菜单 + * @return array + * @author zhaoxiang + */ + public function edit() { + $postData = $this->request->post(); + $res = ApiMenu::update($postData); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + } else { + return $this->buildSuccess([]); + } + } + + /** + * 删除菜单 + * @return array + * @author zhaoxiang + */ + public function del() { + $id = $this->request->get('id'); + if (!$id) { + return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); + } + $childNum = ApiMenu::where(['fid' => $id])->count(); + if ($childNum) { + return $this->buildFailed(ReturnCode::INVALID, '当前菜单存在子菜单,不可以被删除!'); + } else { + ApiMenu::destroy($id); + + return $this->buildSuccess([]); + } + } + +} diff --git a/application/adminRoute.php b/application/adminRoute.php index e5177e1..f9c4ec8 100644 --- a/application/adminRoute.php +++ b/application/adminRoute.php @@ -31,6 +31,26 @@ return [ 'admin/Menu/del', ['method' => 'get', 'after_behavior' => $afterBehavior] ], + 'User/index' => [ + 'admin/User/index', + ['method' => 'get', 'after_behavior' => $afterBehavior] + ], + 'User/changeStatus' => [ + 'admin/User/changeStatus', + ['method' => 'get', 'after_behavior' => $afterBehavior] + ], + 'User/add' => [ + 'admin/User/add', + ['method' => 'post', 'after_behavior' => $afterBehavior] + ], + 'User/edit' => [ + 'admin/User/edit', + ['method' => 'post', 'after_behavior' => $afterBehavior] + ], + 'User/del' => [ + 'admin/User/del', + ['method' => 'get', 'after_behavior' => $afterBehavior] + ], '__miss__' => ['admin/Miss/index'], ], ];