123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- /**
- * 系统基类控制器文件
- *
- * 主要用于基础验证,初始化数据等
- * @author Andy<370791225@qq.com>
- * @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;
- }
- }
|