IRefill.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace refill;
  3. use mtopcard;
  4. abstract class IRefill
  5. {
  6. protected $mName;
  7. protected $mStoreID;
  8. protected $mRefillType;
  9. protected $mOfficialSN;
  10. protected $mAmountTypes = [];
  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. $card_types = function ($stypes)
  20. {
  21. $result = [];
  22. $types = explode(',',$stypes);
  23. foreach ($types as $stype) {
  24. $type = mtopcard\topcard_type($stype);
  25. $result[] = $type;
  26. }
  27. return $result;
  28. };
  29. foreach ( $cfgs['amount'] as $spec => $items)
  30. {
  31. foreach ($items as $gitem)
  32. {
  33. $quality = $gitem['quality'] ?? 1;
  34. $types = $card_types($gitem['card_type']);
  35. foreach ($types as $type) {
  36. $this->mAmountTypes[$quality]["{$spec}-{$type}"] = ['goods_id' => $gitem['goods_id'],'price' => $gitem['price']];
  37. }
  38. }
  39. }
  40. $this->mPeriod = $cfgs['period'];
  41. $this->mOfficialSN = $cfgs['official_sn'] ?? false;
  42. $this->mOpened = true;
  43. $this->mSort = 65536;
  44. }
  45. abstract public function add($card_no,$card_type,$amount,$params);
  46. abstract public function query($refill_info);
  47. public function name() {
  48. return $this->mName;
  49. }
  50. public function store_id() {
  51. return $this->mStoreID;
  52. }
  53. public function official_sn() {
  54. return $this->mOfficialSN === true;
  55. }
  56. public function callback() {
  57. return true;
  58. }
  59. public function refill_type() {
  60. return $this->mRefillType;
  61. }
  62. protected function match_period()
  63. {
  64. $period = $this->mPeriod;
  65. if(empty($period)) {
  66. return true;
  67. }
  68. $cur_time = time();
  69. $date = date('Y-m-d',$cur_time);
  70. $day_start = strtotime("{$date} {$period['start']}");
  71. if(empty($period['end'])) {
  72. $day_end = strtotime("{$date}") + 86400;
  73. }
  74. else {
  75. $day_end = strtotime("{$date} {$period['end']}");
  76. }
  77. return ($cur_time >= $day_start && $cur_time < $day_end);
  78. }
  79. public function amounts()
  80. {
  81. return $this->mAmountTypes;
  82. }
  83. public function goods($quality,int $amount,int $card_type)
  84. {
  85. $maps = $this->mAmountTypes[$quality] ?? [];
  86. $key = "{$amount}-{$card_type}";
  87. if(array_key_exists($key,$maps)) {
  88. $params = $maps[$key];
  89. return [$params['goods_id'], round($params['price'],2)];
  90. }
  91. else {
  92. return [];
  93. }
  94. }
  95. public function setOpened(bool $opened) {
  96. $this->mOpened = $opened;
  97. }
  98. public function opened() {
  99. return $this->mOpened;
  100. }
  101. public function setSort(int $sort) {
  102. $this->mSort = $sort;
  103. }
  104. public function sort() {
  105. return $this->mSort;
  106. }
  107. public function check(int $amount,int $card_type) : array
  108. {
  109. if(!array_key_exists($amount,$this->mAmountTypes)) {
  110. return [false,'额度不支持'];
  111. }
  112. if(!in_array($card_type,$this->mCardType)) {
  113. return [false,'不支持该类型'];
  114. }
  115. if(!$this->match_period()) {
  116. return [false,'不在充值时间范围内'];
  117. }
  118. return [true,'success'];
  119. }
  120. protected function check_empty($value)
  121. {
  122. if (!isset($value))
  123. return true;
  124. if ($value === null)
  125. return true;
  126. if (trim($value) === "")
  127. return true;
  128. return false;
  129. }
  130. }