Blame view

js/init.js 3.94 KB
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
1
2
3
4
5
6
7
8
9
10
11
  // Constantes
  var JETON_TAILLE = 30 // Taille d'un jeton
  var JETON_DUREE = 10*60 // Temps de validité du jeton en secondes
  
  var TRANSACTION_CREATION = 1
  var TRANSACTION_RECHARGEMENT = 2
  var TRANSACTION_PAIEMENT = 3
  var TRANSACTION_VIDANGE = 4
  
  var TRANSACTION_DUREE = 60
  
344a37ad   Geoffrey PREUD'HOMME   Création de clients
12
13
  var PEUT_NFC = false
  
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
14
  // Fonctions pour Materialize
bf26706c   Geoffrey PREUD'HOMME   Écran principal
15
16
17
  $(function(){
      $('.button-collapse').sideNav();
      $('.modal-trigger').leanModal()
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
18
  });
bf26706c   Geoffrey PREUD'HOMME   Écran principal
19
  
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
20
  // Application
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
21
  
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
22
23
24
  var app = new Vue({
      el: 'body',
      data: {
344a37ad   Geoffrey PREUD'HOMME   Création de clients
25
26
27
          // Constantes
          PEUT_NFC: PEUT_NFC,
          // Affichage
e5e2f19a   Geoffrey PREUD'HOMME   Mutli-pages
28
          page: 'connexion',
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
29
30
          erreurTitre: '',
          erreurMessage: '',
344a37ad   Geoffrey PREUD'HOMME   Création de clients
31
32
          // Session
          connecte: false,
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
33
34
35
36
37
38
39
40
41
          date: 1,
      },
      methods: {
          // API
          apiBrute: function(chemin, donnees, cb) {
              $.post('api/' + chemin, donnees, function(data) {
                  cb(data['status'], data);
              })
          },
344a37ad   Geoffrey PREUD'HOMME   Création de clients
42
43
44
45
          api: function(chemin, donnees, cb) {
              donnees['jeton'] = this.jeton
              this.apiBrute(chemin, donnees, cb)
          },
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
          // Affichage
          toast: function(texte) {
              Materialize.toast(texte, 4000);
          },
          erreur: function(retour, donnees) {
              this.erreurTitre = retour
              this.erreurMessage = donnees['message']
              $("#modalErreur").openModal();
          },
          // Fonctionnement
          connecter: function() {
              if (!this.peutConnecter) return
              var that = this;
              this.apiBrute("utilisateur/connexion", {login: this.login , mdp: this.mdp} , function(retour, donnees) {
                  that.mdp = ''
                  switch(retour) {
                      case "ok":
                          that.login = donnees.login
                          that.droit = donnees.droit
                          that.jeton = donnees.jeton
                          that.connecte = that.date
e5e2f19a   Geoffrey PREUD'HOMME   Mutli-pages
67
68
                          that.toast("Correctement identifié en tant que " + that.login + " pour " + JETON_DUREE/60+ " minutes")
                          that.page = 'operations'
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
69
                         break;
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
70
  
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
71
72
73
                      case "identifiants_invalides":
                          that.toast("Identifiants invalides")
                          break;
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
74
  
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
75
76
77
78
79
80
                      default:
                          that.erreur(retour, donnees);
                          break;
                  }
              })
          },
344a37ad   Geoffrey PREUD'HOMME   Création de clients
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
          creer: function() {
              if (!this.peutCreer) return
              var that = this
              this.api("client/ajouter", {idCarte: this.idCarte, solde: this.solde}, function(retour, donnees) {
                  switch(retour) {
                      case "ok":
                          that.toast("Client " + that.idCarte + " crée avec un solde de " + that.solde + " €")
                          break;
  
                      case "solde_negatif":
                          that.toast("Solde négatif")
                          break;
  
                      default:
                          that.erreur(retour, donnees);
                          break;
                  }
              });
          }
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
100
101
102
103
104
      },
      computed: {
          peutConnecter: function() {
              return this.login && this.mdp;
          },
344a37ad   Geoffrey PREUD'HOMME   Création de clients
105
106
107
          peutCreer: function() {
              return this.solde && (this.PEUT_NFC || this.idCarte)
          },
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
108
109
          timer: function() {
              var secondes = this.connecte + JETON_DUREE - this.date
e5e2f19a   Geoffrey PREUD'HOMME   Mutli-pages
110
111
112
              var minutes = Math.floor(secondes/60)
              var secondes = secondes % 60
              return  minutes + ':' + (secondes < 10 ? '0' : '') + secondes
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
113
114
115
          }
      },
  })
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
116
  
2146d35a   Geoffrey PREUD'HOMME   Vue interactive
117
118
119
  setInterval(function actualiserDate() {
      app.$data.date = Math.floor(Date.now()/1000)
  }, 1000);
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
120
  
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...
121
122
  
  // Placeholder
bf26706c   Geoffrey PREUD'HOMME   Écran principal
123
124
125
126
127
128
129
130
131
132
  function vendu() {
      var interieur = $("<span>").text("Vendu 1 bière à KAE1EET2YI (15,30 € → 13,50 €) ").append(
              $("<a>").attr("href", "#!").text("Annuler")
          );
      Materialize.toast(interieur, 4000);
  }
  
  function soldeInsuffisant() {
      $("#soldeInsuffisant").openModal();
  }
79bb411d   Geoffrey PREUD'HOMME   Possibilité de se...