75 lines
2.1 KiB
PHP
75 lines
2.1 KiB
PHP
<?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,
|
|
]);
|
|
}
|
|
}
|