mfcode.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/6
  6. * Time: 下午8:47
  7. */
  8. namespace fcode;
  9. require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
  10. use Exception;
  11. use commonid_helper;
  12. class mfcode
  13. {
  14. private $mParams;
  15. public function __construct($param)
  16. {
  17. if(is_array($param))
  18. {
  19. $this->mParams = $param;
  20. }
  21. elseif(is_numeric($param))
  22. {
  23. $fc_id = intval($param);
  24. if($fc_id <= 0) {
  25. throw new Exception("错误的参数");
  26. }
  27. else
  28. {
  29. $mod_fcode = Model('goods_fcode');
  30. $result = $mod_fcode->find($fc_id);
  31. if(empty($result)) {
  32. throw new Exception("没有这个F码");
  33. } else {
  34. $this->mParams = $result;
  35. }
  36. }
  37. }
  38. elseif(is_string($param))
  39. {
  40. $fc_code = $param;
  41. $mod_fcode = Model('goods_fcode');
  42. $fcode = $mod_fcode->where(array('fc_code' => $fc_code))->find();
  43. if(empty($fcode)) {
  44. throw new Exception("没有这个F码");
  45. } else {
  46. $this->mParams = $fcode;
  47. }
  48. }
  49. else {
  50. throw new Exception("错误的参数");
  51. }
  52. }
  53. public function params() {
  54. return $this->mParams;
  55. }
  56. public function format()
  57. {
  58. $result = [];
  59. $result['fcode_id'] = $this->fc_id();
  60. $gid = commonid_helper::instance()->one_goods($this->commonid());
  61. if($gid == false) return false;
  62. $result['goods_id'] = $gid;
  63. $result['fcode'] = $this->fcode();
  64. $result['key'] = $this->user_key();
  65. $result['usable_time'] = $this->usable_time();
  66. $result['state'] = $this->state();
  67. $result['used'] = $this->used();
  68. $result['expired'] = $this->expired();
  69. return $result;
  70. }
  71. private function state()
  72. {
  73. if($this->used()) return 1;
  74. if($this->expired()) return 2;
  75. if($this->locked()) return 3;
  76. return 0;
  77. }
  78. public function fc_id() {
  79. return intval($this->mParams['fc_id']);
  80. }
  81. public function commonid() {
  82. return intval($this->mParams['goods_commonid']);
  83. }
  84. public function batch_code() {
  85. return ($this->mParams['batch_code']);
  86. }
  87. public function fcode() {
  88. return $this->mParams['fc_code'];
  89. }
  90. public function used() {
  91. return (intval($this->mParams['fc_state']) == 1);
  92. }
  93. public function user_key() {
  94. return $this->mParams['user_key'];
  95. }
  96. public function mobile() {
  97. return $this->mParams['mobile'];
  98. }
  99. public function grabed() {
  100. return intval($this->mParams['grab_state']) == 1;
  101. }
  102. public function binded() {
  103. return intval($this->mParams['grab_state']) == 2;
  104. }
  105. public function usable_time() {
  106. return intval($this->mParams['usable_time']);
  107. }
  108. public function expired()
  109. {
  110. $tm = $this->usable_time();
  111. return ($tm > 0 && $tm <= time());
  112. }
  113. public function locked() {
  114. return (intval($this->mParams['fc_state']) == 3);
  115. }
  116. public function can_use() {
  117. return ($this->commonid() > 0 && $this->expired() == false && $this->used() == false && $this->locked() == false);
  118. }
  119. public function un_used() {
  120. return ($this->commonid() > 0 && $this->expired() == false && $this->used() == false);
  121. }
  122. }