IotCloudHelper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use TencentCloud\Iotcloud\V20210408\IotcloudClient;
  9. use TencentCloud\Common\Credential;
  10. use TencentCloud\Iotcloud\V20210408\Models\PublishMessageRequest;
  11. use TencentCloud\Iotcloud\V20210408\Models\DescribeDeviceRequest;
  12. use app\models\Store;
  13. /**
  14. * 腾讯云物联网收款语音播报助手
  15. * @package
  16. * @author Syan mzsongyan@gmail.com
  17. * @date 2022-06-02
  18. */
  19. class IotCloudHelper
  20. {
  21. private static $region = 'ap-guangzhou'; // 区域
  22. private static $productId = 'W4W43U0BKB'; // 产品ID
  23. private static $secret = 'QUtJRHlrR1dWRGVvaUxpb2RCU1NDZnJxWTBBNGgzOHdUamlRfFRlYUZkalo4UVJmZEtqcmpKY2VQdDYyaTdFNGR2d2c5';
  24. /**
  25. * 发送消息
  26. * @param mixed $storeId
  27. * @param mixed $msg {"cmd":"voice","msg":"微信收款999元"}
  28. * @return mixed
  29. * @throws \yii\base\InvalidConfigException
  30. * @author Syan mzsongyan@gmail.com
  31. * @date 2022-06-02
  32. */
  33. public static function sendMessage($storeId, $msg)
  34. {
  35. try {
  36. $store = self::getStore($storeId);
  37. $deviceName = $store->device_name;
  38. $topic = self::$productId . '/' . $deviceName . '/data';
  39. $secret = base64_decode(self::$secret);
  40. list($secretId, $secretKey) = explode('|', $secret);
  41. $credential = new Credential($secretId, $secretKey);
  42. $client = new IotcloudClient($credential, self::$region);
  43. $request = new PublishMessageRequest();
  44. $request->Topic = $topic;
  45. $request->Payload = $msg;
  46. $request->ProductId = self::$productId;
  47. $request->DeviceName = $deviceName;
  48. $req = $client->PublishMessage($request);
  49. return [
  50. 'code' => 0,
  51. 'data' => json_decode($req->toJsonString(), true),
  52. ];
  53. } catch (\Exception $e) {
  54. return [
  55. 'code' => 1,
  56. 'msg' => $e->getMessage(),
  57. ];
  58. }
  59. }
  60. /**
  61. * 获取设备信息
  62. * @param mixed $storeId
  63. * @return mixed
  64. * @throws \yii\base\InvalidConfigException
  65. * @author Syan mzsongyan@gmail.com
  66. * @date 2022-06-02
  67. */
  68. public static function getDeviceInfo($storeId)
  69. {
  70. try {
  71. $store = self::getStore($storeId);
  72. $deviceName = $store->device_name;
  73. $secret = base64_decode(self::$secret);
  74. list($secretId, $secretKey) = explode('|', $secret);
  75. $credential = new Credential($secretId, $secretKey);
  76. $client = new IotcloudClient($credential, self::$region);
  77. $request = new DescribeDeviceRequest();
  78. $request->ProductId = self::$productId;
  79. $request->DeviceName = $deviceName;
  80. $req = $client->DescribeDevice($request);
  81. return [
  82. 'code' => 0,
  83. 'data' => json_decode($req->toJsonString(), true),
  84. ];
  85. } catch (\Exception $e) {
  86. return [
  87. 'code' => 1,
  88. 'msg' => $e->getMessage(),
  89. ];
  90. }
  91. }
  92. /**
  93. * 获取商城
  94. * @param mixed $storeId
  95. * @return \app\models\Store
  96. * @throws \yii\base\InvalidConfigException
  97. * @throws \Exception
  98. * @author Syan mzsongyan@gmail.com
  99. * @date 2022-06-02
  100. */
  101. private static function getStore($storeId)
  102. {
  103. $store = Store::findOne($storeId);
  104. if (!$store) {
  105. throw new \Exception('未找到商城');
  106. }
  107. if (empty($store->device_name)) {
  108. throw new \Exception('该商城还未绑定设备');
  109. }
  110. return $store;
  111. }
  112. }