storage.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/24
  6. * Time: 上午11:03
  7. */
  8. namespace user_session;
  9. use algorithm;
  10. abstract class storage
  11. {
  12. const NORMAL_SUPPORT = 0;
  13. const DAILY_SUPPORT = 1;
  14. private $mLimitType;
  15. private $mTag;
  16. private $mCurStorage; //PHP 引用不能多次传递,用类成员变量临时保存
  17. public function __construct()
  18. {
  19. $this->mLimitType = $this->limit_type();
  20. $this->mTag = $this->storage_tag();
  21. $this->mCurStorage = null;
  22. }
  23. public function click($uniqueid)
  24. {
  25. $uniqueid = intval($uniqueid);
  26. if($uniqueid < 0) {
  27. return false;
  28. }
  29. if($this->base_supported($uniqueid)) {
  30. $supported = false;
  31. $this->base_unsupport($uniqueid);
  32. } else {
  33. $supported = true;
  34. $this->base_support($uniqueid);
  35. }
  36. return $supported;
  37. }
  38. private function session_data()
  39. {
  40. if(!isset($_SESSION[$this->mTag])) {
  41. $_SESSION[$this->mTag] = [];
  42. }
  43. if($this->mLimitType == self::NORMAL_SUPPORT) {
  44. $this->mCurStorage = &$_SESSION[$this->mTag];
  45. }
  46. else
  47. {
  48. $time = strtotime(date('Y-m-d',time()));
  49. if(!isset($_SESSION[$this->mTag][$time])) {
  50. $_SESSION[$this->mTag][$time] = [];
  51. }
  52. $this->mCurStorage = &$_SESSION[$this->mTag][$time];
  53. }
  54. }
  55. protected function base_supported($uniqueid)
  56. {
  57. $this->session_data();
  58. if(algorithm::binary_search($this->mCurStorage,$uniqueid)) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. protected function base_support($uniqueid)
  65. {
  66. $this->session_data();
  67. if(algorithm::binary_search($this->mCurStorage,$uniqueid) == false) {
  68. $pos = algorithm::lower_bonud($this->mCurStorage,$uniqueid);
  69. algorithm::array_insert($this->mCurStorage,$pos,$uniqueid);
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. protected function base_unsupport($uniqueid)
  76. {
  77. $this->session_data();
  78. if(algorithm::binary_search($this->mCurStorage,$uniqueid) == true) {
  79. $pos = algorithm::lower_bonud($this->mCurStorage,$uniqueid);
  80. algorithm::array_erase($this->mCurStorage,$pos);
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. abstract protected function limit_type();
  87. abstract protected function storage_tag();
  88. }
  89. class friend_caller extends storage
  90. {
  91. public function __construct()
  92. {
  93. parent::__construct();
  94. }
  95. public function limit_type()
  96. {
  97. return storage::DAILY_SUPPORT;;
  98. }
  99. public function storage_tag()
  100. {
  101. return 'friend_caller';
  102. }
  103. public function called($user) {
  104. return $this->base_supported($user);
  105. }
  106. public function call($user) {
  107. parent::base_support($user);
  108. }
  109. }