IRefill.php 3.0 KB

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