IRefill.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace refill;
  3. abstract class IRefill
  4. {
  5. protected $mName;
  6. protected $mSotreID;
  7. protected $mRefillType;
  8. protected $mCardType = [];
  9. protected $mAmounts = [];
  10. protected $mPeriod;
  11. public function __construct($cfgs)
  12. {
  13. $this->mName = $cfgs['name'];
  14. $this->mSotreID = $cfgs['store_id'];
  15. $this->mRefillType = $cfgs['refill_type'];
  16. foreach ( $cfgs['amount'] as $k => $v) {
  17. $this->mAmounts[$k] = $v;
  18. }
  19. $this->mPeriod = $cfgs['period'];
  20. }
  21. abstract public function add($card_no,$card_type,$amount,$params);
  22. abstract public function query($refill_info);
  23. public function name() {
  24. return $this->mName;
  25. }
  26. public function store_id() {
  27. return $this->mSotreID;
  28. }
  29. public function refill_type() {
  30. return $this->mRefillType;
  31. }
  32. protected function match_period()
  33. {
  34. $period = $this->mPeriod;
  35. if(empty($period)) {
  36. return true;
  37. }
  38. $cur_time = time();
  39. $date = date('Y-m-d',$cur_time);
  40. $day_start = strtotime("{$date} {$period['start']}");
  41. if(empty($period['end'])) {
  42. $day_end = strtotime("{$date}") + 86400;
  43. }
  44. else {
  45. $day_end = strtotime("{$date} {$period['end']}");
  46. }
  47. return ($cur_time >= $day_start && $cur_time < $day_end);
  48. }
  49. public function amounts()
  50. {
  51. return $this->mAmounts;
  52. }
  53. public function goods(int $amout)
  54. {
  55. if(array_key_exists($amout,$this->mAmounts)) {
  56. $params = $this->mAmounts[$amout];
  57. return [$params['goods_id'], $params['price']];
  58. }
  59. else {
  60. return [];
  61. }
  62. }
  63. public function check(int $amount,int $card_type) : array
  64. {
  65. if(!array_key_exists($amount,$this->mAmounts)) {
  66. return [false,'额度不支持'];
  67. }
  68. if(!in_array($card_type,$this->mCardType)) {
  69. return [false,'不支持该类型'];
  70. }
  71. if(!$this->match_period()) {
  72. return [false,'不在充值时间范围内'];
  73. }
  74. return [true,'success'];
  75. }
  76. protected function check_empty($value)
  77. {
  78. if (!isset($value))
  79. return true;
  80. if ($value === null)
  81. return true;
  82. if (trim($value) === "")
  83. return true;
  84. return false;
  85. }
  86. }