index.jsp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="tag" tagdir="/WEB-INF/tags" %>
  4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  5. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  6. <c:set var="req" value="${pageContext.request}" />
  7. <c:set var="url">${req.requestURL}</c:set>
  8. <c:set var="base" value="${fn:substring(url, 0, fn:length(url) - fn:length(req.requestURI))}${req.contextPath}/" />
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <tag:head title="Scan" />
  13. </head>
  14. <body class="grey lighten-3">
  15. <!-- Header -->
  16. <header class="navbar-fixed">
  17. <nav class="red">
  18. <div class="nav-wrapper">
  19. <span class="brand-logo center mtop-half hide-on-med-and-down"><img src="../img/logo.png"
  20. title="Logo Microfolie" class="responsive-img" style="max-width: 128px;" /></span>
  21. <span class="brand-logo center hide-on-large-only">Microfolie Lens</span>
  22. <div class="left mleft-2 hide-on-med-and-down">
  23. <span>
  24. <h5>Microfolie Lens</h5>
  25. </span>
  26. </div>
  27. </div>
  28. </nav>
  29. </header>
  30. <div class="mtop-5 hide-on-med-and-down"></div>
  31. <!-- Main -->
  32. <main class="container">
  33. <div class="row">
  34. <div class="col s12 center-align">
  35. <h3 id="title">QR Code Scanner</h3>
  36. </div>
  37. </div>
  38. <div class="row white z-depth-1">
  39. <div class="col s12">
  40. <ul class="tabs">
  41. <li class="tab col s6"><a href="#scan" class="active">Scanner</a></li>
  42. <li class="tab col s6"><a href="#code" class="active">Code</a></li>
  43. </ul>
  44. </div>
  45. <div id="scan" class="col s12">
  46. <div class="row">
  47. <div class="col s12 center-align">
  48. <video id="qr-preview"></video>
  49. </div>
  50. </div>
  51. </div>
  52. <div id="code" class="col s12">
  53. <div class="row">
  54. <div class="col s12">
  55. <div class="row mtop-5">
  56. <div class="input-field col s8 offset-s2 m6 offset-m3">
  57. <i class="material-icons prefix">person</i>
  58. <input id="code-input" type="text">
  59. <label for="code-input">Code</label>
  60. <span id="code-helper" class="helper-text"></span>
  61. </div>
  62. </div>
  63. <div class="row mbot-5">
  64. <div class="col s12 center-align">
  65. <button id="code-btn" class="waves-effect waves-light btn" disabled>
  66. <i class="material-icons left">check</i>Valider
  67. </button>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </main>
  75. <!-- Popup -->
  76. <div id="ok-popup" class="modal popup">
  77. <div class="modal-content" style="overflow: hidden;">
  78. <div class="row">
  79. <div class="col offset-m1 m3 s12">
  80. <i class="material-icons green-text" style="font-size: 8em;">check_circle</i>
  81. </div>
  82. <div class="col m7 s12 mtop-3">
  83. <h4 id="ok-text">Success message</h4>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. <div id="err-popup" class="modal popup">
  89. <div class="modal-content" style="overflow: hidden;">
  90. <div class="row">
  91. <div class="col offset-m1 m3 s12">
  92. <i class="material-icons red-text" style="font-size: 8em;">error</i>
  93. </div>
  94. <div class="col m7 s12 mtop-3">
  95. <h4 id="error-text">Error message</h4>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. <!-- Loader -->
  101. <tag:loader name="loader" />
  102. <!-- Script -->
  103. <tag:script />
  104. <script type="text/javascript" src="../js/instascan.min.js"></script>
  105. <script type="text/javascript">
  106. function sendCode(code, espace, nom, loader, okPopup, errPopup) {
  107. loader.open();
  108. const result = espace ? espace + "/" + code : code;
  109. $.ajax({
  110. type: "POST",
  111. url: "${base}api/badge/" + result,
  112. error: () => {
  113. $("#error-text").html('Une erreur est survenue');
  114. loader.close();
  115. errPopup.open();
  116. },
  117. success: (result) => {
  118. loader.close();
  119. if (result.success) {
  120. $("#ok-text").html(result.data.msg.replace("#ESPACE#", nom));
  121. okPopup.open();
  122. } else {
  123. $("#error-text").html(result.data);
  124. errPopup.open();
  125. }
  126. },
  127. });
  128. }
  129. $(document).ready(() => {
  130. const okPopup = M.Modal.getInstance($('#ok-popup'));
  131. const errPopup = M.Modal.getInstance($('#err-popup'));
  132. const loader = M.Modal.getInstance($('#loader'));
  133. // Lecteur cookie espace
  134. const espace = getCookie("microfolies.lens.espace");
  135. // Si pas de cookie retour sur la page d'accueil
  136. if (espace === undefined) {
  137. window.location.href = "${base}";
  138. }
  139. // Recupération du nom reel de l'espace pour le titre
  140. loader.open();
  141. let nom = espace.capitalize();
  142. $.ajax({
  143. type: "GET",
  144. url: "${base}api/espace/get/" + espace,
  145. error: () => {
  146. loader.close();
  147. $('#title').html(nom);
  148. },
  149. success: (result) => {
  150. loader.close();
  151. nom = result.data.libelle.capitalize();
  152. $('#title').html(nom);
  153. },
  154. });
  155. // Scanner QRcode
  156. const scanner = new Instascan.Scanner({ video: document.getElementById('qr-preview') });
  157. scanner.addListener('scan', function (content) {
  158. sendCode(content, espace, nom, loader, okPopup, errPopup);
  159. });
  160. Instascan.Camera.getCameras().then(function (cameras) {
  161. if (cameras.length > 0) {
  162. scanner.start(cameras[0]);
  163. }
  164. else {
  165. console.error('No cameras found.');
  166. }
  167. }).catch(err => {
  168. console.error(err);
  169. });
  170. // Formulaire
  171. $('#code-input').on('keyup', function() {
  172. const regex = /[A-Za-z0-9]{4}/gm;
  173. if (regex.test($(this).val()) && $(this).val().length == 4) {
  174. $(this).addClass('valid');
  175. $(this).removeClass('invalid');
  176. $("#code-btn").attr('disabled', false);
  177. } else {
  178. $(this).addClass('invalid');
  179. $(this).removeClass('valid');
  180. $("#code-helper").attr('data-error', 'Code invalide, exemple de code : Az6y');
  181. $("#code-btn").attr('disabled', true);
  182. }
  183. });
  184. $('#code-btn').on('click', () => {
  185. sendCode($('#code-input').val(), espace, nom, loader, okPopup, errPopup);
  186. $('#code-input').val('');
  187. });
  188. });
  189. </script>
  190. </body>
  191. </html>