Session.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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;
  10. use Yii;
  11. use Ramsey\Uuid\Uuid;
  12. use fecshop\services\session\SessionPhp;
  13. //use fecshop\services\session\SessionMongodb;
  14. //use fecshop\services\session\SessionMysqldb;
  15. //use fecshop\services\session\SessionRedis;
  16. /**
  17. * Session services.
  18. * @author Terry Zhao <2358269014@qq.com>
  19. * @since 1.0
  20. */
  21. class Session extends Service
  22. {
  23. // 设置session超时时间
  24. public $timeout;
  25. // 当过期时间 + session创建时间 - 当前时间 < $updateTimeLimit ,则更新session创建时间
  26. public $updateTimeLimit = 600;
  27. public function isUpdateTimeOut($createTime){
  28. return (int)$this->timeout + (int)$createTime - time() < $this->updateTimeLimit;
  29. }
  30. /**
  31. * $storage , $storagePath 为找到当前的storage而设置的配置参数
  32. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  33. */
  34. public $storage = ''; // 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  35. /**
  36. * 设置storage的path路径
  37. * 如果不设置,则系统使用默认路径
  38. * 如果设置了路径,则使用自定义的路径
  39. */
  40. public $storagePath = '';
  41. // 生成的uuid唯一标识码
  42. protected $_uuid;
  43. private $_session;
  44. public $fecshop_uuid = 'fecshop-uuid';
  45. /**
  46. * 1. \Yii::$app->user->enableSession = false;
  47. * 查看是否是false,如果是
  48. *
  49. */
  50. public function init()
  51. {
  52. parent::init();
  53. if (\Yii::$app->user->enableSession == true) {
  54. $this->_session = new SessionPhp; // phpsession
  55. } else {
  56. $currentService = $this->getStorageService($this);
  57. $this->_session = new $currentService();
  58. /*
  59. if ($this->storageEngine == 'mongodb') {
  60. $this->_session = new MongoDbSession;
  61. }else if ($this->storageEngine == 'mysqldb') {
  62. $this->_session = new MysqlDbSession;
  63. }else if ($this->storageEngine == 'redis') {
  64. $this->_session = new RedisSession;
  65. }
  66. */
  67. }
  68. $appName = Yii::$service->helper->getAppName();
  69. $accessTokenTimeoutConfig = Yii::$app->store->get($appName.'_base', 'customer_access_token_timeout');
  70. $accessTokenUpdateTimeLimitConfig = Yii::$app->store->get($appName.'_base', 'customer_access_token_update_time_limit');
  71. $this->timeout = $accessTokenTimeoutConfig ? $accessTokenTimeoutConfig : 86400 ;
  72. $this->updateTimeLimit = $accessTokenUpdateTimeLimitConfig ? $accessTokenUpdateTimeLimitConfig : 600 ;
  73. // var_dump([$this->timeout, $this->updateTimeLimit]);
  74. }
  75. /**
  76. * 访问端:
  77. * api访问接口,返回数据的时候,需要从 response headers 中 读取 uuid
  78. * api 如果获取了uuid,那么下次访问的时候,需要在request header 中附带uuid信息。
  79. *
  80. * 接收端: 也就是下面的函数
  81. * 先从 request headers 读取uuid
  82. * 读取不到,自己生成uuid
  83. * 最后将uuid写入response headers中
  84. */
  85. public function getUUID()
  86. {
  87. if (!$this->_uuid) {
  88. $header = Yii::$app->request->getHeaders();
  89. $uuidName = $this->fecshop_uuid;
  90. // 1.从requestheader里面获取uuid,
  91. if (isset($header[$uuidName]) && !empty($header[$uuidName])) {
  92. $this->_uuid = $header[$uuidName];
  93. } else { // 2.如果获取不到uuid,就生成uuid
  94. $uuid1 = Uuid::uuid1();
  95. $this->_uuid = $uuid1->toString();
  96. }
  97. // 3.把 $this->_uuid 写入到 response 的header里面
  98. Yii::$app->response->getHeaders()->set($uuidName, $this->_uuid);
  99. }
  100. return $this->_uuid;
  101. }
  102. // 刷新uuid(帐号退出后,需要刷新uuid)
  103. public function reflushUUID()
  104. {
  105. $uuid1 = Uuid::uuid1();
  106. $this->_uuid = $uuid1->toString();
  107. Yii::$app->response->getHeaders()->set($this->fecshop_uuid, $this->_uuid);
  108. }
  109. public function set($key, $val, $timeout='')
  110. {
  111. if (!$timeout && (Yii::$app->user->enableSession == false)) {
  112. $timeout = $this->timeout;
  113. }
  114. return $this->_session->set($key, $val, $timeout);
  115. }
  116. public function get($key, $reflush=false)
  117. {
  118. return $this->_session->get($key, $reflush);
  119. }
  120. public function remove($key)
  121. {
  122. return $this->_session->remove($key);
  123. }
  124. public function setFlash($key, $val, $timeout='')
  125. {
  126. if (!$timeout && (Yii::$app->user->enableSession == false)) {
  127. $timeout = $this->timeout;
  128. }
  129. return $this->_session->setFlash($key, $val, $timeout);
  130. }
  131. public function getFlash($key)
  132. {
  133. return $this->_session->getFlash($key);
  134. }
  135. public function destroy()
  136. {
  137. return $this->_session->destroy();
  138. }
  139. }