UserLogin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\adminUser;
  10. use Yii;
  11. use fecshop\services\Service;
  12. /**
  13. * AdminUser services. 用来给后台的用户提供数据。
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class UserLogin extends Service
  18. {
  19. protected $_adminUserLoginModelName = '\fecshop\models\mysqldb\adminUser\AdminUserLogin';
  20. protected $_adminUserLoginModel;
  21. public function init()
  22. {
  23. parent::init();
  24. list($this->_adminUserLoginModelName, $this->_adminUserLoginModel) = \Yii::mapGet($this->_adminUserLoginModelName);
  25. }
  26. /**
  27. * @param $data|array
  28. * 数组格式:['username'=>'xxx@xxx.com','password'=>'xxxx']
  29. */
  30. public function actionLogin($data)
  31. {
  32. $model = new $this->_adminUserLoginModelName();
  33. $model->username = $data['username'];
  34. $model->password = $data['password'];
  35. $loginStatus = $model->login();
  36. $errors = $model->errors;
  37. if (!empty($errors)) {
  38. Yii::$service->helper->errors->addByModelErrors($errors);
  39. }
  40. return $loginStatus;
  41. }
  42. /** Appapi 部分使用的函数
  43. * @param $username | String
  44. * @param $password | String
  45. * Appapi 和 第三方进行数据对接部分的用户登陆验证
  46. */
  47. public function loginAndGetAccessToken($username, $password)
  48. {
  49. $header = Yii::$app->request->getHeaders();
  50. if (isset($header['access-token']) && $header['access-token']) {
  51. $accessToken = $header['access-token'];
  52. }
  53. // 如果request header中有access-token,则查看这个 access-token 是否有效
  54. if ($accessToken) {
  55. $identity = Yii::$app->user->loginByAccessToken($accessToken);
  56. if ($identity !== null) {
  57. $access_token_created_at = $identity->access_token_created_at;
  58. $timeout = Yii::$service->session->timeout;
  59. if ($access_token_created_at + $timeout > time()) {
  60. return $accessToken;
  61. }
  62. }
  63. }
  64. // 如果上面access-token不存在
  65. $data = [
  66. 'username' => $username,
  67. 'password' => $password,
  68. ];
  69. if ($this->login($data)) {
  70. $identity = Yii::$app->user->identity;
  71. $identity->generateAccessToken();
  72. $identity->access_token_created_at = time();
  73. $identity->save();
  74. $this->setHeaderAccessToken($identity->access_token);
  75. return $identity->access_token;
  76. }
  77. return null;
  78. }
  79. public function setHeaderAccessToken($accessToken)
  80. {
  81. if ($accessToken) {
  82. Yii::$app->response->getHeaders()->set('access-token', $accessToken);
  83. return true;
  84. }
  85. return false;
  86. }
  87. /** AppServer 部分使用的函数
  88. * @param $type | null or Object
  89. * 从request headers中获取access-token,然后执行登录
  90. * 如果登录成功,然后验证时间是否过期
  91. * 如果不过期,则返回identity
  92. * ** 该方法为appserver用户通过access-token验证需要执行的函数。
  93. */
  94. public function loginByAccessToken($type = null)
  95. {
  96. $header = Yii::$app->request->getHeaders();
  97. if (isset($header['access-token']) && $header['access-token']) {
  98. $accessToken = $header['access-token'];
  99. }
  100. if ($accessToken) {
  101. $identity = Yii::$app->user->loginByAccessToken($accessToken, $type);
  102. if ($identity !== null) {
  103. $access_token_created_at = $identity->access_token_created_at;
  104. $timeout = Yii::$service->session->timeout;
  105. // 如果时间没有过期,则返回identity
  106. if ($access_token_created_at + $timeout > time()) {
  107. //如果时间没有过期,但是快要过期了,在过$updateTimeLimit段时间就要过期,那么更新access_token_created_at。
  108. $updateTimeLimit = Yii::$service->session->updateTimeLimit;
  109. if ($access_token_created_at + $timeout <= (time() + $updateTimeLimit)) {
  110. $identity->access_token_created_at = time();
  111. $identity->save();
  112. }
  113. return $identity;
  114. } else {
  115. $this->logoutByAccessToken();
  116. return false;
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * 通过accessToek的方式,进行登出从操作。
  123. */
  124. public function logoutByAccessToken()
  125. {
  126. $userComponent = Yii::$app->user;
  127. $identity = $userComponent->identity;
  128. if ($identity !== null) {
  129. if (!Yii::$app->user->isGuest) {
  130. $identity->access_token = null;
  131. $identity->access_token_created_at = null;
  132. $identity->save();
  133. }
  134. $userComponent->switchIdentity(null);
  135. }
  136. return $userComponent->getIsGuest();
  137. }
  138. }