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.

47 lines
1.3 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Model\Order;
  4. use App\Model\Store;
  5. use App\TaskWorker\MQTTClientTask;
  6. class MqttSpeakerService implements MqttServiceInterface
  7. {
  8. const TOPIC = 'test01';
  9. /**
  10. * @inheritDoc
  11. */
  12. public function speakToStore($orderId, $isMain = true)
  13. {
  14. // 获取订单
  15. $orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']);
  16. if ($isMain) {
  17. $orders = $orders->where(['order_main_id' => $orderId])->get()->toArray();
  18. } else {
  19. $orders = $orders->where(['id' => $orderId])->get()->toArray();
  20. }
  21. if(empty($orders)) return;
  22. // 循环发送
  23. foreach ($orders as $k => &$order) {
  24. $order['template'] = "懒族生活支付到账".floatval($order['money'])."";
  25. // 获取终端ID
  26. $order['to_client_id'] = Store::query()->where(['id' => $order['store_id']])->value('loudspeaker_imei');
  27. // 发布订阅消息
  28. $this->pubToMqtt($order['template'], self::TOPIC, $order['to_client_id']);
  29. }
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. public function pubToMqtt($message, $topic, $toClientId)
  35. {
  36. $task = ApplicationContext::getContainer()->get(MQTTClientTask::class);
  37. $task->publish($message, $topic, $toClientId);
  38. }
  39. }