diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 7444d1fd..1c7986d6 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -11,14 +11,13 @@ Route::group('v1/', function () { // 设备管理相关 Route::group('devices', function () { Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\device\\GetRelatedAccountsV1Controller@index'); // 设备关联微信账号路由 - Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录 - Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 - Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数 - Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情 + Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\device\\GetDeviceHandleLogsV1Controller@index'); // 获取设备操作记录 + Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 + Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情 Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备 - Route::put('refresh', 'app\\cunkebao\\controller\\device\\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态 + Route::put('refresh', 'app\\cunkebao\\controller\\device\\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态 Route::delete(':id', 'app\\cunkebao\\controller\\Device@delete'); // 删除设备 - Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 + Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 }); // 设备微信相关 diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php index b9fdbf69..ddead438 100644 --- a/Server/application/cunkebao/controller/Device.php +++ b/Server/application/cunkebao/controller/Device.php @@ -1,11 +1,9 @@ userInfo; - - // 获取设备ID - $deviceId = $this->request->param('id/d'); - if (empty($deviceId)) { - return json([ - 'code' => 400, - 'msg' => '设备ID不能为空' - ]); - } - - // 检查用户是否有权限访问该设备 - if ($userInfo['isAdmin'] != 1) { - // 非管理员需要检查是否有权限访问该设备 - $hasPermission = \app\common\model\DeviceUser::checkUserDevicePermission( - $userInfo['id'], - $deviceId, - $userInfo['companyId'] - ); - - if (!$hasPermission) { - return json([ - 'code' => 403, - 'msg' => '您没有权限查看该设备' - ]); - } - } - - // 获取设备信息,确认设备存在 - $device = DeviceModel::where('id', $deviceId) - ->where('isDeleted', 0) - ->find(); - - if (!$device) { - return json([ - 'code' => 404, - 'msg' => '设备不存在或已删除' - ]); - } - - // 获取设备关联的微信账号 - $wechatAccounts = DeviceWechatLogin::getDeviceRelatedAccounts($deviceId, $userInfo['companyId']); - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'deviceId' => $deviceId, - 'accounts' => $wechatAccounts, - 'total' => count($wechatAccounts) - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - /** * 获取设备操作记录 * @return \think\response\Json diff --git a/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php new file mode 100644 index 00000000..cec957e1 --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php @@ -0,0 +1,87 @@ + $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ]; + + $hasPermission = DeviceUserModel::where($where)->count() > 0; + + if (!$hasPermission) { + throw new \Exception('您没有权限查看该设备', '403'); + } + } + + /** + * 查询设备操作记录,并关联用户表获取操作人信息 + * + * @param int $deviceId + * @return \think\Paginator + */ + protected function getHandleLogs(int $deviceId): \think\Paginator + { + return DeviceHandleLog::alias('l') + ->field([ + 'l.id', + 'l.content', + 'l.createTime', + 'u.username' + ]) + ->leftJoin('users u', 'l.userId = u.id') + ->where('l.deviceId', $deviceId) + ->order('l.createTime desc') + ->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]); + } + + /** + * 获取设备操作记录 + * + * @return \think\response\Json + */ + public function index() + { + try { + $deviceId = $this->request->param('id/d'); + + if ($this->getUserInfo('isAdmin') != 1) { + $this->checkUserDevicePermission($deviceId); + } + + $logs = $this->getHandleLogs($deviceId); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'total' => $logs->total(), + 'list' => $logs->items() + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index b3d47db7..235e9de8 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -66,7 +66,7 @@ class GetDeviceListV1Controller extends BaseController * @param int $limit 每页数量 * @return \think\Paginator 分页对象 */ - protected function getDeviceList(array $where, int $page = 1, int $limit = 10) + protected function getDeviceList(array $where, int $page = 1, int $limit = 10): \think\Paginator { $query = DeviceModel::alias('d') ->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend']) @@ -82,7 +82,7 @@ class GetDeviceListV1Controller extends BaseController $query->where($key, $value); } - return $query->paginate($limit, false, ['page' => $page]); + return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]); } /** @@ -115,15 +115,12 @@ class GetDeviceListV1Controller extends BaseController public function index() { try { - $page = (int)$this->request->param('page', 1); - $limit = (int)$this->request->param('limit', 10); - if ($this->getUserInfo('isAdmin') == 1) { $where = $this->makeWhere(); - $result = $this->getDeviceList($where, $page, $limit); + $result = $this->getDeviceList($where); } else { $where = $this->makeWhere($this->makeDeviceIdsWhere()); - $result = $this->getDeviceList($where, $page, $limit); + $result = $this->getDeviceList($where); } return json([