123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace refill;
- use mtopcard;
- abstract class IRefill
- {
- protected $mName;
- public $mStoreID;
- protected $mRefillType;
- protected $mOfficialSN;
- protected $mAmountTypes = [];
- protected $mPeriod;
- protected $mOpened;
- protected $mSort;
- protected $mSupportRegins;
- public function __construct($cfgs)
- {
- if(empty($cfgs)) return;
- $this->mName = $cfgs['name'];
- $this->mStoreID = $cfgs['store_id'];
- $this->mRefillType = $cfgs['refill_type'];
- $this->mSupportRegins = [];
- $card_types = function ($stypes)
- {
- $result = [];
- $types = explode(',',$stypes);
- foreach ($types as $stype) {
- $type = mtopcard\topcard_type($stype);
- $result[] = $type;
- }
- return $result;
- };
- foreach ( $cfgs['amount'] as $spec => $items)
- {
- foreach ($items as $gitem)
- {
- $quality = $gitem['quality'] ?? 1;
- $types = $card_types($gitem['card_type']);
- foreach ($types as $type) {
- $this->mAmountTypes[$quality]["{$spec}-{$type}"] = ['goods_id' => $gitem['goods_id'],'price' => $gitem['price']];
- }
- }
- }
- $this->mPeriod = $cfgs['period'];
- $this->mOfficialSN = $cfgs['official_sn'] ?? false;
- $this->mOpened = true;
- $this->mSort = 65536;
- $this->mSupportRegins = [];
- }
- //[$state, $errmsg, $neterr]
- abstract public function add($card_no,$card_type,$amount,$params,&$net_errno = 0);
- abstract public function query($refill_info);
- abstract public function balance();
- //分省相关
- public function setRegins($regins) {
- $this->mSupportRegins = $regins;
- }
- public function isRegin(): bool {
- return !empty($this->mSupportRegins);
- }
- public function match_regin($card_type,$regin_no): bool
- {
- if(empty($this->mSupportRegins) || empty($this->mSupportRegins[$card_type])) {
- return true;
- }
- elseif(in_array(-1,$this->mSupportRegins[$card_type])) {
- return true;
- }
- else {
- return in_array($regin_no,$this->mSupportRegins[$card_type]);
- }
- }
- //
- public function name() {
- return $this->mName;
- }
- public function store_id() {
- return $this->mStoreID;
- }
- public function official_sn() {
- return $this->mOfficialSN === true;
- }
- public function callback() {
- return true;
- }
- public function refill_type() {
- return $this->mRefillType;
- }
- protected function match_period()
- {
- $period = $this->mPeriod;
- if(empty($period)) {
- return true;
- }
- $cur_time = time();
- $date = date('Y-m-d',$cur_time);
- $day_start = strtotime("{$date} {$period['start']}");
- if(empty($period['end'])) {
- $day_end = strtotime("{$date}") + 86400;
- }
- else {
- $day_end = strtotime("{$date} {$period['end']}");
- }
- return ($cur_time >= $day_start && $cur_time < $day_end);
- }
- public function amounts()
- {
- return $this->mAmountTypes;
- }
- public function goods($quality,int $amount,int $card_type,$regin_no,$other)
- {
- $maps = $this->mAmountTypes[$quality] ?? [];
- $key = "{$amount}-{$card_type}";
- if(array_key_exists($key,$maps)) {
- $params = $maps[$key];
- return [$params['goods_id'], round($params['price'],4)];
- }
- else {
- return [0,0];
- }
- }
- public function setOpened(bool $opened) {
- $this->mOpened = $opened;
- }
- public function opened() {
- return $this->mOpened;
- }
- public function setSort(int $sort) {
- $this->mSort = $sort;
- }
- public function sort() {
- return $this->mSort;
- }
- public function check(int $amount,int $card_type) : array
- {
- if(!array_key_exists($amount,$this->mAmountTypes)) {
- return [false,'额度不支持'];
- }
- if(!in_array($card_type,$this->mCardType)) {
- return [false,'不支持该类型'];
- }
- if(!$this->match_period()) {
- return [false,'不在充值时间范围内'];
- }
- return [true,'success'];
- }
- protected function check_empty($value)
- {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
- }
|