Module.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace backend\modules\ib;
  3. use backend\models\LoginApi;
  4. use backend\models\MemberIdentity;
  5. use Yii;
  6. /**
  7. * ib module definition class
  8. */
  9. class Module extends \yii\base\Module
  10. {
  11. public $layout = 'main';
  12. /**
  13. * @inheritdoc
  14. */
  15. public $controllerNamespace = 'backend\modules\ib\controllers';
  16. /**
  17. * @inheritdoc
  18. */
  19. public function init()
  20. {
  21. parent::init();
  22. // custom initialization code goes here
  23. Yii::$app->set('user', Yii::$app->get('userIb'));
  24. }
  25. public function beforeAction($action)
  26. {
  27. $id = !empty($_COOKIE[MemberIdentity::IB_LOGIN_ID_COOKIE]) ? trim($_COOKIE[MemberIdentity::IB_LOGIN_ID_COOKIE]) : '';
  28. $password = !empty($_COOKIE[MemberIdentity::IB_LOGIN_TOKEN_COOKIE]) ? trim($_COOKIE[MemberIdentity::IB_LOGIN_TOKEN_COOKIE]) : '';
  29. if ($id && $password) {
  30. $api = new LoginApi();
  31. $result = $api->loginByIdPassword($id, $password);
  32. if ($result['code'] == 1 && isset($result['data']['type']) && $result['data']['type'] == 2) {
  33. $identity = MemberIdentity::findIdentity($id);
  34. Yii::$app->getUser()->login($identity);
  35. }
  36. }
  37. if (Yii::$app->getUser()->getIsGuest()) {
  38. if (!Yii::$app->getRequest()->getIsAjax()) {
  39. Yii::$app->getUser()->loginRequired(false, false);
  40. }
  41. return false;
  42. }
  43. return parent::beforeAction($action);
  44. }
  45. }