SiteController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace backend\controllers;
  3. use backend\models\AdminUser;
  4. use backend\models\LoginForm;
  5. use Yii;
  6. use yii\base\ErrorException;
  7. use yii\web\Controller;
  8. use yii\filters\VerbFilter;
  9. use yii\filters\AccessControl;
  10. use yii\web\HttpException;
  11. /**
  12. * Site controller
  13. */
  14. class SiteController extends Controller
  15. {
  16. /**
  17. * @inheritdoc
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'access' => [
  23. 'class' => AccessControl::className(),
  24. 'rules' => [
  25. [
  26. 'actions' => ['login', 'error','init'],
  27. 'allow' => true,
  28. ],
  29. [
  30. 'actions' => ['logout', 'index'],
  31. 'allow' => true,
  32. 'roles' => ['@'],
  33. ],
  34. ],
  35. ],
  36. 'verbs' => [
  37. 'class' => VerbFilter::className(),
  38. 'actions' => [
  39. 'logout' => ['post'],
  40. ],
  41. ],
  42. ];
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function actions()
  48. {
  49. return [
  50. 'error' => [
  51. 'class' => 'yii\web\ErrorAction',
  52. ],
  53. ];
  54. }
  55. /**
  56. * Displays homepage.
  57. *
  58. * @return string
  59. */
  60. public function actionIndex()
  61. {$this->layout = 'hplus';
  62. return $this->render('index');
  63. }
  64. /**
  65. * Login action.
  66. *
  67. * @return string
  68. */
  69. public function actionLogin()
  70. {
  71. $this->layout= 'iframe';
  72. if (!Yii::$app->user->isGuest) {
  73. return $this->goHome();
  74. }
  75. $model = new LoginForm();
  76. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  77. return $this->goBack();
  78. } else {
  79. if(Yii::$app->request->isPost)
  80. Yii::$app->getSession()->setFlash('error','账号或密码错误');
  81. return $this->render('login', [
  82. 'model' => $model,
  83. ]);
  84. }
  85. }
  86. /**
  87. * Logout action.
  88. *
  89. * @return string
  90. */
  91. public function actionLogout()
  92. {
  93. Yii::$app->user->logout();
  94. return $this->goHome();
  95. }
  96. /**
  97. * 初始化后台管理员账号
  98. */
  99. // public function actionInit(){
  100. // $model = AdminUser::find()->one();
  101. // if(empty($model)){
  102. // $model = new AdminUser();
  103. // $model->username = 'admin';
  104. // $model->setPassword('admin');
  105. // $model->generateAuthKey();
  106. // $model->created_at = time();
  107. // $model->updated_at = time();
  108. // if($model->validate()){
  109. // $model->save();
  110. // }else{
  111. // return new HttpException(402,'Initialize account failed ');
  112. // }
  113. //
  114. // }else{
  115. // throw new HttpException(402,'Initialize account failed ');
  116. // }
  117. // }
  118. }