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.4 KiB

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