From 24c14bcab2093bffe2e4cd304cd056a4c844ec96 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Fri, 27 Dec 2024 14:44:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20HTTPDataSource=20=E7=9A=84?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/system_config.json | 8 ++++---- src/network/HTTPDataSource.cpp | 26 ++++++++++---------------- tools/map_websocket.html | 2 +- tools/mock_server.py | 26 ++++++++++++++++---------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/config/system_config.json b/config/system_config.json index 7e2e51d..30262d8 100644 --- a/config/system_config.json +++ b/config/system_config.json @@ -28,10 +28,10 @@ "port": 8081, "username": "dianxin", "password": "dianxin@123", - "auth_path": "/api/login", - "aircraft_path": "/api/getCurrentFlightPositions", - "vehicle_path": "/api/getCurrentVehiclePositions", - "traffic_light_path": "/api/getTrafficLightSignals", + "auth_path": "/login", + "aircraft_path": "/openApi/getCurrentFlightPositions", + "vehicle_path": "/openApi/getCurrentVehiclePositions", + "traffic_light_path": "/getTrafficLightSignals", "refresh_interval_ms": 1000, "timeout_ms": 5000, "read_timeout_ms": 2000 diff --git a/src/network/HTTPDataSource.cpp b/src/network/HTTPDataSource.cpp index 2fa4089..881560b 100644 --- a/src/network/HTTPDataSource.cpp +++ b/src/network/HTTPDataSource.cpp @@ -409,33 +409,25 @@ bool HTTPDataSource::authenticate() { return false; } - // 构造认证请求URL - std::string url = "http://" + host_ + ":" + port_ + config_.auth_path; + // 构造带参数的认证URL + std::string url = "http://" + host_ + ":" + port_ + config_.auth_path + + "?username=" + config_.username + + "&password=" + config_.password; + Logger::debug("Authentication URL: ", url); - // 构造认证请求体 - nlohmann::json request = { - {"username", config_.username}, - {"password", config_.password} - }; - - std::string request_body = request.dump(); std::string response; - - Logger::debug("Sending authentication request with username: ", config_.username); - Logger::debug("Request body: ", request_body); // 设置CURL选项 curl_easy_reset(curl_); curl_easy_setopt(curl_, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl_, CURLOPT_POST, 1L); - curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, request_body.c_str()); - curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, static_cast(request_body.length())); + curl_easy_setopt(curl_, CURLOPT_POST, 1L); // 使用POST方法 + curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, ""); // 空的POST数据 curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &response); // 设置请求头 struct curl_slist* headers = nullptr; - headers = curl_slist_append(headers, "Content-Type: application/json"); + headers = curl_slist_append(headers, "Accept: application/json"); curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers); // 发送请求 @@ -453,6 +445,7 @@ bool HTTPDataSource::authenticate() { if (http_code != 200) { Logger::error("Authentication failed with HTTP code: ", http_code); + Logger::error("Response body: ", response); return false; } @@ -479,6 +472,7 @@ bool HTTPDataSource::authenticate() { } catch (const std::exception& e) { Logger::error("Failed to parse authentication response: ", e.what()); + Logger::error("Raw response: ", response); return false; } } \ No newline at end of file diff --git a/tools/map_websocket.html b/tools/map_websocket.html index 62189b7..8ee927a 100644 --- a/tools/map_websocket.html +++ b/tools/map_websocket.html @@ -426,7 +426,7 @@ } try { - ws = new WebSocket('ws://10.0.0.118:8010'); + ws = new WebSocket('ws://localhost:8010'); ws.onopen = () => { log('连接成功', 'success'); diff --git a/tools/mock_server.py b/tools/mock_server.py index 10ce7e9..5c44aa6 100644 --- a/tools/mock_server.py +++ b/tools/mock_server.py @@ -744,7 +744,7 @@ def check_auth(): return False return True -@app.route('/api/getCurrentFlightPositions', methods=['GET', 'OPTIONS']) +@app.route('/openApi/getCurrentFlightPositions', methods=['GET', 'OPTIONS']) def get_flight_positions(): if request.method == 'OPTIONS': return '', 204 @@ -790,7 +790,7 @@ def switch_traffic_light_state(): if old_state != traffic_light_data[1]["state"]: print(f"东路口红绿灯状态切换为: {'绿灯' if traffic_light_data[1]['state'] == 1 else '红灯'}") -@app.route('/api/getCurrentVehiclePositions', methods=['GET', 'OPTIONS']) +@app.route('/openApi/getCurrentVehiclePositions', methods=['GET', 'OPTIONS']) def get_vehicle_positions(): if request.method == 'OPTIONS': return '', 204 @@ -821,7 +821,7 @@ def get_vehicle_positions(): print(f"Error in get_vehicle_positions: {str(e)}") return jsonify({"error": str(e)}), 500 -@app.route('/api/getTrafficLightSignals', methods=['GET', 'OPTIONS']) +@app.route('/getTrafficLightSignals', methods=['GET', 'OPTIONS']) def get_traffic_light_signals(): if request.method == 'OPTIONS': return '', 204 @@ -860,21 +860,27 @@ AUTH_USERNAME = "dianxin" AUTH_PASSWORD = "dianxin@123" AUTH_TOKEN = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI3ODMwOTAsInVzZXJuYW1lIjoiYWRtaW4ifQ.y9feEL_9NT8UzED9NNkb0Ln6C-PBoufiSHWobWe5vWY" -@app.route('/api/login', methods=['POST', 'OPTIONS']) +@app.route('/login', methods=['POST', 'OPTIONS']) def login(): if request.method == 'OPTIONS': return '', 204 - data = request.get_json() - if not data: + + # 从 URL query parameters 中获取用户名和密码 + username = request.args.get('username') + password = request.args.get('password') + + print(f"收到登录请求: username={username}, password={password}") + print(f"请求 URL: {request.url}") + print(f"请求方法: {request.method}") + print(f"请求参数: {request.args}") + + if not username or not password: return jsonify({ "status": 400, - "msg": "请求格式错误", + "msg": "缺少用户名或密码", "data": None }), 400 - username = data.get('username') - password = data.get('password') - if username == AUTH_USERNAME and password == AUTH_PASSWORD: return jsonify({ "status": 200,