123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
- require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
- require_once(BASE_CORE_PATH . '/framework/function/http.php');
- class providerControl extends SystemControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function indexOp()
- {
- $this->sync_cfgs();
- $provider_model = Model('refill_provider');
- $condition = [];
- if (trim($_GET['name']) != '') {
- $condition['store_name'] = ['like', '%' . $_GET['name'] . '%'];
- Tpl::output('name', $_GET['name']);
- }
- if (in_array($_GET['type'], [1, 2])) {
- $condition['type'] = $_GET['type'];
- }
- $provider_list = $provider_model->table('refill_provider,store')
- ->field('refill_provider.*,store.store_name')
- ->join('inner')
- ->on('store.store_id=refill_provider.store_id')
- ->where($condition)
- ->order('opened asc, name asc')
- ->page(100)
- ->select();
- foreach ($provider_list as $key => $provider) {
- if (!empty($provider['start_period']) && !empty($provider['end_period'])) {
- $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
- } else {
- $provider_list[$key]['period'] = '全时间段';
- }
- }
- $opened_text = ['使用中', '已禁用'];
- $type_text = ['油卡', '手机充值卡', '增值业务'];
- Tpl::output('opened_text', $opened_text);
- Tpl::output('type_text', $type_text);
- Tpl::output('provider_list', $provider_list);
- Tpl::output('show_page', $provider_model->showpage());
- Tpl::showpage('provider.index');
- }
- public function sync_cfgs()
- {
- $name_val = function ($items) {
- $result = [];
- foreach ($items as $item) {
- $name = $item['name'];
- $result[$name] = $item;
- }
- return $result;
- };
- $match = function ($all, $cur) {
- $inserts = $updates = [];
- foreach ($all as $key => $value) {
- if (!array_key_exists($key, $cur)) {
- $insert['name'] = $key;
- $insert['store_id'] = $value['cfg']['store_id'];
- $insert['qualitys'] = $value['cfg']['qualitys'];
- $inserts[] = $insert;
- } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
- $update['provider_id'] = $cur[$key]['provider_id'];
- $update['qualitys'] = $value['cfg']['qualitys'];
- $updates[] = $update;
- }
- }
- return [$inserts, $updates];
- };
- $updater = function ($mod, $updates) {
- if (empty($updates)) return;
- foreach ($updates as $update) {
- $provider_id = $update['provider_id'];
- $data = ['qualitys' => $update['qualitys']];
- $mod->editProvider($data, ['provider_id' => $provider_id]);
- }
- };
- $inserter = function ($mod, $type, $names) {
- foreach ($names as $name) {
- $data = ['name' => $name['name'], 'type' => $type, 'store_id' => $name['store_id'], 'qualitys' => $name['qualitys'], 'opened' => 2];
- $mod->insert($data);
- }
- };
- global $config;
- $oil_configs = $config['oil_providers'];
- $pho_configs = $config['phone_providers'];
- $third_configs = $config['third_providers'];
- $oils = $name_val($oil_configs);
- $phones = $name_val($pho_configs);
- $third = $name_val($third_configs);
- $mod_prov = Model('refill_provider');
- $oil_items = $mod_prov->getProviderList(['type' => 1]);
- $oil_items = $name_val($oil_items);
- [$oil_inserts, $oil_updates] = $match($oils, $oil_items);
- $phone_items = $mod_prov->getProviderList(['type' => 2]);
- $phone_items = $name_val($phone_items);
- [$phone_inserts, $phone_updates] = $match($phones, $phone_items);
- $third_items = $mod_prov->getProviderList(['type' => 3]);
- $third_items = $name_val($third_items);
- [$third_inserts, $third_updates] = $match($third, $third_items);
- $inserter($mod_prov, 1, $oil_inserts);
- $inserter($mod_prov, 2, $phone_inserts);
- $inserter($mod_prov, 3, $third_inserts);
- $updater($mod_prov, $oil_updates);
- $updater($mod_prov, $phone_updates);
- $updater($mod_prov, $third_updates);
- }
- public function ProviderBalanceOp()
- {
- $provider_model = Model('refill_provider');
- $provider_id = $_GET['id'] ?? $_POST['id'];
- $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
- if (empty($provider_info)) {
- exit(json_encode(['code' => 500, 'balance' => 0, 'msg' => '通道信息有误']));
- }
- $channel_name = $provider_info['name'];
- if ($provider_info['type'] == 1) {
- $type = 'RefillOil';
- } elseif ($provider_info['type'] == 2) {
- $type = 'RefillPhone';
- } else {
- $type = '';
- }
- $provider = $this->getProvider($channel_name,$type);
- [$state, $balance] = $provider->balance();
- if(!$state) {
- Log::record("query balance err, channel_name:{$channel_name}, err_msg:{$balance}");
- exit(json_encode(['code' => 500, 'balance' => 0, 'msg' => $balance]));
- }else{
- exit(json_encode(['code' => 200, 'balance' => $balance, 'msg' => 'success']));
- }
- }
- private function getProvider($name,$type = 'RefillPhone')
- {
- $file = BASE_HELPER_RAPI_PATH . "/$name/{$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_name = "refill\\{$name}\\{$type}";
- 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;
- }
- }
- }
|