favorite.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/5
  6. * Time: 下午10:46
  7. */
  8. namespace user_session;
  9. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  10. require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
  11. use algorithm;
  12. use session_helper;
  13. class favorite
  14. {
  15. static $stTypes = array('goods','brand');
  16. private $mBrands;
  17. private $mGoods;
  18. private $mDirty;
  19. public function __construct()
  20. {
  21. $this->mBrands = [];
  22. $this->mGoods = [];
  23. $this->mDirty = false;
  24. if(isset($_SESSION['favorite']))
  25. {
  26. if(isset($_SESSION['favorite']['brands'])) {
  27. $this->mBrands = $_SESSION['favorite']['brands'];
  28. }
  29. if(isset($_SESSION['favorite']['goods'])) {
  30. $this->mGoods = $_SESSION['favorite']['goods'];
  31. }
  32. }
  33. else {
  34. $this->onLogin();
  35. }
  36. }
  37. public function __destruct()
  38. {
  39. if($this->mDirty) {
  40. $_SESSION['favorite']['brands'] = $this->mBrands;
  41. $_SESSION['favorite']['goods'] = $this->mGoods;
  42. }
  43. }
  44. public function onLogin()
  45. {
  46. $this->mBrands = [];
  47. $this->mGoods = [];
  48. $this->mDirty = true;
  49. $mod_favorites = Model('favorites');
  50. $favorites = $mod_favorites->getGoodsFavoritesList(array('member_id' => $_SESSION['member_id']));
  51. foreach ($favorites as $item)
  52. {
  53. if($item['fav_type'] == 'goods') {
  54. $this->mGoods[] = intval($item['fav_id']);
  55. }
  56. elseif($item['fav_type'] == 'brand') {
  57. $this->mBrands[] = intval($item['fav_id']);
  58. }
  59. else {
  60. continue;
  61. }
  62. }
  63. sort($this->mBrands);
  64. sort($this->mGoods);
  65. }
  66. public function add($type,$id)
  67. {
  68. $id = intval($id);
  69. if($type == 'brand')
  70. {
  71. if(algorithm::binary_search($this->mBrands,$id) == false) {
  72. $pos = algorithm::lower_bonud($this->mBrands,$id);
  73. algorithm::array_insert($this->mBrands,$pos,$id);
  74. $this->mDirty = true;
  75. }
  76. }
  77. elseif($type == 'goods')
  78. {
  79. if(algorithm::binary_search($this->mGoods,$id) == false) {
  80. $pos = algorithm::lower_bonud($this->mGoods,$id);
  81. algorithm::array_insert($this->mGoods,$pos,$id);
  82. $this->mDirty = true;
  83. }
  84. }
  85. }
  86. public function del($type,$id)
  87. {
  88. $id = intval($id);
  89. if($type == 'brand')
  90. {
  91. if(algorithm::binary_search($this->mBrands,$id) == true) {
  92. $pos = algorithm::lower_bonud($this->mBrands,$id);
  93. algorithm::array_erase($this->mBrands,$pos);
  94. $this->mDirty = true;
  95. }
  96. }
  97. elseif($type == 'goods')
  98. {
  99. if(algorithm::binary_search($this->mGoods,$id) == true) {
  100. $pos = algorithm::lower_bonud($this->mGoods,$id);
  101. algorithm::array_erase($this->mGoods,$pos);
  102. $this->mDirty = true;
  103. }
  104. }
  105. }
  106. public function favored_goods($goods_id)
  107. {
  108. if(!session_helper::logined()) return false;
  109. $goods_id = intval($goods_id);
  110. return algorithm::binary_search($this->mGoods,$goods_id);
  111. }
  112. public function favored_brand($brand_id)
  113. {
  114. if(!session_helper::logined()) return false;
  115. $brand_id = intval($brand_id);
  116. return algorithm::binary_search($this->mBrands,$brand_id);
  117. }
  118. }