设备部分组提交

This commit is contained in:
wong
2025-04-14 09:21:01 +08:00
parent b8d8a89bf9
commit 84f33106eb
5 changed files with 178 additions and 4 deletions

View File

@@ -21,6 +21,9 @@ Route::group('v1', function () {
Route::get('list', 'app\\api\\controller\\DeviceController@getList'); // 获取设备列表 √
//Route::get('add/:accountId', 'app\\api\\controller\\DeviceController@addDevice'); // 生成设备二维码
Route::post('add', 'app\\api\\controller\\DeviceController@addDevice'); // 生成设备二维码POST方式
Route::post('updateDeviceGroup', 'app\\api\\controller\\DeviceController@updateDeviceGroup'); // 更新设备分组 √
Route::post('createGroup', 'app\\api\\controller\\DeviceController@createGroup'); // 创建设备分组 √
Route::get('groupList', 'app\\api\\controller\\DeviceController@getGroupList'); // 获取设备分组列表 √
});
// FriendTask控制器路由

View File

@@ -3,6 +3,7 @@
namespace app\api\controller;
use app\api\model\DeviceModel;
use app\api\model\DeviceGroupModel;
use think\facade\Request;
use think\facade\Env;
use Endroid\QrCode\QrCode;
@@ -131,6 +132,122 @@ class DeviceController extends BaseController
}
}
/**
* 创建设备分组
* @return \think\response\Json
*/
public function createGroup()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取参数
$groupName = $this->request->param('groupName', '');
$groupMemo = $this->request->param('groupMemo', '');
if (empty($groupName)) {
return errorJson('分组名称不能为空');
}
// 构建请求参数
$params = [
'groupName' => $groupName,
'groupMemo' => $groupMemo
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
// 发送请求
$result = requestCurl($this->baseUrl . 'api/DeviceGroup/new', $params, 'POST', $header);
$response = handleApiResponse($result);
return successJson($response);
} catch (\Exception $e) {
return errorJson('创建设备分组失败:' . $e->getMessage());
}
}
/**
* 更新设备分组
* @return \think\response\Json
*/
public function updateDeviceGroup()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取参数
$id = $this->request->param('id', '');
$groupId = $this->request->param('groupId', '');
if (empty($id)) {
return errorJson('设备ID不能为空');
}
if (empty($groupId)) {
return errorJson('分组ID不能为空');
}
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
// 发送请求
$result = requestCurl($this->baseUrl . 'api/device/updateDeviceGroup?id=' . $id . '&groupId=' . $groupId, [], 'PUT', $header);
$response = handleApiResponse($result);
return successJson($response);
} catch (\Exception $e) {
return errorJson('更新设备分组失败:' . $e->getMessage());
}
}
/**
* 获取设备分组列表
* @return \think\response\Json
*/
public function getGroupList()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
// 发送请求
$result = requestCurl($this->baseUrl . 'api/DeviceGroup/list', [], 'GET', $header);
$response = handleApiResponse($result);
// 保存数据到数据库
if (!empty($response)) {
foreach ($response as $item) {
$this->saveDeviceGroup($item);
}
}
return successJson($response);
} catch (\Exception $e) {
return errorJson('获取设备分组列表失败:' . $e->getMessage());
}
}
/************************ 私有辅助方法 ************************/
/**
@@ -192,6 +309,31 @@ class DeviceController extends BaseController
}
}
/**
* 保存设备分组数据到数据库
* @param array $item 设备分组数据
*/
private function saveDeviceGroup($item)
{
$data = [
'id' => $item['id'],
'tenantId' => $item['tenantId'],
'groupName' => $item['groupName'],
'groupMemo' => $item['groupMemo'],
'count' => isset($item['count']) ? $item['count'] : 0,
'createTime' => $item['createTime'] == '0001-01-01T00:00:00' ? 0 : strtotime($item['createTime'])
];
// 使用ID作为唯一性判断
$group = DeviceGroupModel::where('id', $item['id'])->find();
if ($group) {
$group->save($data);
} else {
DeviceGroupModel::create($data);
}
}
/**
* 生成二维码图片base64格式
* @param string $data 二维码数据

View File

@@ -0,0 +1,29 @@
<?php
namespace app\api\model;
use think\Model;
class DeviceGroupModel extends Model {
// 设置表名
protected $table = 's2_device_group';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 设置字段类型
protected $type = [
'id' => 'integer',
'tenantId' => 'integer',
'count' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer'
];
}

View File

@@ -24,7 +24,7 @@ class FriendTaskCommand extends Command
try {
// 从缓存获取初始页码缓存10分钟有效
$pageIndex = Cache::get('friendTaskPage', 21);
$pageIndex = Cache::get('friendTaskPage', 0);
$output->writeln('从缓存获取页码:' . $pageIndex);
$pageSize = 1000; // 每页获取1000条记录

View File

@@ -24,12 +24,12 @@ class WechatFriendCommand extends Command
try {
// 从缓存获取初始页码和上次处理的好友ID缓存10分钟有效
$pageIndex = Cache::get('friendsPage', 21);
$preFriendId = Cache::get('preFriendId', 19426090);
$pageIndex = Cache::get('friendsPage', 0);
$preFriendId = Cache::get('preFriendId', '');
$output->writeln('从缓存获取页码:' . $pageIndex . '上次处理的好友ID' . ($preFriendId ?: '无'));
$pageSize = 1000; // 每页获取1000条记录
$pageSize = 100; // 每页获取1000条记录
// 将任务添加到队列
$this->addToQueue($pageIndex, $pageSize, $preFriendId);