123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace backend\controllers;
- use backend\models\AdminUser;
- use backend\models\LoginForm;
- use Yii;
- use yii\base\ErrorException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use yii\web\HttpException;
- /**
- * Site controller
- */
- class SiteController extends Controller
- {
-
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['login', 'error','init'],
- 'allow' => true,
- ],
- [
- 'actions' => ['logout', 'index'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- ];
- }
- /**
- * Displays homepage.
- *
- * @return string
- */
- public function actionIndex()
- {$this->layout = 'hplus';
- return $this->render('index');
- }
- /**
- * Login action.
- *
- * @return string
- */
- public function actionLogin()
- {
- $this->layout= 'iframe';
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- } else {
- if(Yii::$app->request->isPost)
- Yii::$app->getSession()->setFlash('error','账号或密码错误');
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Logout action.
- *
- * @return string
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * 初始化后台管理员账号
- */
- // public function actionInit(){
- // $model = AdminUser::find()->one();
- // if(empty($model)){
- // $model = new AdminUser();
- // $model->username = 'admin';
- // $model->setPassword('admin');
- // $model->generateAuthKey();
- // $model->created_at = time();
- // $model->updated_at = time();
- // if($model->validate()){
- // $model->save();
- // }else{
- // return new HttpException(402,'Initialize account failed ');
- // }
- //
- // }else{
- // throw new HttpException(402,'Initialize account failed ');
- // }
- // }
- }
|