| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Console\Commands;
- use App\Repositories\ConfigRepoistory;
- use Carbon\Carbon;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- class ConfigUsdtRate extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'config:usdt_rate';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '获取usdt汇率';
- private $reatUrl = [
- 'http://api.coincap.io/v2/rates/chinese-yuan-renminbi',
- 'https://v6.exchangerate-api.com/v6/1a45491bb39175ddc4203829/latest/CNY',
- 'https://api.exchangerate-api.com/v4/latest/CNY'
- ];
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- $this->comment('开始获取usdt汇率');
- $confRepo = app(ConfigRepoistory::class);
- $config = $confRepo->pickByKeys(['usdt_rate', 'usdt_pick_switch'], 'sys');
- if ($config['usdt_pick_switch']['svalue'] != 1) {
- $this->comment('ust采集开关已关闭');
- return '';
- }
- $oldUsdtRate = $config['usdt_rate']['svalue'];
- foreach ($this->reatUrl as $key => $url) {
- try {
- $res = $this->curl($url);
- $aRes = json_decode($res,true);
- if ($key == 0) {
- if (empty($aRes['data']) || $aRes['data']['symbol'] != 'CNY') {
- continue;
- }
- $rateUsd = round($aRes['data']['rateUsd'], 2);
- break;
- }
- if ($key == 1) {
- if ( empty($aRes['result'])|| $aRes['result'] != 'success') {
- continue;
- }
- $rateUsd = round($aRes['conversion_rates']['USD'], 2);
- break;
- }
- if ($key == 2) {
- if (empty($aRes['rates'])) {
- continue;
- }
- $rateUsd = round($aRes['rates']['USD'], 2);
- break;
- }
- } catch (\Exception $e) {
- $this->comment('ustd汇率获取失败'. $key);
- continue;
- }
- }
- $usdtRate = (round(100 / $rateUsd, 2)) / 100;
- if ($oldUsdtRate != $usdtRate) {
- $confRepo->saveByKeys(['usdt_rate' => ($usdtRate + 0.3)], 'sys');
- $confRepo->updateConfigCache('sys');
- }
- }
- private function curl($url)
- {
- $client = new Client();
- $res = $client->request('GET', $url, [
- 'headers' => ['Content-type' => 'application/json']
- ]);
- $data = $res->getBody()->getContents();
- return $data;
- }
- }
|