初始化项目
This commit is contained in:
74
app/Console/Commands/DataSyncCommand.php
Normal file
74
app/Console/Commands/DataSyncCommand.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\YiiModels\Platform;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DataSyncCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'data:sync {--type=} {--data=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '数据同步';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$type = $this->option('type');
|
||||
$data = $this->option('data');
|
||||
log_s('数据同步 => 开始发送:' . $type, 'sync', 'send');
|
||||
log_s($data, 'sync-old', 'send');
|
||||
try {
|
||||
$platform = Platform::first();
|
||||
if (!$platform) {
|
||||
log_s('数据同步 => 发送结果:无平台数据', 'sync', 'send');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
if ($platform->status != Platform::STATUS_PASS) {
|
||||
log_s('数据同步 => 发送结果:权限不足', 'sync', 'send');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
$response = $this->newRequest()->post($platform->url_new, [
|
||||
'form_params' => [
|
||||
'platform_token' => $platform->token,
|
||||
'type' => $type,
|
||||
'data' => json_encode($data),
|
||||
]
|
||||
]);
|
||||
$contents = $response->getBody()->getContents();
|
||||
$result = json_decode($contents, true);
|
||||
log_s('数据同步 => 发送结果:' . var_export($result, true), 'sync', 'send');
|
||||
} catch (Exception $exception) {
|
||||
log_s('数据同步 => 错误信息:' . $exception->getMessage(), 'sync', 'send');
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
public function newRequest(): Client
|
||||
{
|
||||
return new Client([
|
||||
'timeout' => 10,
|
||||
'verify' => false,
|
||||
'http_errors' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user