Files
xk-api-old-laravel/app/Console/Commands/DataSyncOldCommand.php
2025-01-11 14:16:20 +08:00

81 lines
2.4 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 DataSyncOldCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'data:sync-old {--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-old', 'send');
log_s($data, 'sync-old', 'send');
try {
$platform = Platform::first();
if (!$platform) {
log_s('数据同步(老) => 发送结果:无平台数据', 'sync-old', 'send');
return Command::SUCCESS;
}
if ($platform->status != Platform::STATUS_PASS) {
log_s('数据同步(老) => 发送结果:权限不足', 'sync-old', 'send');
return Command::SUCCESS;
}
[$uri, $form_params] = $this->$type($data);
$response = $this->newRequest()->post(rtrim($platform->url, '/') . '/' . $uri, [
'headers' => [
'Authorization' => "Bearer $platform->token",
],
'form_params' => $form_params
]);
$contents = $response->getBody()->getContents();
console_info($contents);
$result = json_decode($contents, true);
log_s('数据同步(老) => 发送结果:' . var_export($result, true), 'sync-old', 'send');
} catch (Exception $exception) {
log_s('数据同步(老) => 错误信息:' . $exception->getMessage(), 'sync-old', 'send');
}
return Command::SUCCESS;
}
public function newRequest(): Client
{
return new Client([
'timeout' => 10,
'verify' => false,
'http_errors' => false,
]);
}
private function syncOrder($data): array
{
return ['platform/v1/sync/order', json_decode($data, true)];
}
}