IRefill.php 4.5 KB

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