You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 lines
4.0 KiB

  1. <?php
  2. namespace backend\controllers;
  3. use common\models\LoginForm;
  4. use Yii;
  5. use yii\web\Controller;
  6. use yii\filters\AccessControl;
  7. use yii\web\HttpException;
  8. /**
  9. * Site controller
  10. */
  11. class SiteController extends Controller
  12. {
  13. public $enableCsrfValidation = false;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function behaviors()
  18. {
  19. return [
  20. 'access' => [
  21. 'class' => AccessControl::className(),
  22. 'rules' => [
  23. [
  24. 'actions' => ['login', 'error', 'logout'],
  25. 'allow' => true,
  26. ],
  27. [
  28. 'actions' => ['logout', 'index', 'logout-yii'],
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. ],
  32. ],
  33. ],
  34. ];
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function actions()
  40. {
  41. return [
  42. // 'error' => [
  43. // 'class' => 'yii\web\ErrorAction',
  44. // 'view' => '@backend/views/site/error.php'
  45. // ],
  46. ];
  47. }
  48. /**
  49. * Displays homepage.
  50. *
  51. * @return string
  52. */
  53. public function actionIndex()
  54. {
  55. return $this->goHome();
  56. }
  57. /**
  58. * Login action.
  59. *
  60. * @return string
  61. */
  62. public function actionLogin()
  63. {
  64. $uri = explode('.', $_SERVER['HTTP_HOST']);
  65. $sub_domain = $uri[0];
  66. if ($sub_domain !== 'hotel') { //域名不是酒店,都跳转到cs登录
  67. return $this->redirect('http://' . CS_DOMAIN);
  68. } else {
  69. $this->layout = 'full';
  70. if (!Yii::$app->user->isGuest) {
  71. return $this->goHome();
  72. }
  73. $model = new LoginForm();
  74. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  75. $cookies = Yii::$app->response->cookies;
  76. $cookies->add(new \yii\web\Cookie([
  77. 'name' => 'home_page_show',
  78. 'value' => 'hide',
  79. ]));
  80. $cookies->add(new \yii\web\Cookie([
  81. 'name' => 'user_id',
  82. 'value' => Yii::$app->user->id,
  83. ]));
  84. return $this->goHome();
  85. } else {
  86. if(Yii::$app->request->isPost)
  87. Yii::$app->session->setFlash('error', $model->getFirstErrors());
  88. return $this->render('login', [
  89. 'model' => $model,
  90. ]);
  91. }
  92. }
  93. }
  94. /**
  95. * Logout action.
  96. * 退出后,全部跳转到cs的登录页
  97. *
  98. * @return string|null
  99. */
  100. public function actionLogout($cs = '')
  101. {
  102. if(Yii::$app->user->identity != null)
  103. Yii::$app->user->logout();
  104. $session = Yii::$app->session;
  105. $session->destroy();
  106. if($cs === '')
  107. return $this->redirect('http://' . CS_DOMAIN);
  108. else{
  109. return null;
  110. }
  111. }
  112. public function actionLogoutYii()
  113. {
  114. Yii::$app->user->logout();
  115. $session = Yii::$app->session;
  116. $session->destroy();
  117. return $this->goHome();
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function actionError()
  123. {
  124. $this->layout = "@backend/modules/motorcade/views/layouts/iframe";
  125. $exception = Yii::$app->errorHandler->exception;
  126. if ($exception !== null) {
  127. //无权限的页面,其他错误不显示
  128. /* @var $exception HttpException */
  129. $code = $exception->statusCode;
  130. if ($code == 403) {
  131. $viewFile = 'error403';
  132. } else {
  133. $viewFile = 'error';
  134. }
  135. if (Yii::$app->request->isPjax || Yii::$app->request->isAjax) {
  136. return $this->renderPartial($viewFile . 'Pjax', ['exception' => $exception]);
  137. } else
  138. return $this->render($viewFile, ['exception' => $exception]);
  139. }
  140. return '';
  141. }
  142. }