_childMenu($menu); } private function _childMenu($parentArray){ $menuResult = []; foreach($parentArray as $key => $parentMenu){ // 菜单是否显示 if(isset($parentMenu['show']) && !$parentMenu['show']){ continue; } // 子菜单同样设置 if(isset($parentMenu['child']) && !empty($parentMenu['child'])){ $parentMenu['child'] = $this->_childMenu($parentMenu['child']); } $menuResult[] = $parentMenu; } return $menuResult; } // "visible":true, 是否在菜单可见 // "icon":"ri-computer-line", 图标 // "schemaApi":"/page/home", 拉取页面的地址 // "label":"控制台", 显示的名字 // "leaf":true, 是否有子数据 // "url":"home" 浏览器的url private function _renderMenu($parentArray){ $menuResult = []; foreach($parentArray as $key => $parentMenu){ $temp = []; $hasSon = isset($parentMenu['child']) && !empty($parentMenu['child']) ? true : false; $temp['visible'] = isset($parentMenu['show']) && $parentMenu['show']=="1" ? true : false; $temp['icon'] = isset($parentMenu['icon']) ? $parentMenu['icon'] : ""; $temp['label'] = $parentMenu['name']; $temp['leaf'] = $hasSon;// 是否有子节点 if (!$hasSon) { $temp['url'] = "/".$parentMenu['routePath']; // 浏览器上展示的前端路由 $temp['schemaApi'] = isset($parentMenu['schemaApi']) ? '/v1/oauth/page?code='.$parentMenu['schemaApi'] : ""; // 获取页面的请求.之前没有,之前是请求的后端api请求 }else { if (strpos($parentMenu['routePath'], "/") !== false) { // 如果包含/ , 表示需要展示到页面上 $temp['schemaApi'] = isset($parentMenu['schemaApi']) ? '/v1/oauth/page?code='.$parentMenu['schemaApi'] : ""; // 获取页面的请求.之前没有,之前是请求的后端api请求 } } if(isset($parentMenu['child']) && !empty($parentMenu['child'])){ $temp['children'] = $this->_renderMenu($parentMenu['child']); } $menuResult[] = $temp; } return $menuResult; } public function actionSystemMenu(){ $menu = require Yii::getAlias('@backendApi/config/menu.php'); $menu = $this->_adminChildMenu($menu); // 先校验权限 $menu = $this->_renderMenu($menu);// 返回渲染的格式 $children = [ 'children' => $menu ]; $pages = [ 'pages' => $children ]; $ret = [ 'msg' => 'success', 'data' => $pages, 'status' => 0 ]; return static::notice($ret); } private function _adminChildMenu($parentArray){ $menuResult = []; foreach($parentArray as $key => $parentMenu){ // 菜单是否显示 if(isset($parentMenu['show']) && !$parentMenu['show']){ continue; } // 查看是否有该控制器的权限 if(isset($parentMenu['controller']) && $parentMenu['controller']){ if(!Yii::$app->user->validateAdminController($parentMenu['controller'])) continue; } // 查看是否有权限 if(isset($parentMenu['action']) && $parentMenu['action']){ if(!Yii::$app->user->validateAdminAction($parentMenu['controller'], $parentMenu['action'])) continue; } // 子菜单同样设置 if(isset($parentMenu['child']) && !empty($parentMenu['child'])){ $parentMenu['child'] = $this->_childMenu($parentMenu['child']); } // 如果在白名单的不显示菜单 if(isset($parentMenu['controller']) && $parentMenu['controller']){ if(Yii::$app->user->noCheckAdminController($parentMenu['controller'])) continue; } $menuResult[] = $parentMenu; } return $menuResult; } // 获取页面 public function actionPage(){ $request = Yii::$app->request; $sqlCode = $request->get('code', ''); $pageInfo = PageDetails::getInfoByCode($sqlCode); Yii::$app->response->format = Response::FORMAT_HTML; return $pageInfo['page_json']; } public function actionInfo(){ $userInfo = User::find()->where(['ID'=>Yii::$app->user->id])->asArray()->one(); unset($userInfo['PASSWORD_HASH'], $userInfo['PASSWORD_RESET_TOKEN'], $userInfo['AUTH_KEY']); return static::notice($userInfo); } /** * 登录 * @return mixed * @throws HttpException * @throws \yii\base\Exception */ public function actionLogin() { $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { $token = Yii::$app->getUser()->getToken(); return static::notice($token); } else { $firstError = $model->getFirstError('LoginForm'); if( $firstError === LoginForm::ERROR_IS_MODIFY_PASSWORD ) { return static::notice(LoginForm::ERROR_IS_MODIFY_PASSWORD, 403); } return static::notice(Form::formatErrorsForApi($model->getErrors()), 401); } } public function actionNoLoginModifyPassword() { $form = new AdminForm(); $form->scenario = 'noLoginModifyPassword'; if(Yii::$app->request->isPost && $form->load(Yii::$app->request->post(), '') && $result = $form->edit()){ // Log::adminHandle('管理员'.$result->ADMIN_NAME.'重置密码'); return static::notice('重置密码成功'); } else { return static::notice(Form::formatErrorsForApi($form->getErrors()), 400); } } /** * 用refreshToken刷新accessToken和refreshToken * @return mixed * @throws HttpException */ public function actionRefreshToken(){ $refreshToken = Yii::$app->request->get('refresh-token'); Yii::$app->user->refreshToken($refreshToken); $token = Yii::$app->getUser()->getToken(); if($token){ return static::notice($token); } else { return static::notice('更新Token失败', 401); } } /** * 用refreshToken刷新accessToken * @return mixed * @throws HttpException */ public function actionRefreshAccessToken(){ $refreshToken = Yii::$app->request->get('refresh-token'); Yii::$app->user->refreshAccessToken($refreshToken); $token = Yii::$app->getUser()->getToken(); if($token){ return static::notice($token); } else { return static::notice('更新Token失败', 401); } } /** * 用refreshToken刷新refreshToken * @return mixed * @throws HttpException */ public function actionRefreshRefreshToken(){ $refreshToken = Yii::$app->request->get('refresh-token'); Yii::$app->user->refreshRefreshToken($refreshToken); $token = Yii::$app->getUser()->getToken(); if($token){ return static::notice($token); } else { return static::notice('更新Token失败', 401); } } public function actionTest(){ return static::notice(['test'=>Yii::$app->getUser()->id]); } }