提交其他文件
This commit is contained in:
138
app/common/service/iot/DoorService.php
Normal file
138
app/common/service/iot/DoorService.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\iot;
|
||||
|
||||
use think\facade\Cache;
|
||||
use think\facade\Config;
|
||||
|
||||
/**
|
||||
* 门禁控制类
|
||||
*/
|
||||
class DoorService
|
||||
{
|
||||
/**
|
||||
* 获取通通锁token(带缓存)
|
||||
*/
|
||||
public function getTtlockToken()
|
||||
{
|
||||
$config = Config::get('door.ttlock', []);
|
||||
|
||||
// 检查缓存
|
||||
if (Cache::has('ttlock_token')) {
|
||||
return Cache::get('ttlock_token');
|
||||
}
|
||||
|
||||
$apiurl = 'https://cnapi.sciener.com' . '/oauth2/token';
|
||||
$postData = [
|
||||
'clientId' =>'f5e04886d0b94e128a3115f895e29e22',
|
||||
'clientSecret' => '3b3d0eea32242e6b8dddb6ddb9fa9080',
|
||||
'username' => '18184888888',
|
||||
'password' => md5('cz221001')
|
||||
];
|
||||
|
||||
$result = $this->ttcurl($apiurl, $postData);
|
||||
if (isset($result['access_token'])) {
|
||||
$expireTime = $result['expires_in'];
|
||||
$accessToken = $result['access_token'];
|
||||
|
||||
// 缓存token,提前5秒过期
|
||||
Cache::set('ttlock_token', $accessToken, $expireTime - 5);
|
||||
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通通锁-网关开锁
|
||||
*/
|
||||
public function unlockByNetwork($lockId)
|
||||
{
|
||||
|
||||
$apiurl = 'https://cnapi.sciener.com' . '/v3/lock/unlock';
|
||||
$accessToken = $this->getTtlockToken();
|
||||
|
||||
$postData = [
|
||||
'clientId' => 'f5e04886d0b94e128a3115f895e29e22',
|
||||
'accessToken' => $accessToken,
|
||||
'lockId' => $lockId,
|
||||
'date' => time() * 1000
|
||||
];
|
||||
|
||||
|
||||
return $this->ttcurl($apiurl, $postData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通通锁-创建临时密码
|
||||
*/
|
||||
public function createTempPassword($lockId, $password, $name, $startTime, $endTime)
|
||||
{
|
||||
$config = Config::get('door.ttlock', []);
|
||||
|
||||
$apiurl = 'https://cnapi.sciener.com' . '/v3/keyboardPwd/add';
|
||||
$accessToken = $this->getTtlockToken();
|
||||
|
||||
$postData = [
|
||||
'clientId' => 'f5e04886d0b94e128a3115f895e29e22',
|
||||
'accessToken' => $accessToken,
|
||||
'lockId' => $lockId,
|
||||
'keyboardPwd' => $password,
|
||||
'keyboardPwdType' => '3', // 时效密码
|
||||
'keyboardPwdName' => $name,
|
||||
'startDate' => $startTime * 1000,
|
||||
'endDate' => $endTime * 1000,
|
||||
'addType' => 2,
|
||||
'date' => time() * 1000
|
||||
];
|
||||
|
||||
return $this->ttcurl($apiurl, $postData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通通锁-删除密码
|
||||
*/
|
||||
public function deletePassword($lockId, $passwordId)
|
||||
{
|
||||
$config = Config::get('door.ttlock', []);
|
||||
|
||||
$apiurl = $config['base_url'] . '/v3/keyboardPwd/delete';
|
||||
$accessToken = $this->getTtlockToken();
|
||||
|
||||
$postData = [
|
||||
'clientId' => $config['client_id'],
|
||||
'lockId' => $lockId,
|
||||
'accessToken' => $accessToken,
|
||||
'keyboardPwdId' => $passwordId,
|
||||
'deleteType' => 2,
|
||||
'date' => time() * 1000
|
||||
];
|
||||
|
||||
return $this->ttcurl($apiurl, $postData);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通通锁CURL请求(保持原有格式)
|
||||
*/
|
||||
private function ttcurl($apiurl, $postData)
|
||||
{
|
||||
$data_urlencode = http_build_query($postData);
|
||||
$ch = curl_init($apiurl);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_urlencode);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// 修复SSL问题
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return json_decode($result, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
81
app/common/service/iot/IotService.php
Normal file
81
app/common/service/iot/IotService.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\iot;
|
||||
|
||||
use think\facade\Config;
|
||||
use app\common\service\HttpService;
|
||||
|
||||
class IotService
|
||||
{
|
||||
/**
|
||||
* @var HttpService HTTP服务
|
||||
*/
|
||||
protected $httpService;
|
||||
|
||||
/**
|
||||
* @var array 配置
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->httpService = new HttpService();
|
||||
$this->config = $this->getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制设备开关
|
||||
*/
|
||||
public function controlDevice(string $deviceId, int $powerState)
|
||||
{
|
||||
// 验证参数
|
||||
if (empty($deviceId)) {
|
||||
throw new \Exception('未配置空开设备');
|
||||
}
|
||||
|
||||
if (!in_array($powerState, [0, 1])) {
|
||||
throw new \Exception('电源状态值无效');
|
||||
}
|
||||
|
||||
// 生成签名
|
||||
$timestamp = time();
|
||||
$sign = $this->generateSign($timestamp);
|
||||
|
||||
// 构建URL
|
||||
$url = "{$this->config['base_url']}/{$this->config['app_key']}/device/control/?sign={$sign}&ts={$timestamp}";
|
||||
|
||||
// 构建请求数据
|
||||
$data = [
|
||||
'device' => $deviceId,
|
||||
'power' => $powerState
|
||||
];
|
||||
|
||||
// 发送请求
|
||||
return $this->httpService->json($url, 'POST', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*/
|
||||
protected function generateSign(int $timestamp): string
|
||||
{
|
||||
$appSecret = $this->config['app_secret'];
|
||||
return md5(md5($appSecret) . $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*/
|
||||
protected function getConfig(): array
|
||||
{
|
||||
|
||||
$config = Config::get('door.other', []);
|
||||
|
||||
// 优先从配置读取,没有则使用默认值
|
||||
return [
|
||||
'base_url' => 'https://iot-api.unisoft.cn',
|
||||
'app_key' => 'ptyVWcgl9p',
|
||||
'app_secret' => 'c9fc117f9ec942449c7e61812cd245b4',
|
||||
];
|
||||
}
|
||||
}
|
||||
58
app/common/service/iot/SpeakerService.php
Normal file
58
app/common/service/iot/SpeakerService.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\iot;
|
||||
|
||||
class SpeakerService
|
||||
{
|
||||
|
||||
// 基础配置
|
||||
private $url = 'https://speaker.17laimai.cn/notify.php';
|
||||
private $token = 'HK1654603135';
|
||||
private $version = 1;
|
||||
|
||||
/**
|
||||
* 语音播报
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendSpeakerNotify($speakerId)
|
||||
{
|
||||
|
||||
$data = [
|
||||
'version' => $this->version,
|
||||
'token' => $this->token,
|
||||
'id' => $speakerId,
|
||||
'message' => '您好您预约的时间段还有10分钟即将结束',
|
||||
];
|
||||
|
||||
return $this->curlPost($this->url, $data);
|
||||
}
|
||||
|
||||
private function curlPost($url, $data)
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
|
||||
// 增加SSL选项(如果是HTTPS)
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
|
||||
// 添加错误处理
|
||||
if (curl_errno($ch)) {
|
||||
$error_msg = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \Exception("CURL请求失败: " . $error_msg);
|
||||
}
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user