pcpuma_unam_operador/plugins/inject.js
2023-05-30 15:58:01 -06:00

123 lines
2.4 KiB
JavaScript

const alertsGenericos = {
imprimirError(
buefy,
router,
err = {},
title = '¡Hubo un error!',
onConfirm = () => {}
) {
let mensaje = ''
if (err.message === 'Unauthorized')
mensaje = 'Tu sesión se terminó. Si deseas continuar logueate nuevamente.'
else mensaje = err.message
buefy.dialog.alert({
ariaModal: true,
hasIcon: true,
onConfirm,
ariaRole: 'alertdialog',
confirmText: 'Entendido',
icon: 'alert-octagon',
iconPack: 'mdi',
message: mensaje,
title,
type: 'is-danger',
})
if (err.message === 'Unauthorized') {
localStorage.clear()
router.push('/')
}
},
imprimirMensaje(
buefy,
message,
onConfirm = () => {},
title = '¡Felicidades!'
) {
buefy.dialog.alert({
ariaModal: true,
hasIcon: true,
onConfirm,
ariaRole: 'alertdialog',
confirmText: 'Ok',
icon: 'check-circle',
iconPack: 'mdi',
message,
title,
type: 'is-success',
})
},
imprimirWarning(
buefy,
message,
onConfirm = () => {},
title = '¡Espera un minuto!',
onCancel = () => {}
) {
buefy.dialog.alert({
ariaModal: true,
canCancel: ["button"],
hasIcon: true,
onCancel,
onConfirm,
ariaRole: 'alertdialog',
cancelText: 'Cancelar',
confirmText: 'Confirmar',
icon: 'help-circle',
iconPack: 'mdi',
message,
title,
type: 'is-warning',
})
},
}
const getToken = {
token() {
const token = this.tokenStr()
if (!token) return null
return {
headers: {
Authorization: `Bearer ${token}`,
},
}
},
tokenDelete(data) {
const token = this.tokenStr()
if (!token) return null
return {
headers: {
Authorization: `Bearer ${token}`,
},
data,
}
},
tokenStr() {
return localStorage.getItem('token')
},
tokenFile() {
const token = this.tokenStr()
if (!token) return null
return {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${token}`,
},
}
},
}
const funcionesGlobales = {
objIsEmpty(obj) {
for (let key in obj) if (obj.hasOwnProperty(key)) return false
return true
},
}
export default ({ app }, inject) => {
inject('alertsGenericos', alertsGenericos)
inject('getToken', getToken)
inject('funcionesGlobales', funcionesGlobales)
}