Files
2026-03-14 16:20:49 +08:00

138 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}