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 { $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() { $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']; if(array_key_exists($name,$this->mProviders)) { $this->mProviders[$name]->setOpened($item['opened']); } } } private function read_channel() { $refill_provider = Model('refill_provider'); $items = $refill_provider->getProviderList([]); $result = []; foreach ($items as $item) { $name = $item['name']; $val = ['name' => $name, 'type' => intval($item['type']), 'opened' => (intval($item['opened']) == 1) ? true : false, 'sort' => intval($item['sort'])]; $result[$name] = $val; } return $result; } public function find_providers(int $spec, int $card_type,int $quality): array { $qnames = $this->mSpecTypes[$quality] ?? []; $key = "{$card_type}-{$spec}"; if(array_key_exists($key,$qnames)) { $names = $qnames[$key]; $providers = []; foreach ($names as $name) { $provider = $this->mProviders[$name]; if($provider->opened()) { $providers[] = $provider; } } return $providers; } else { return []; } } public function provider(string $chname) { if(array_key_exists($chname,$this->mProviders)) { return $this->mProviders[$chname]; } else { return null; } } public function combine_goods($configs,$type) { $mod_prov = Model('refill_provider'); $provider_items = $mod_prov->getProviderList(['type' => $type]); foreach ($provider_items as $item) { $providers[$item['name']] = $item; } $result = []; foreach ($configs as $item) { if($providers[$item['name']]['opened'] != 1) { continue; } $cfg = $item['cfg']; $card_types = $cfg['card_type'] ?? []; $amounts = $cfg['amount'] ?? []; foreach ($card_types as $type) { if (array_key_exists($type, $result)) { $item = $result[$type]; } else { $item = []; } foreach ($amounts as $amount => $val) { $item[] = $amount; } $item = array_unique($item); $result[$type] = $item; } } return $result; } public function goods() { global $config; $oil = $this->combine_goods($config['oil_providers'], 1); $phone = $this->combine_goods($config['phone_providers'], 2); return array_merge($oil, $phone); } public function providers() { return ['oil' => $this->mOilProvider,'phone' => $this->mPhoneProvider]; } public function getCaller($chname) { try { $class_name = "refill\\{$chname}\\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; } } }