import 'dart:convert'; import 'package:EasyDartModule/EasyDartModule.dart'; import 'package:EasyDartModule/base/webserver/WebServer.dart'; import '../const/HttpStatusCode.dart'; import '../const/MessageConstants.dart'; import '../const/ResponseJsonCode.dart'; import '../model/ApiResponse.dart'; import '../service/AreaService.dart'; part 'AreaController.route.dart'; @RequestMapping(path: "/report") class AreaController { final AreaService areaService = AreaService(); set callHandler(handler) => _callHandler = handler; Map> get routeMap => routes; AreaController(); // 获取实时数据 @RequestMapping(path: "/getInstantData", method: HttpMethod.GET,auth: false) Future getInstantData( Request request, Map jwt) async { ApiResponse apiResponse = ApiResponse(); try { var queryParameters = request.requestedUri.queryParameters; String? mac = queryParameters['mac']; String? openId = queryParameters['openId']; if (mac == null || mac.isEmpty) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "查询失败,设备mac不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } if (openId == null || openId.isEmpty) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "查询失败,设备openId不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } Map result = await areaService.getInstantData(mac,openId); if (result['flag'] == 1) { // 成功 // apiResponse.code = ResponseJsonCode.success; // apiResponse.message = result['msg'] ?? "查询成功"; // apiResponse.data = result['data']; apiResponse.code = ResponseJsonCode.success; apiResponse.message = result['msg'] ?? "查询成功"; // 2. 将 result['data'] 包装成 List> if (result['data'] != null) { // result['data'] 是一个 Map,包装成 List Map dataMap = Map.from(result['data']); // 可以在这里添加一些额外字段(可选) dataMap['responseTime'] = DateTime.now().millisecondsSinceEpoch; // 将单个 Map 放入 List 中 apiResponse.data = [dataMap]; } else { apiResponse.data = []; } } else { // 失败 apiResponse.code = ResponseJsonCode.fail; apiResponse.message = result['msg'] ?? "查询失败"; apiResponse.data = null; } return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } catch (e) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "查询失败: $e"; return Response( HttpStatusCode.internalServerError, body: apiResponse.serialize(), ); } } // 开始体验设备 @RequestMapping(path: "/start", method: HttpMethod.POST,auth: false) Future start(Request request) async { ApiResponse apiResponse = ApiResponse(); try { var data = jsonDecode(await request.readAsString()); if (data == null || data.isEmpty || data['mac'] == null) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "请求失败,设备mac不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } if (data == null || data.isEmpty || data['openId'] == null) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "请求失败,设备openId不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } Map result = await areaService.startKuaijian(data); if (result['flag'] == 1) { apiResponse.code = ResponseJsonCode.success; apiResponse.message = result['msg'] ?? MessageConstants.ADD_SUCCESS; } else { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = result['msg'] ?? "操作失败"; } return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } catch (e) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "添加失败: $e"; return Response( HttpStatusCode.internalServerError, body: apiResponse.serialize(), ); } } // 结束体验设备 @RequestMapping(path: "/end", method: HttpMethod.POST,auth: false) Future end(Request request) async { ApiResponse apiResponse = ApiResponse(); try { var data = jsonDecode(await request.readAsString()); if (data == null || data.isEmpty || data['mac'] == null) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "请求失败,设备mac不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } if (data == null || data.isEmpty || data['openId'] == null) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "请求失败,设备openId不能为空"; return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } Map result = await areaService.endKuaijian(data); if (result['flag'] == 1) { apiResponse.code = ResponseJsonCode.success; apiResponse.message = result['msg'] ?? MessageConstants.ADD_SUCCESS; } else { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = result['msg'] ?? "操作失败"; } return Response( HttpStatusCode.ok, body: apiResponse.serialize(), ); } catch (e) { apiResponse.code = ResponseJsonCode.fail; apiResponse.message = "添加失败: $e"; return Response( HttpStatusCode.internalServerError, body: apiResponse.serialize(), ); } } // // 更新区域 // @RequestMapping(path: "/updateArea", method: HttpMethod.PUT) // Future updateArea(Request request) async { // ApiResponse apiResponse = ApiResponse(); // try { // var data = jsonDecode(await request.readAsString()); // var areaId = data['_id']; // var updatedArea = Area.fromJson(data); // if (updatedArea.id == null || updatedArea.id!.isEmpty) { // apiResponse.code = ResponseJsonCode.fail; // apiResponse.message = "区域ID不能为空"; // String serializedJson = apiResponse.serialize(); // return Response(HttpStatusCode.ok, body: serializedJson); // } // var result = await areaService.updateArea(areaId, updatedArea); // apiResponse.code = 1; // apiResponse.message = result; // String serializedJson = apiResponse.serialize(); // return Response(200, body: serializedJson); // 发送成功响应 // } catch (e) { // apiResponse.code = -1; // apiResponse.message = "更新失败: $e"; // 包含异常信息 // String serializedJson = apiResponse.serialize(); // return Response(500, body: serializedJson); // 发送失败响应 // } // } // 删除区域 // @RequestMapping(path: "/deleteArea", method: HttpMethod.DELETE) // Future deleteArea(Request request) async { // ApiResponse apiResponse = ApiResponse(); // try { // var data = jsonDecode(await request.readAsString()); // var areaId = data['areaId']; // var result = await areaService.deleteArea(areaId); // apiResponse.code = 1; // if (result.isEmpty) { // apiResponse.code = ResponseJsonCode.success; // apiResponse.message = MessageConstants.DELETE_SUCCESS; // String serializedJson = apiResponse.serialize(); // return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应 // } // apiResponse.code = ResponseJsonCode.fail; // apiResponse.message = result; // String serializedJson = apiResponse.serialize(); // return Response(HttpStatusCode.ok, body: serializedJson); // } catch (e) { // apiResponse.code = -1; // apiResponse.message = "删除失败: $e"; // 包含异常信息 // String serializedJson = apiResponse.serialize(); // return Response(500, body: serializedJson); // 发送失败响应 // } // } }