张国岭 2017-06-26 09:56:21 3027次浏览 1条回复 1 0 0

<?php

namespace app\controllers;

use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; use app\models\EntryForm; use app\models\Mobile; use app\models\User; use yii\web\NotFoundHttpException;

class SiteController extends Controller {

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout'],
            'rules' => [
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post','get'],
            ],
        ],
    ];
}

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        ],
    ];
}

public function actionIndex()
{	
	// 如果未登录,则跳转到登录页面
    if (Yii::$app->user->isGuest)
    {
        return $this->redirect(array('site/login'));
        echo "你好";
    }

    return $this->render('index');
}

public function actionContent()
{
  return $this->render('content');
}

public function actionLogin()
{
   // var_dump(Yii::$app->request->get());
   // die();
   // 
    $cookie = Yii::$app->request->cookies;//注意此处是request
      $language = $cookie->get('uname');
      
      if ($language === null) 
      {
          $value = 0;
      }
      else {
          
        $value = $language->value;
        $identity = User::findByName($value);
        Yii::$app->user->login($identity);
      }

    $this->layout = false;
    if (!\Yii::$app->user->isGuest) {
         return  $this->redirect(array('site/index'));
    //    return $this->goHome();
    }
    $model = new LoginForm();
   // $model->openid = Yii::$app->request->get("id");
   // $model->go = Yii::$app->request->get('go');
   // 
   
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
  
        // if (empty($model->go))
        // {
        // 
            $cookies = Yii::$app->response->cookies;
              $cookies->add(new \yii\web\Cookie([
                'name' => 'uname',
                'value' => $model->uname,
                'expire'=>time()+60*60*24*365,
              ]));

            $this->layout = "main";
            $adid = Yii::$app->user->identity->id;
           return $this->render('index', [
                'adid' => $adid,
            ]); 
          // echo "string"; die();
        // }
        // else
        // {
            //echo 
           // return $this->redirect(Yii::app()->user->returnUrl);
        //}
        
    }
    return $this->render('login', [
        'model' => $model,
    ]);
}

public function actionLogout()
{
    Yii::$app->user->logout();

    //清除cookie
    $cookies = Yii::$app->response->cookies;
    // var_dump($cookies);
    // die();
    $cookies->remove('uname');

    return $this->goHome();
}

public function actionContact()
{
    $model = new ContactForm();
    if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
        Yii::$app->session->setFlash('contactFormSubmitted');

        return $this->refresh();
    }
    return $this->render('contact', [
        'model' => $model,
    ]);
}

public function actionAbout()
{
    $model = new Mobile();

    return $this->render('about');
}

// public function actionSignup() // { // $model = new EntryForm(); // if ($model->load(Yii::$app->request->post())) { // if ($user = $model->signup()) { // // $login = new SiteLoginForm();
// if(Yii::$app->getUser()->login($user)) { // return $this->goHome(); // } // else // { // var_dump($user); // } // } // }

// return $this->render('entry', [ // 'model' => $model, // ]); // } //

public function actionMobile()
{
    $model = new Mobile();
   if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // var_dump($model->mobile);
         // die();
        return $this->redirect(array('/site/entry',"mobile"=>$model->mobile));
      //  return $this->render('entry', ['model' => $modelentry]);

   }
   else
   {
    return $this->render('mobile', ['model' => $model]);
   }
    
}
public function actionEntry()
{
    $model = new EntryForm();
    // var_dump(Yii::$app->request);
    // die();
    // if (!is_null((Yii::$app->request->get('mobile'))))
    // {

    //      $model->mobile =  Yii::$app->request->get('mobile') ;
    // }
      

    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    

        // valid data received in $model

        // do something meaningful here about $model ...
        // 
        if ($user = $model->signup()) {
             if(Yii::$app->getUser()->login($user)) {
               // $this->redirect
               return $this->render('entry-confirm', ['model' => $model]);
             }
              else
            {
               var_dump($user);                }

        }

       
    } else {
        // either the page is initially displayed or there is some validation error
        return $this->render('entry', ['model' => $model]);
    }
}

//登录管理员修改自身密码
public function actionEditpwd()
{
    // var_dump(Yii::$app->request->post());
    // die();
    $id = Yii::$app->user->id;
    $model=  $this->findModel($id);
    // $model = new User();
    $model->scenario="edit_pwd";
    $post = Yii::$app->request->post();
    // if ($post != null)
    // {
    //   var_dump($post);
    //   die();
    // }
    if($post != null){
        // var_dump($model);
        // die();
        // var_dump(Yii::$app->request->post());
        // die();
        $old_pwd = $post['old_pwd'];
        $password=$post['new_password'];
        $password2=$post['new_pwdconfirm'];
        $appsecret = "(uJhu^&8*%#)";
        // $model->setAttribute('id', $id);
        // $model->attributes = $post;
        if ($model->password == md5($old_pwd+$appsecret))
        //if($model->load(Yii::$app->request->post()) && $model->validate())
        {
            // var_dump($model);
            // die();
            if (!empty($password) && !empty($password2) && $password===$password2) {
              
                //$appsecret = "(uJhu^&8*%#)";
                // $model->password = md5($password+$appsecret);
                //$model->password = $model->setPassword($password);
                $res = User::updateAll(array('password'=>md5($password+$appsecret)),'id="'.$id.'"');
            } 
            else
            {
                Yii::$app->getSession()->setFlash('error','请检查输入项');
                return $this->render('rebackPwd',['model'=>$model]);
            } 

            if($res)
            {
                Yii::$app->getSession()->setFlash('status','修改成功,请重新登录');
                //$this->refresh(); 
                return $this->redirect('/site/logout');
            }else{
                // Yii::$app->user->setFlash('error','请检查输入项');
                return $this->render('rebackPwd',['model'=>$model]);
            } 
        }
        else
        {
            // echo 'check_not_ok';
            // die();
            Yii::$app->getSession()->setFlash('error','旧密码输入错误');
            return $this->render('rebackPwd',['model'=>$model]);
        }

        // $model->setAttribute('userid', $id);
        // $model->attributes = $_POST['Admin'];
        
    }
    // else
    // {
    //    Yii::$app->getSession()->setFlash('error','输入内容不能为空'); 
    //   return $this->render('rebackPwd',['model'=>$model]);
    //   // echo 'notok';
    //   // die;
    // }
   return $this->render('rebackPwd',['model'=>$model]);
  //return $this->render('rebackPwd');
}

/**
 * Finds the User model based on its primary key value.
 * If the model is not found, a 404 HTTP exception will be thrown.
 * @param integer $id
 * @return User the loaded model
 * @throws NotFoundHttpException if the model cannot be found
 */
protected function findModel($id)
{
    if (($model = User::findOne($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

}

您需要登录后才可以回复。登录 | 立即注册