You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

48 lines
1.3 KiB

<?php
namespace App\Service;
use App\Model\Order;
use App\Model\Store;
use App\TaskWorker\MQTTClientTask;
class MqttSpeakerService implements MqttServiceInterface
{
const TOPIC = 'test01';
/**
* @inheritDoc
*/
public function speakToStore($orderId, $isMain = true)
{
// 获取订单
$orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']);
if ($isMain) {
$orders = $orders->where(['order_main_id' => $orderId])->get()->toArray();
} else {
$orders = $orders->where(['id' => $orderId])->get()->toArray();
}
if(empty($orders)) return;
// 循环发送
foreach ($orders as $k => &$order) {
$order['template'] = "懒族生活支付到账".floatval($order['money'])."";
// 获取终端ID
$order['to_client_id'] = Store::query()->where(['id' => $order['store_id']])->value('loudspeaker_imei');
// 发布订阅消息
$this->pubToMqtt($order['template'], self::TOPIC, $order['to_client_id']);
}
}
/**
* @inheritDoc
*/
public function pubToMqtt($message, $topic, $toClientId)
{
$task = ApplicationContext::getContainer()->get(MQTTClientTask::class);
$task->publish($message, $topic, $toClientId);
}
}