Session.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  69. /**
  70. * 访问端:
  71. * api访问接口,返回数据的时候,需要从 response headers 中 读取 uuid
  72. * api 如果获取了uuid,那么下次访问的时候,需要在request header 中附带uuid信息。
  73. *
  74. * 接收端: 也就是下面的函数
  75. * 先从 request headers 读取uuid
  76. * 读取不到,自己生成uuid
  77. * 最后将uuid写入response headers中
  78. */
  79. public function getUUID()
  80. {
  81. if (!$this->_uuid) {
  82. $header = Yii::$app->request->getHeaders();
  83. $uuidName = $this->fecshop_uuid;
  84. // 1.从requestheader里面获取uuid,
  85. if (isset($header[$uuidName]) && !empty($header[$uuidName])) {
  86. $this->_uuid = $header[$uuidName];
  87. } else { // 2.如果获取不到uuid,就生成uuid
  88. $uuid1 = Uuid::uuid1();
  89. $this->_uuid = $uuid1->toString();
  90. }
  91. // 3.把 $this->_uuid 写入到 response 的header里面
  92. Yii::$app->response->getHeaders()->set($uuidName, $this->_uuid);
  93. }
  94. return $this->_uuid;
  95. }
  96. // 刷新uuid(帐号退出后,需要刷新uuid)
  97. public function reflushUUID()
  98. {
  99. $uuid1 = Uuid::uuid1();
  100. $this->_uuid = $uuid1->toString();
  101. Yii::$app->response->getHeaders()->set($this->fecshop_uuid, $this->_uuid);
  102. }
  103. public function set($key, $val, $timeout='')
  104. {
  105. if (!$timeout && (Yii::$app->user->enableSession == false)) {
  106. $timeout = $this->timeout;
  107. }
  108. return $this->_session->set($key, $val, $timeout);
  109. }
  110. public function get($key, $reflush=false)
  111. {
  112. return $this->_session->get($key, $reflush);
  113. }
  114. public function remove($key)
  115. {
  116. return $this->_session->remove($key);
  117. }
  118. public function setFlash($key, $val, $timeout='')
  119. {
  120. if (!$timeout && (Yii::$app->user->enableSession == false)) {
  121. $timeout = $this->timeout;
  122. }
  123. return $this->_session->setFlash($key, $val, $timeout);
  124. }
  125. public function getFlash($key)
  126. {
  127. return $this->_session->getFlash($key);
  128. }
  129. public function destroy()
  130. {
  131. return $this->_session->destroy();
  132. }
  133. }