pcpuma_unam_operador/components/operador/Login.vue

193 lines
4.6 KiB
Vue
Raw Normal View History

2022-05-25 03:38:17 +00:00
<template>
2022-06-16 04:02:08 +00:00
<div
class="full-h is-flex is-justify-content-center is-align-items-center my-5"
>
2022-05-25 03:38:17 +00:00
<form class="box">
<div class="has-text-centered">
<h2 class="is-size-1">PC Puma</h2>
</div>
<b-field label="Usuario" :type="error">
2022-05-31 04:22:49 +00:00
<b-input v-model="usuario" @keyup.enter.native="login()" />
2022-05-25 03:38:17 +00:00
</b-field>
<b-field label="Contraseña" :type="error">
<b-input
type="password"
2022-05-30 23:08:35 +00:00
password-reveal
2022-05-31 04:22:49 +00:00
v-model="password"
@keyup.enter.native="login()"
2022-05-25 03:38:17 +00:00
/>
</b-field>
2022-05-31 18:57:35 +00:00
<b-field label="Institucion" :type="error">
<b-select v-model="idInstitucion" expanded>
<option value="" disabled>Institucion</option>
<option
v-for="(ins, i) in instituciones"
:key="i"
:value="ins.id_institucion"
>
{{ ins.institucion }}
</option>
</b-select>
</b-field>
<b-field label="Modulo" :type="error">
2022-05-31 04:22:49 +00:00
<b-select v-model="idModulo" expanded>
<option value="" disabled>Módulo</option>
2022-05-31 18:57:35 +00:00
<option v-for="(m, i) in modulos" :key="i" :value="m.id_modulo">
2022-05-31 04:22:49 +00:00
{{ m.modulo }}
</option>
</b-select>
2022-05-31 18:57:35 +00:00
</b-field>
2022-05-31 04:22:49 +00:00
2022-05-25 03:38:17 +00:00
<div class="has-text-centered">
<b-button
@click="login()"
type="is-success"
2022-08-08 06:43:19 +00:00
:disabled="error || !usuario || !password || !idModulo"
2022-05-25 03:38:17 +00:00
>
Iniciar Sesión
</b-button>
</div>
2022-08-09 18:20:37 +00:00
2022-08-09 19:08:18 +00:00
<recaptcha />
2022-05-25 03:38:17 +00:00
</form>
</div>
</template>
<script>
import axios from 'axios'
2022-08-09 18:20:37 +00:00
//import { VueRecaptcha } from 'vue-recaptcha';
2022-05-25 03:38:17 +00:00
export default {
props: {
updateIsLoading: { type: Function, required: true },
},
data() {
return {
2022-05-30 23:08:35 +00:00
error: '',
2022-05-31 04:22:49 +00:00
usuario: '',
2022-05-31 18:57:35 +00:00
password: '',
idModulo: '',
idInstitucion: '',
2022-05-31 04:22:49 +00:00
modulos: [],
2022-05-31 18:57:35 +00:00
instituciones: [],
2022-05-25 03:38:17 +00:00
}
},
methods: {
2022-08-09 18:35:43 +00:00
async login() {
2022-08-09 19:08:18 +00:00
console.log('ReCaptcha token')
const token = await this.$recaptcha.getResponse().catch((err) => {
console.log(err)
})
console.log('ReCaptcha token:', token)
if (this.usuario && this.password && token && !this.error) {
console.log('hola')
2022-06-27 21:59:35 +00:00
const data = {
operador: this.usuario,
password: this.password,
id_modulo: this.idModulo,
2022-05-25 03:38:17 +00:00
}
this.updateIsLoading(true)
axios
2022-06-27 21:59:35 +00:00
.post(`${process.env.api}/auth/login-operador`, data)
2022-08-09 19:08:18 +00:00
.then(async (res) => {
2022-07-29 19:34:21 +00:00
const token = res.data.token
const operador = JSON.stringify(res.data.operador)
localStorage.setItem('operador', operador)
localStorage.setItem('token', token)
localStorage.setItem('idModulo', this.idModulo)
2022-08-09 19:08:18 +00:00
await this.$recaptcha.reset()
2022-05-25 03:38:17 +00:00
this.updateIsLoading(false)
2022-07-29 15:23:07 +00:00
this.$router.push('/prestamo_devolucion')
2022-05-25 03:38:17 +00:00
})
.catch((err) => {
this.error = 'is-danger'
this.updateIsLoading(false)
2022-08-05 23:41:37 +00:00
this.$alertsGenericos.imprimirError(
this.$buefy,
this.$router,
err.response.data
)
2022-05-25 03:38:17 +00:00
})
}
},
2022-05-31 18:57:35 +00:00
obtenerCatalogoInstitucion() {
axios
2022-08-01 08:01:49 +00:00
.get(`${process.env.api}/institucion/instituciones-activas`)
2022-05-31 18:57:35 +00:00
.then((res) => {
this.instituciones = res.data
})
.catch((err) => {
2022-08-05 23:41:37 +00:00
this.$alertsGenericos.imprimirError(
this.$buefy,
this.$router,
err.response.data
)
2022-05-31 18:57:35 +00:00
})
},
2022-05-31 04:22:49 +00:00
obtenerCatalogoModulo() {
axios
2022-06-13 18:37:02 +00:00
.get(
2022-08-08 06:43:19 +00:00
`${process.env.api}/modulo/modulos-activos?id_institucion=${this.idInstitucion}`
2022-06-13 18:37:02 +00:00
)
2022-05-31 04:22:49 +00:00
.then((res) => {
this.modulos = res.data
})
.catch((err) => {
2022-08-05 23:41:37 +00:00
this.$alertsGenericos.imprimirError(
this.$buefy,
this.$router,
err.response.data
)
2022-05-31 04:22:49 +00:00
})
},
2022-08-09 19:08:18 +00:00
/*
2022-08-09 18:20:37 +00:00
async onSubmit() {
try {
const token = await this.$recaptcha.getResponse()
console.log('ReCaptcha token:', token)
// send token to server alongside your form data
// at the end you need to reset recaptcha
await this.$recaptcha.reset()
} catch (error) {
console.log('Login error:', error)
}
}, */
2022-05-25 03:38:17 +00:00
},
watch: {
password() {
if (this.error) this.error = ''
},
usuario() {
if (this.error) this.error = ''
},
2022-05-31 18:57:35 +00:00
idInstitucion() {
this.obtenerCatalogoModulo()
},
2022-05-25 03:38:17 +00:00
},
2022-08-02 14:47:06 +00:00
created() {
this.obtenerCatalogoInstitucion()
},
2022-08-09 18:35:43 +00:00
beforeDestroy() {
2022-08-09 19:08:18 +00:00
this.$recaptcha.destroy()
},
2022-05-25 03:38:17 +00:00
}
</script>
<style scoped>
.full-h {
height: 75vh;
}
form {
width: 30rem;
}
</style>