Fix: send request / receive json data

This commit is contained in:
2018-07-10 18:03:46 +09:00
parent 7fe64c7a45
commit b5b56e8c2d
8 changed files with 175 additions and 87 deletions
-10
View File
@@ -1,10 +0,0 @@
<?php
function send_error_code($error_code) {
$returnValue = array(
'error' => $error_code
);
echo json_encode($returnValue, JSON_UNESCAPED_UNICODE);
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
$jsonReply = null;
function set_error_message($message) {
global $jsonReply;
$jsonReply["error"] = $message;
}
function set_data($name, $data) {
global $jsonReply;
$jsonReply[$name] = $data;
}
function send_result_success() {
global $jsonReply;
global $db_conn;
$jsonReply["result"] = "success";
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
$db_conn->close();
}
function send_result_fail() {
global $jsonReply;
global $db_conn;
$jsonReply["result"] = "fail";
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
$db_conn->close();
}
?>
+15 -15
View File
@@ -1,9 +1,11 @@
<?php
header('Content-Type: application/json');
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
$maestro_name = $_POST["maestro_name"];
$password = $_POST["password"];
/*
if(!is_numeric($enterCode)) {
send_error_code('생년월일이 숫자가 아닙니다. : '.$enterCode);
@@ -14,28 +16,26 @@ if(!is_numeric($enterCode)) {
}
*/
include "./../setup/connect_db.php";
$maestro_id = get_maestro_id($maestro_name);
if($maestro_id === null) {
$replyJSON["ERROR"] = "마에스트로 아이디가 없습니다";
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
set_error_message("등록되지 않은 마에스트로 아이디니다.");
send_result_fail();
exit;
}
$replyJSON = login($maestro_name, $password);
if($replyJSON === null || $replyJSON[MaestroID] === null) {
$replyJSON["ERROR"] = "비밀번호를 잘못 입력하셨습니다.";
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
$login_data = login($maestro_name, $password);
if($login_data === null || $login_data[maestroID] === null) {
set_error_message("비밀번호가 틀렸습니다.");
send_result_fail();
exit;
}
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
set_data("maestroId", $login_data[maestroID]);
send_result_success();
exit;
function get_maestro_id($maestro_name) {
global $db_conn;
@@ -65,8 +65,8 @@ function login($maestro_name, $password) {
$stmt->fetch();
$stmt->close();
$replyJSON["MaestroID"] = $maestro_id;
return $replyJSON;
$result["maestroID"] = $maestro_id;
return $result;
}
?>
+8 -19
View File
@@ -1,7 +1,7 @@
<?php
// load service db setting
$hasServiceDBSetting = file_exists("./../setup/service_db_setting.php");
if($hasServiceDBSetting == true) {
include("./../setup/service_db_setting.php");
} else {
@@ -11,27 +11,16 @@ if($hasServiceDBSetting == true) {
$db_name = "jisangs";
}
// connect db
$db_conn = new mysqli($db_host, $db_user, $db_passwd, $db_name);
if ($db_conn->connect_error) {
send_error_code("Connection failed: ".$db_conn->connect_error);
set_error_message("Failed to connect DB: ".$db_conn->connect_error);
send_result_fail();
exit;
}
$query = "USE jisangs";
$result = mysqli_query($db_conn, $query);
function send_error_message($replyJSON, $error_message) {
$replyJSON["ERROR"] = $error_message;
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
// $db_conn->close();
}
function send_result_message($replyJSON, $result_message) {
$replyJSON["RESULT"] = $result_message;
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
// $db_conn->close();
} else {
$query = "USE jisangs";
$result = mysqli_query($db_conn, $query);
}
?>