fcode.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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($clear = false)
  23. {
  24. $this->mCodes = [];
  25. $this->mDirty = false;
  26. $this->mFClear = $clear;
  27. if(isset($_SESSION['fcodes'])) {
  28. $this->mCodes = $_SESSION['fcodes'];
  29. }
  30. else {
  31. $this->onStatus();
  32. }
  33. }
  34. public function __destruct()
  35. {
  36. if($this->mFClear)
  37. {
  38. Log::record("fcode __destruct clear all fcodes.",Log::DEBUG);
  39. if(isset($_SESSION['fcodes'])) {
  40. unset($_SESSION['fcodes']);
  41. }
  42. $this->mDirty = false;
  43. return;
  44. }
  45. if($this->mDirty) {
  46. $_SESSION['fcodes'] = $this->mCodes;
  47. }
  48. $this->mDirty = false;
  49. }
  50. public function onStatus()
  51. {
  52. $this->mCodes = [];
  53. $this->mDirty = true;
  54. if(session_helper::logined() == false || empty(session_helper::mobile())) {
  55. return;
  56. }
  57. $mod_fcode = Model('goods_fcode');
  58. $fcodes = $mod_fcode->getFcodeList(['mobile' => session_helper::mobile()]);
  59. foreach ($fcodes as $item)
  60. {
  61. $fcoder = new mfcode($item);
  62. if($fcoder->un_used()) {
  63. $this->mCodes[$fcoder->commonid()][] = $item;
  64. }
  65. }
  66. }
  67. public function goods_has_code($goods_id,&$lock_num)
  68. {
  69. $lock_num = 0;
  70. $common_id = commonid_helper::instance()->common_id($goods_id);
  71. if($common_id == false) {
  72. return false;
  73. }
  74. return $this->usable_num($common_id,$lock_num);
  75. }
  76. public function usable_num($common_id,&$lock_num)
  77. {
  78. $lock_num = 0;
  79. if(session_helper::logined() == false) {
  80. return false;
  81. }
  82. if(array_key_exists($common_id,$this->mCodes))
  83. {
  84. $total = 0;
  85. $fcodes = $this->mCodes[$common_id];
  86. foreach ($fcodes as $item)
  87. {
  88. $fcoder = new mfcode($item);
  89. if($fcoder->can_use()) {
  90. $total += 1;
  91. }
  92. if($fcoder->locked()) {
  93. $lock_num += 1;
  94. }
  95. }
  96. return ($total == 0 ? false : $total);
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103. public function fetch($commonid,$num)
  104. {
  105. if($num <= 0) return false;
  106. if(array_key_exists($commonid,$this->mCodes))
  107. {
  108. $result = [];
  109. $fcodes = $this->mCodes[$commonid];
  110. foreach ($fcodes as $item)
  111. {
  112. $fcoder = new mfcode($item);
  113. if($fcoder->can_use())
  114. {
  115. $result[] = $fcoder->fc_id();
  116. if(--$num == 0) {
  117. $this->mFClear = true;
  118. return $result;
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. }