IRefill.php 4.6 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. //[$state, $errmsg, $neterr]
  50. abstract public function add($card_no,$card_type,$amount,$params,&$net_errno = 0);
  51. abstract public function query($refill_info);
  52. abstract public function balance();
  53. //分省相关
  54. public function setRegins($regins) {
  55. $this->mSupportRegins = $regins;
  56. }
  57. public function isRegin(): bool {
  58. return !empty($this->mSupportRegins);
  59. }
  60. public function match_regin($card_type,$regin_no): bool
  61. {
  62. if(empty($this->mSupportRegins) || empty($this->mSupportRegins[$card_type])) {
  63. return true;
  64. }
  65. elseif(in_array(-1,$this->mSupportRegins[$card_type])) {
  66. return true;
  67. }
  68. else {
  69. return in_array($regin_no,$this->mSupportRegins[$card_type]);
  70. }
  71. }
  72. //
  73. public function name() {
  74. return $this->mName;
  75. }
  76. public function store_id() {
  77. return $this->mStoreID;
  78. }
  79. public function official_sn() {
  80. return $this->mOfficialSN === true;
  81. }
  82. public function callback() {
  83. return true;
  84. }
  85. public function refill_type() {
  86. return $this->mRefillType;
  87. }
  88. protected function match_period()
  89. {
  90. $period = $this->mPeriod;
  91. if(empty($period)) {
  92. return true;
  93. }
  94. $cur_time = time();
  95. $date = date('Y-m-d',$cur_time);
  96. $day_start = strtotime("{$date} {$period['start']}");
  97. if(empty($period['end'])) {
  98. $day_end = strtotime("{$date}") + 86400;
  99. }
  100. else {
  101. $day_end = strtotime("{$date} {$period['end']}");
  102. }
  103. return ($cur_time >= $day_start && $cur_time < $day_end);
  104. }
  105. public function amounts()
  106. {
  107. return $this->mAmountTypes;
  108. }
  109. public function goods($quality,int $amount,int $card_type,$regin_no,$other)
  110. {
  111. $maps = $this->mAmountTypes[$quality] ?? [];
  112. $key = "{$amount}-{$card_type}";
  113. if(array_key_exists($key,$maps)) {
  114. $params = $maps[$key];
  115. return [$params['goods_id'], round($params['price'],4)];
  116. }
  117. else {
  118. return [0,0];
  119. }
  120. }
  121. public function setOpened(bool $opened) {
  122. $this->mOpened = $opened;
  123. }
  124. public function opened() {
  125. return $this->mOpened;
  126. }
  127. public function setSort(int $sort) {
  128. $this->mSort = $sort;
  129. }
  130. public function sort() {
  131. return $this->mSort;
  132. }
  133. public function check(int $amount,int $card_type) : array
  134. {
  135. if(!array_key_exists($amount,$this->mAmountTypes)) {
  136. return [false,'额度不支持'];
  137. }
  138. if(!in_array($card_type,$this->mCardType)) {
  139. return [false,'不支持该类型'];
  140. }
  141. if(!$this->match_period()) {
  142. return [false,'不在充值时间范围内'];
  143. }
  144. return [true,'success'];
  145. }
  146. protected function check_empty($value)
  147. {
  148. if (!isset($value))
  149. return true;
  150. if ($value === null)
  151. return true;
  152. if (trim($value) === "")
  153. return true;
  154. return false;
  155. }
  156. }