set json data to qr code , at_school is using id as key

This commit is contained in:
Karel Košnar 2024-12-02 18:03:04 +01:00
parent ca11bae56c
commit 5505e23754
2 changed files with 49 additions and 19 deletions

View file

@ -3,10 +3,40 @@
<head> <head>
<script src="https://unpkg.com/html5-qrcode"></script> <script src="https://unpkg.com/html5-qrcode"></script>
</head> </head>
<style>
#reader {
position: relative;
}
#qr-reader {
position: absolute;
width:800px;
left: 0px;
top: 0px;
}
#wave {
position: absolute;
left: 0px;
top: 0px;
visibility: hidden;
width:800px;
height: 640px;
background-color: #cccccc;
background-image: url('wave.gif');
background-repeat: no-repeat;
background-size: auto;
background-position: center;
}
</style>
<body> <body>
<H1>Osvobozená základní škola - Docházka</H1> <H1>Osvobozená základní škola - Docházka</H1>
<H2>Ukaž svoji školní průkazku</H2> <H2>Ukaž svoji školní průkazku</H2>
<div id="qr-reader" style="width:800px; background-color: #cccccc;"></div> <div id="reader">
<div id="qr-reader" ></div>
<div id="wave"></div>
</div>
<script> <script>
@ -21,7 +51,7 @@
var at_school = JSON.parse(at_school_json); var at_school = JSON.parse(at_school_json);
console.log(at_school); console.log(at_school);
var result = "<ul>"; var result = "<ul>";
for (var i = 0; i < at_school.length; i++) { for(var i in at_school){
result += "<li>"+ at_school[i] + "</li>"; result += "<li>"+ at_school[i] + "</li>";
} }
result += "</ul>"; result += "</ul>";
@ -29,14 +59,16 @@
return result; return result;
} }
function onScanSuccess(decodedText, decodedResult) { function onScanSuccess(decodedText, decodedResult) {
// const card = JSON.parse(decodedText); const card = JSON.parse(decodedText);
// document.getElementById('qr-reader-results').textContent = card.salutation; var fullname = card.name+" "+card.surname;
document.getElementById('qr-reader-results').textContent = decodedText; document.getElementById('qr-reader-results').textContent = fullname;
html5QrcodeScanner.clear(); //document.getElementById('qr-reader-results').textContent = decodedText;
html5QrcodeScanner.pause();
document.getElementById('wave').style.visibility = "visible"; document.getElementById('wave').style.visibility = "visible";
var formData ={}; var formData ={};
formData.id=33; formData.id=card.id;
formData.name=decodedText; formData.name=fullname;
formData.direction=document.querySelector('input[name="entrance"]:checked').id; formData.direction=document.querySelector('input[name="entrance"]:checked').id;
formData.main=true; formData.main=true;
console.log(JSON.stringify(formData)); console.log(JSON.stringify(formData));
@ -46,12 +78,13 @@
}, },
body:JSON.stringify(formData) body:JSON.stringify(formData)
}).then(response => response.text()) }).then(response => response.text())
.then(data => document.querySelector("p.result").innerHTML = data); .then(data => document.getElementById('qr-reader-results').textContent = data);
//.then(data => document.querySelector("p.result").innerHTML = data);
setTimeout(() => { setTimeout(() => {
document.getElementById('qr-reader-results').textContent = "Načítám ..."; document.getElementById('qr-reader-results').textContent = "Načítám ...";
document.getElementById('wave').style.visibility = "hidden"; document.getElementById('wave').style.visibility = "hidden";
html5QrcodeScanner.render(onScanSuccess); html5QrcodeScanner.resume();
get_at_school(); get_at_school();
}, 4000); }, 4000);
@ -68,9 +101,7 @@
</label> </label>
<H3>Kdo jsi? </H3> <H3>Kdo jsi? </H3>
<p id="qr-reader-results">Načítám ...</p> <p id="qr-reader-results">Načítám ...</p>
<p class="result"></p>
<H3>Kdo je ve škole</H3> <H3>Kdo je ve škole</H3>
<p id="at-school">Načítám ...</p> <p id="at-school">Načítám ...</p>
<img style = "visibility:hidden" id = "wave" src="giphy.webp"/>
</body> </body>
</html> </html>

View file

@ -29,17 +29,16 @@ $pozadavek = json_decode($postdata);
//check if student is already at school //check if student is already at school
//if not, add him to the list //if not, add him to the list
if ($pozadavek->direction == "P") { if ($pozadavek->direction == "P") {
if( !in_array($pozadavek->name, $at_school)) { if( !isset($at_school[$pozadavek->id])) {
$at_school[] = $pozadavek->name; $at_school[$pozadavek->id] = $pozadavek->name;
} else { } else {
echo $pozadavek->name." už je ve škole"; echo $pozadavek->name." už je ve škole";
return; return;
} }
} else { } else {
//if student is already at school, remove him from the list //if student is already at school, remove him from the list
if( in_array($pozadavek->name, $at_school)) { if( isset($at_school[$pozadavek->id])) {
$key = array_search($pozadavek->name, $at_school); unset($at_school[$pozadavek->id]);
unset($at_school[$key]);
} else { } else {
echo $pozadavek->name." není ve škole"; echo $pozadavek->name." není ve škole";
return; return;
@ -58,8 +57,8 @@ $pruchod = array(
"CteckaId" => "CAM1", "CteckaId" => "CAM1",
"Poznamka" => "" "Poznamka" => ""
); );
$payload = json_encode($pruchod)."<br>"; $payload = json_encode($pruchod);
echo $payload; echo $pozadavek->name;
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$cesta); curl_setopt($ch, CURLOPT_URL, $url.$cesta);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);