| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils;
- use TencentCloud\Iotcloud\V20210408\IotcloudClient;
- use TencentCloud\Common\Credential;
- use TencentCloud\Iotcloud\V20210408\Models\PublishMessageRequest;
- use TencentCloud\Iotcloud\V20210408\Models\DescribeDeviceRequest;
- use app\models\Store;
- /**
- * 腾讯云物联网收款语音播报助手
- * @package
- * @author Syan mzsongyan@gmail.com
- * @date 2022-06-02
- */
- class IotCloudHelper
- {
- private static $region = 'ap-guangzhou'; // 区域
- private static $productId = 'W4W43U0BKB'; // 产品ID
- private static $secret = 'QUtJRHlrR1dWRGVvaUxpb2RCU1NDZnJxWTBBNGgzOHdUamlRfFRlYUZkalo4UVJmZEtqcmpKY2VQdDYyaTdFNGR2d2c5';
- /**
- * 发送消息
- * @param mixed $storeId
- * @param mixed $msg {"cmd":"voice","msg":"微信收款999元"}
- * @return mixed
- * @throws \yii\base\InvalidConfigException
- * @author Syan mzsongyan@gmail.com
- * @date 2022-06-02
- */
- public static function sendMessage($storeId, $msg)
- {
- try {
- $store = self::getStore($storeId);
- $deviceName = $store->device_name;
- $topic = self::$productId . '/' . $deviceName . '/data';
- $secret = base64_decode(self::$secret);
- list($secretId, $secretKey) = explode('|', $secret);
- $credential = new Credential($secretId, $secretKey);
- $client = new IotcloudClient($credential, self::$region);
- $request = new PublishMessageRequest();
- $request->Topic = $topic;
- $request->Payload = $msg;
- $request->ProductId = self::$productId;
- $request->DeviceName = $deviceName;
- $req = $client->PublishMessage($request);
- return [
- 'code' => 0,
- 'data' => json_decode($req->toJsonString(), true),
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取设备信息
- * @param mixed $storeId
- * @return mixed
- * @throws \yii\base\InvalidConfigException
- * @author Syan mzsongyan@gmail.com
- * @date 2022-06-02
- */
- public static function getDeviceInfo($storeId)
- {
- try {
- $store = self::getStore($storeId);
- $deviceName = $store->device_name;
- $secret = base64_decode(self::$secret);
- list($secretId, $secretKey) = explode('|', $secret);
- $credential = new Credential($secretId, $secretKey);
- $client = new IotcloudClient($credential, self::$region);
- $request = new DescribeDeviceRequest();
- $request->ProductId = self::$productId;
- $request->DeviceName = $deviceName;
- $req = $client->DescribeDevice($request);
- return [
- 'code' => 0,
- 'data' => json_decode($req->toJsonString(), true),
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取商城
- * @param mixed $storeId
- * @return \app\models\Store
- * @throws \yii\base\InvalidConfigException
- * @throws \Exception
- * @author Syan mzsongyan@gmail.com
- * @date 2022-06-02
- */
- private static function getStore($storeId)
- {
- $store = Store::findOne($storeId);
- if (!$store) {
- throw new \Exception('未找到商城');
- }
- if (empty($store->device_name)) {
- throw new \Exception('该商城还未绑定设备');
- }
- return $store;
- }
- }
|