init.js
3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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
var PEUT_NFC = false
// Fonctions pour Materialize
$(function(){
$('.button-collapse').sideNav();
$('.modal-trigger').leanModal()
});
// Application
var app = new Vue({
el: 'body',
data: {
// Constantes
PEUT_NFC: PEUT_NFC,
// Affichage
page: 'connexion',
erreurTitre: '',
erreurMessage: '',
// Session
connecte: false,
date: 1,
},
methods: {
// API
apiBrute: function(chemin, donnees, cb) {
$.post('api/' + chemin, donnees, function(data) {
cb(data['status'], data);
})
},
api: function(chemin, donnees, cb) {
donnees['jeton'] = this.jeton
this.apiBrute(chemin, donnees, cb)
},
// 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
that.toast("Correctement identifié en tant que " + that.login + " pour " + JETON_DUREE/60+ " minutes")
that.page = 'operations'
break;
case "identifiants_invalides":
that.toast("Identifiants invalides")
break;
default:
that.erreur(retour, donnees);
break;
}
})
},
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;
}
});
}
},
computed: {
peutConnecter: function() {
return this.login && this.mdp;
},
peutCreer: function() {
return this.solde && (this.PEUT_NFC || this.idCarte)
},
timer: function() {
var secondes = this.connecte + JETON_DUREE - this.date
var minutes = Math.floor(secondes/60)
var secondes = secondes % 60
return minutes + ':' + (secondes < 10 ? '0' : '') + secondes
}
},
})
setInterval(function actualiserDate() {
app.$data.date = Math.floor(Date.now()/1000)
}, 1000);
// Placeholder
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();
}