fcode.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/7
  6. * Time: 下午5:04
  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. require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
  12. require_once (BASE_ROOT_PATH . '/helper/fcode/mfcode.php');
  13. use fcode\mfcode;
  14. use session_helper;
  15. use commonid_helper;
  16. use Log;
  17. class fcode
  18. {
  19. private $mCodes;
  20. private $mDirty;
  21. private $mFClear;
  22. public function __construct()
  23. {
  24. $this->mCodes = [];
  25. $this->mDirty = false;
  26. $this->mFClear = false;
  27. if(isset($_SESSION['fcodes'])) {
  28. $this->mCodes = $_SESSION['fcodes'];
  29. } else {
  30. $this->onStatus();
  31. }
  32. }
  33. public function __destruct()
  34. {
  35. if($this->mFClear)
  36. {
  37. Log::record("fcode __destruct clear all fcodes.",Log::DEBUG);
  38. if(isset($_SESSION['fcodes'])) {
  39. unset($_SESSION['fcodes']);
  40. }
  41. $this->mDirty = false;
  42. return;
  43. }
  44. if($this->mDirty) {
  45. $_SESSION['fcodes'] = $this->mCodes;
  46. }
  47. $this->mDirty = false;
  48. }
  49. public function onStatus()
  50. {
  51. $this->mCodes = [];
  52. $this->mDirty = true;
  53. $mod_fcode = Model('goods_fcode');
  54. $codes = $mod_fcode->getFcodeList(array('mobile' => session_helper::cur_mobile()));
  55. foreach ($codes as $item)
  56. {
  57. $fcoder = new mfcode($item);
  58. if($fcoder->can_use()) {
  59. $this->mCodes[$fcoder->commonid()][] = $item;
  60. }
  61. }
  62. }
  63. public function goods_has_code($goods_id)
  64. {
  65. $common_id = commonid_helper::instance()->common_id($goods_id);
  66. if($common_id == false) {
  67. return false;
  68. }
  69. return $this->common_has_code($common_id);
  70. }
  71. public function common_has_code($common_id)
  72. {
  73. if(array_key_exists($common_id,$this->mCodes))
  74. {
  75. $total = 0;
  76. $fcodes = $this->mCodes[$common_id];
  77. foreach ($fcodes as $item)
  78. {
  79. $fcoder = new mfcode($item);
  80. if($fcoder->can_use()) {
  81. $total += 1;
  82. }
  83. }
  84. return ($total == 0 ? false : $total);
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. public function fetch($commonid,$num)
  92. {
  93. if($num <= 0) return false;
  94. if(array_key_exists($commonid,$this->mCodes))
  95. {
  96. $result = [];
  97. $fcodes = $this->mCodes[$commonid];
  98. foreach ($fcodes as $item)
  99. {
  100. $fcoder = new mfcode($item);
  101. if($fcoder->can_use())
  102. {
  103. $result[] = $fcoder->fc_id();
  104. if(--$num == 0) {
  105. $this->mFClear = true;
  106. return $result;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. }