* @date 2019-06-20 10:00:00 */ namespace app\controllers; use app\models\Admin; use Yii; use yii\web\Controller; class BaseController extends CController { // 无需登录验证白名单 static $guestAcl = [ 'login/index', 'login/captcha', 'login/password', 'api/login' ]; public function beforeAction($action) { $presentRequestUrl = Yii::$app->request->getPathInfo(); if (Yii::$app->user->isGuest) {// 用户未登录操作 if (in_array($presentRequestUrl, self::$guestAcl)) { return true; } if(isset(Yii::$app->session['user'])){ return true; } //判断token $token=''; $getUserId=0; if(Yii::$app->request->get('sig')){ $token=Yii::$app->request->get('sig'); $getUserId=Yii::$app->request->get('user_id'); }elseif(Yii::$app->request->post('sig')){ $token=Yii::$app->request->post('sig'); $getUserId=Yii::$app->request->post('user_id'); } if($token && $getUserId){ $user=Admin::find()->where('token=:token',[':token'=>$token])->asArray()->one(); if(!$user){ echo 'error'; return false; }else{ if($user['user_id']!=$getUserId){ echo 'error'; return false; } $data=[ 'user_id' => $user['user_id'], 'username' => $user['user_name'], 'real_name' => $user['real_name'], 'role'=>$user['role'], ]; Yii::$app->session['user'] = $data; return true; } } if (Yii::$app->request->isAjax) { echo 'error'; return false; } else { return $this->redirect(['login/index'])->send(); } } else { return true; } } //提示信息并跳转 protected function alert($status,$msg){ $url=Yii::$app->request->getReferrer(); return $this->renderPartial('../error/error',['message'=>$msg,'status'=>$status,'gotoUrl'=>$url]); } /** * 初始化接收参数 * @return array */ protected static function getRequest() { $requestData = []; $request = Yii::$app->request; if ($request->isGet && $request->get()) { $requestData = $request->get(); } elseif ($request->isPost && $request->post()) { $requestData = $request->post(); } elseif (file_get_contents('php://input')) { $requestData = json_decode(file_get_contents('php://input'), true); } return $requestData; } }