diff --git a/Server/application/api/config/route.php b/Server/application/api/config/route.php index 1a615a5f..8d4354ff 100644 --- a/Server/application/api/config/route.php +++ b/Server/application/api/config/route.php @@ -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控制器路由 diff --git a/Server/application/api/controller/DeviceController.php b/Server/application/api/controller/DeviceController.php index 4ffc4f97..b95f9471 100644 --- a/Server/application/api/controller/DeviceController.php +++ b/Server/application/api/controller/DeviceController.php @@ -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 二维码数据 diff --git a/Server/application/api/model/DeviceGroupModel.php b/Server/application/api/model/DeviceGroupModel.php new file mode 100644 index 00000000..b6747533 --- /dev/null +++ b/Server/application/api/model/DeviceGroupModel.php @@ -0,0 +1,29 @@ + 'integer', + 'tenantId' => 'integer', + 'count' => 'integer', + 'createTime' => 'integer', + 'updateTime' => 'integer' + ]; +} \ No newline at end of file diff --git a/Server/application/command/FriendTaskCommand.php b/Server/application/command/FriendTaskCommand.php index 4d59eaac..7f3c71b2 100644 --- a/Server/application/command/FriendTaskCommand.php +++ b/Server/application/command/FriendTaskCommand.php @@ -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条记录 diff --git a/Server/application/command/WechatFriendCommand.php b/Server/application/command/WechatFriendCommand.php index c100ca13..48862bf3 100644 --- a/Server/application/command/WechatFriendCommand.php +++ b/Server/application/command/WechatFriendCommand.php @@ -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);