mAllQMapPTS; } private function map_cfg($cfgs,$refill_type) { $card_types = function ($stypes) { $result = []; $types = explode(',',$stypes); foreach ($types as $stype) { $type = mtopcard\topcard_type($stype); $result[] = $type; } return $result; }; $result = []; foreach ($cfgs as $item) { $name = $item['name']; $cfg = $item['cfg']; $provider = $this->create_provider($name,$cfg,$refill_type); if($provider !== false) { $this->mProviders[$name] = $provider; $this->mProviderNames[] = $name; } else { continue; } $amounts = $cfg['amount'] ?? []; foreach ($amounts as $spec => $goods) { foreach ($goods as $gitem) { $quality = $gitem['quality'] ?? 1; $types = $card_types($gitem['card_type']); foreach ($types as $type) { $this->mAllQMapPTS[$quality]["{$name}-{$type}-{$spec}"] = ['goods_id' => $gitem['goods_id'],'price' => $gitem['price'],'provider' => $provider]; $this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name; } } } } return $result; } private function create_provider($name,$cfg,$refill_type) { try { $file = BASE_HELPER_RAPI_PATH . "/{$name}/{$refill_type}.php"; if(!file_exists($file)){ Log::record("provider api file={$file} not exist.",Log::DEBUG); return false; } else { require_once($file); Log::record("file={$file} load success.",Log::DEBUG); } $class = "refill\\{$name}\\{$refill_type}"; if (class_exists($class, false)) { $provider = new $class($cfg); $provider->setOpened(false); return $provider; } else { $error = "Base Error: class {$class} isn't exists!"; Log::record(__FUNCTION__ . " {$error}", Log::ERR); } } catch (Exception $ex) { Log::record($ex->getMessage(), Log::ERR); } return false; } public function load() { try { $file = BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php'; if(!file_exists($file)){ Log::record("refill.ini.php ={$file} not exist",Log::DEBUG); return false; } else { include($file); } $this->mProviderNames = []; $this->mAllQMapPTS = []; $this->mProviders = []; $this->mSpecTypes = []; global $config; $this->map_cfg($config['phone_providers'],'RefillPhone'); $this->map_cfg($config['oil_providers'],'RefillOil'); $this->mProviderNames = array_unique($this->mProviderNames); $channels = $this->read_channel(); foreach ($channels as $item) { $name = $item['name']; $regins = $item['regins']; if(array_key_exists($name,$this->mProviders)) { $this->mProviders[$name]->setOpened($item['opened']); $this->mProviders[$name]->setRegins($regins); } } } catch (Exception $ex) { Log::record(__FUNCTION__ ." " . $ex->getMessage(), Log::ERR); } } private function read_channel() { $refill_provider = Model('refill_provider'); $items = $refill_provider->getProviderList(['provider_id' => ['gt' , 0]]); $result = []; foreach ($items as $item) { $name = $item['name']; $regins = []; $tmp = unserialize($item['provinces']); if(!empty($tmp)) { foreach ($tmp as $card_type => $sregions) { $regins = explode(',',$sregions); if(!empty($regins)) { $regins[$card_type] = $regins; } } } $val = ['name' => $name, 'type' => intval($item['type']), 'opened' => intval($item['opened']) == 1, 'sort' => intval($item['sort']), 'regins' => $regins ]; $result[$name] = $val; } return $result; } public function find_providers(int $spec, int $card_type,int $quality,$regin_no): array { $qnames = $this->mSpecTypes[$quality] ?? []; $key = "{$card_type}-{$spec}"; Log::record("quality = {$quality} , key = {$key}",Log::DEBUG); if(array_key_exists($key,$qnames)) { $names = $qnames[$key]; $providers = []; foreach ($names as $name) { Log::record("name = {$name}",Log::DEBUG); $provider = $this->mProviders[$name]; if($provider->opened() && $provider->match_regin($card_type,$regin_no)) { $providers[] = $provider; } } Log::record("providers count = " . count($providers),Log::DEBUG); return $providers; } else { return []; } } private function debug() { //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name; foreach ($this->mSpecTypes as $quality => $items) { Log::record("quality = {$quality}",Log::DEBUG); foreach ($items as $types_spec => $names) { $snames = implode(',',$names); Log::record("types_spec = {$types_spec} providers={$snames}",Log::DEBUG); } } } public function provider(string $chname) { if(array_key_exists($chname,$this->mProviders)) { return $this->mProviders[$chname]; } else { return null; } } public function goods() { //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name; $result = []; foreach ($this->mSpecTypes as $qMaps) { foreach ($qMaps as $type_spec => $names) { $data = explode('-',$type_spec); $type = intval($data[0]); $spec = intval($data[1]); $result[$type][] = $spec; } } $type_specs = []; foreach ($result as $type => $value) { $type_specs[$type] = array_unique($value); } return $type_specs; } public function getCaller($name) { try { $file = BASE_HELPER_RAPI_PATH . "/{$name}/RefillCallBack.php"; if(!file_exists($file)){ Log::record("provider callback api file={$file} not exist.",Log::DEBUG); return false; } else { require_once($file); Log::record("file={$file} load success.",Log::DEBUG); } $class_name = "refill\\{$name}\\RefillCallBack"; if (class_exists($class_name, false)) { $caller = new $class_name(); return $caller; } else { $error = "Base Error: class {$class_name} isn't exists!"; Log::record($error, Log::ERR); return false; } } catch (Exception $ex) { Log::record($ex->getMessage(), Log::ERR); return false; } } }