From 0656c688ec301df1e067509803199a899438c5f6 Mon Sep 17 00:00:00 2001 From: dongpd Date: Tue, 26 May 2020 11:47:51 +0700 Subject: [PATCH] fix bug --- controllers/ConfigController.php | 31 +++++++++++++++++++------------ web/js/config.js | 22 +++++++++++++++++++--- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/controllers/ConfigController.php b/controllers/ConfigController.php index 1914033e..0cdffb38 100644 --- a/controllers/ConfigController.php +++ b/controllers/ConfigController.php @@ -161,8 +161,8 @@ class ConfigController extends Controller { $servermqtt = $t['servermqtt']; } return $this->render('index', [ - "servermqtt" => $servermqtt, - "url" => isset($temp[2]) ? $temp[2] : "" + "servermqtt" => $servermqtt === "" ? $ip . ":1883" : $servermqtt, + "url" => isset($temp[2]) ? $temp[2] : $ip . ":5001" ]); } @@ -260,16 +260,20 @@ class ConfigController extends Controller { 'method' => "POST" ] ])), true); - if (!$tempConfig['status']) - return $this->redirect(['/config']); - - $engineConfig = json_decode($tempConfig['data'], true); - if (count($engineConfig['data']['engines']) == 0) { - $path = ""; + if ($tempConfig['status']) { + if ($tempConfig['data'] !== "") { + $engineConfig = json_decode($tempConfig['data'], true); + if (count($engineConfig['data']['engines']) == 0) { + $path = ""; + } else { + $path = $engineConfig['data']['engines'][0]['path']; + } + } else { + $path = ""; + } } else { - $path = $engineConfig['data']['engines'][0]['path']; + $path = ""; } - file_get_contents("http://localhost:4004/Reset", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", @@ -293,7 +297,10 @@ class ConfigController extends Controller { ] ])), true); if (!$tempConfig['status']) - return ['status' => false]; + return ['status' => false, 'text' => '']; + + if ($tempConfig['data'] === "") + return ['status' => false, 'text' => 'Cấu hình ip chưa đúng!']; $engineConfig = json_decode($tempConfig['data'], true); if (count($engineConfig['data']['engines']) == 0) @@ -310,7 +317,7 @@ class ConfigController extends Controller { ])), true); if (!$checkConfig['status']) { - return ['status' => false]; + return ['status' => false, 'text' => '']; } else { return ['status' => true, 'url' => Url::to(['cau-hinh-nhan-dien'])]; } diff --git a/web/js/config.js b/web/js/config.js index 2f443947..68d8af80 100644 --- a/web/js/config.js +++ b/web/js/config.js @@ -2,13 +2,23 @@ $(function () { }); function saveStep1(e) { + var servermqtt = $("input[name='servermqtt']").val(); + if (servermqtt === "") { + alert("Hãy nhập thông tin!"); + return; + } + var url = $("input[name='url']").val(); + if (url === "") { + alert("Hãy nhập thông tin!"); + return; + } common.modalBlock(true); $.ajax({ url: $(e).attr("data-href"), type: 'POST', data: { - servermqtt: $("input[name='servermqtt']").val(), - url: $("input[name='url']").val() + servermqtt: servermqtt, + url: url }, success: function (data) { setInterval(function () { @@ -16,8 +26,14 @@ function saveStep1(e) { url: data, type: 'POST', success: function (data) { - if (data.status) + if (data.status) { window.location = data.url; + } else { + if (data.text !== "") { + alert(data.text); + window.location.reload(true); + } + } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError();