add mq test

This commit is contained in:
2025-11-17 16:51:51 +08:00
parent 6b8943f07f
commit b7a628e13c
9 changed files with 292 additions and 7 deletions
+87
View File
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Model\User;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerInterface;
use App\Platform\Tmall\Producer\OrderProducer as TmallOrderProducer;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Amqp\Producer;
#[Command]
class AppMqInit extends HyperfCommand
{
#[Inject]
private Producer $producerService;
public function __construct(protected ContainerInterface $container)
{
parent::__construct('app:mq:init');
}
public function configure()
{
parent::configure();
$this->setDescription('Insert mq test feeds');
}
public function handle()
{
$this->line('Insert MQ test feeds now...', 'info');
// shopee
// $company_id = 171;
// $platform_id = 25;
// $store_id = 255;
// Tmall
$company_id = 188;
$platform_id = 2;
$store_id = 292;
for ($i=0; $i < 2; $i++) {
$unique_id = \uniqid();
// 构建 $message_id 格式为 {prefix}#{app_id}#{company_id}#{platform_id}#{store_id}#{entity_type}#{unique_id}
$data = Db::connection('raw')->table('wpic_taobao_order')
->select(['id', 'order_raw'])->orderBy('t_created', 'asc')
->limit(20)->offset($i * 20)
->get()->toArray();
$data = [
'platform_id' => $platform_id,
'company_id' => $company_id,
'store_id' => $store_id,
'unique_id' => $unique_id,
'raw_data' => $data,
];
$message = new TmallOrderProducer($data);
$result = $this->producerService->produce($message, true);
$log = \sprintf('%s %s %s %s %s',
$company_id,
$platform_id,
$store_id,
$unique_id,
$result
);
dump($log);
}
}
}
@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace App\Platform\Tmall\Producer;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
#[Producer]
class OrderProducer extends ProducerMessage
{
/**
* Exchange 名称
*/
protected string $exchange = 'tmall.exchange';
/**
* Routing key
*/
protected string|array $routingKey = 'order.tmall';
/**
* VHost
*/
protected string $vhost = 'dataflow';
/**
* 消息持久化
*/
protected array $properties = [
'delivery_mode' => 2, // 持久化消息
];
/**
* 构造消息
*
* @param array $data 订单数据
* @return string
*/
public function __construct(array $data = [])
{
if (!empty($data)) {
$this->payload = $this->buildMessage($data);
}
}
/**
* 构建消息格式
*
* @param array $data 原始订单数据
* @return array
*/
protected function buildMessage(array $data): array
{
// 根据 RabbitMQ.md 中定义的消息格式规范
return [
'message_id' => $this->generateMessageId($data),
'timestamp' => date('c'), // ISO 8601 格式
'platform' => 'tmall',
'data_type' => 'order',
'metadata' => [
'platform_id' => $data['platform_id'] ?? null,
'company_id' => $data['company_id'] ?? null,
'store_id' => $data['store_id'] ?? null,
'source_system' => 'tmall-open-api',
'retry_count' => 0,
'data_version' => $data['data_version'] ?? time(),
],
'data' => $data['raw_data'],
];
}
/**
* 生成消息ID
*
* 格式: {prefix}#{app_id}#{company_id}#{platform_id}#{store_id}#{entity_type}#{entity_id}
*
* @param array $data
* @return string
*/
protected function generateMessageId(array $data): string
{
$company_id = $data['company_id'];
$platform_id = $data['platform_id'];
$store_id = $data['store_id'];
$entity_type = 'order';
$unique_id = $data['unique_id'];
return sprintf(
'%s#%s#%s#%s#%s',
$company_id,
$platform_id,
$store_id,
$entity_type,
$unique_id
);
}
}