IRefill.php 4.5 KB

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