add axios plugin for global options

This commit is contained in:
Jonathan Baecker 2020-04-30 10:53:13 +02:00
parent b43fe21b3d
commit b85131101a
10 changed files with 58 additions and 73 deletions

View File

@ -43,6 +43,7 @@ export default {
** Plugins to load before mounting the App ** Plugins to load before mounting the App
*/ */
plugins: [ plugins: [
{ src: '~/plugins/axios' },
{ src: '~/plugins/filters' }, { src: '~/plugins/filters' },
{ src: '~plugins/video.js', ssr: false }, { src: '~plugins/video.js', ssr: false },
{ src: '~plugins/scrollbar.js', ssr: false }, { src: '~plugins/scrollbar.js', ssr: false },

View File

@ -130,8 +130,8 @@
<div class="actions"> <div class="actions">
<b-button-group class="actions-grp"> <b-button-group class="actions-grp">
<b-button to="/control" variant="primary"> <b-button to="/player" variant="primary">
Control Player
</b-button> </b-button>
<b-button to="/media" variant="primary"> <b-button to="/media" variant="primary">
Media Media
@ -212,13 +212,13 @@ export default {
}, },
async sysStats () { async sysStats () {
const response = await this.$axios.get('api/stats/?stats=all', { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken }, progress: false }) const response = await this.$axios.get('api/stats/?stats=all')
this.stat = response.data this.stat = response.data
if (process.browser) { if (process.browser) {
this.interval = setInterval(async () => { this.interval = setInterval(async () => {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
const response = await this.$axios.get('api/stats/?stats=all', { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken }, progress: false }) const response = await this.$axios.get('api/stats/?stats=all')
this.stat = response.data this.stat = response.data
}, 2000) }, 2000)
} }

View File

@ -75,10 +75,7 @@ export default {
methods: { methods: {
async getLog (type) { async getLog (type) {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
const response = await this.$axios.get( const response = await this.$axios.get(`api/log/?type=${type}`)
`api/log/?type=${type}`,
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (response.data.log) { if (response.data.log) {
this.currentLog = response.data.log this.currentLog = response.data.log
@ -86,11 +83,7 @@ export default {
}, },
async getSystemLog () { async getSystemLog () {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
const response = await this.$axios.post( const response = await this.$axios.post('api/system/', { run: 'log' })
'api/system/',
{ run: 'log' },
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (response.data.data) { if (response.data.data) {
this.currentLog = response.data.data this.currentLog = response.data.data

View File

@ -277,8 +277,7 @@ export default {
await this.$axios.post( await this.$axios.post(
'api/media/op/', 'api/media/op/',
{ folder: this.folderName, path: this.crumbs.map(e => e.text).join('/') }, { folder: this.folderName, path: this.crumbs.map(e => e.text).join('/') }
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
) )
this.$root.$emit('bv::hide::modal', 'folder-modal') this.$root.$emit('bv::hide::modal', 'folder-modal')
@ -307,14 +306,13 @@ export default {
async onSubmitUpload (evt) { async onSubmitUpload (evt) {
evt.preventDefault() evt.preventDefault()
await this.$store.dispatch('auth/inspectToken')
const uploadProgress = fileName => (progressEvent) => { const uploadProgress = fileName => (progressEvent) => {
const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total) const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)
this.currentProgress = progress this.currentProgress = progress
} }
for (const [i, file] of this.inputFiles.entries()) { for (const [i, file] of this.inputFiles.entries()) {
await this.$store.dispatch('auth/inspectToken')
this.uploadTask = file.name this.uploadTask = file.name
const config = { const config = {
@ -330,8 +328,7 @@ export default {
) )
.then( .then(
this.overallProgress = (i + 1) * 100 / this.inputFiles.length, this.overallProgress = (i + 1) * 100 / this.inputFiles.length,
this.currentProgress = 0, this.currentProgress = 0
await this.$store.dispatch('auth/inspectToken')
) )
.catch(err => console.log(err)) .catch(err => console.log(err))
} }
@ -406,10 +403,7 @@ export default {
pathName = this.deleteSource pathName = this.deleteSource
} }
await this.$axios.delete( await this.$axios.delete(`api/media/op/?file=${encodeURIComponent(file)}&path=${encodeURIComponent(pathName)}`)
`api/media/op/?file=${encodeURIComponent(file)}&path=${encodeURIComponent(pathName)}`,
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
.catch(err => console.log(err)) .catch(err => console.log(err))
this.$root.$emit('bv::hide::modal', 'delete-modal') this.$root.$emit('bv::hide::modal', 'delete-modal')

View File

@ -281,7 +281,7 @@ export default {
if (preset) { if (preset) {
req = `?name=${preset}` req = `?name=${preset}`
} }
const response = await this.$axios.get(`api/messenger/${req}`, { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }) const response = await this.$axios.get(`api/messenger/${req}`)
if (response.data && !preset) { if (response.data && !preset) {
for (const item of response.data) { for (const item of response.data) {
@ -333,11 +333,7 @@ export default {
overall_alpha: this.form.overallAlpha overall_alpha: this.form.overallAlpha
} }
const response = await this.$axios.post( const response = await this.$axios.post('api/messenger/', preset)
'api/messenger/',
preset,
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (response.status === 201) { if (response.status === 201) {
this.success = true this.success = true
@ -369,11 +365,7 @@ export default {
overall_alpha: this.form.overallAlpha overall_alpha: this.form.overallAlpha
} }
const response = await this.$axios.put( const response = await this.$axios.put(`api/messenger/${this.form.id}/`, preset)
`api/messenger/${this.form.id}/`,
preset,
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (response.status === 200) { if (response.status === 200) {
this.success = true this.success = true
@ -393,10 +385,7 @@ export default {
async deletePreset () { async deletePreset () {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
if (this.selected) { if (this.selected) {
await this.$axios.delete( await this.$axios.delete(`api/messenger/${this.form.id}/`)
`api/messenger/${this.form.id}/`,
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
} }
this.$bvModal.hide('delete-modal') this.$bvModal.hide('delete-modal')
@ -422,11 +411,7 @@ export default {
boxborderw: this.form.border boxborderw: this.form.border
} }
const response = await this.$axios.post( const response = await this.$axios.post('api/send/', { data: obj })
'api/send/',
{ data: obj },
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (response.data && response.data.status.Success && response.data.status.Success === '0 Success') { if (response.data && response.data.status.Success && response.data.status.Success === '0 Success') {
this.success = true this.success = true

View File

@ -366,11 +366,7 @@ export default {
async getStatus () { async getStatus () {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
const status = await this.$axios.post( const status = await this.$axios.post('api/system/', { run: 'status' })
'api/system/',
{ run: 'status' },
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
if (status.data.data && status.data.data === 'active') { if (status.data.data && status.data.data === 'active') {
this.isPlaying = 'is-playing' this.isPlaying = 'is-playing'
@ -380,11 +376,7 @@ export default {
}, },
async playoutControl (state) { async playoutControl (state) {
await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('auth/inspectToken')
await this.$axios.post( await this.$axios.post('api/system/', { run: state })
'api/system/',
{ run: state },
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
setTimeout(() => { this.getStatus() }, 1000) setTimeout(() => { this.getStatus() }, 1000)
}, },
@ -450,8 +442,7 @@ export default {
await this.$axios.post( await this.$axios.post(
'api/playlist/', 'api/playlist/',
{ data: { channel: this.$store.state.playlist.playlistChannel, date: this.today, program: saveList } }, { data: { channel: this.$store.state.playlist.playlistChannel, date: this.today, program: saveList } }
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
) )
} }
} }

View File

@ -0,0 +1,21 @@
export default function ({ $axios, store, redirect }) {
$axios.onRequest((config) => {
const token = store.state.auth.jwtToken
if (token) {
config.headers.common.Authorization = `Bearer ${token}`
}
// disable progress on auth and stats
if (config.url.includes('stats') || config.url.includes('auth')) {
config.progress = false
}
})
$axios.onError((error) => {
const code = parseInt(error.response && error.response.status)
if (code === 400) {
redirect('/')
}
})
}

View File

@ -25,9 +25,9 @@ export const mutations = {
} }
export const actions = { export const actions = {
async getGuiConfig ({ commit, state, rootState }) { async getGuiConfig ({ commit, state }) {
const options = await this.$axios.options('api/guisettings/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const options = await this.$axios.options('api/guisettings/')
const response = await this.$axios.get('api/guisettings/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const response = await this.$axios.get('api/guisettings/')
if (options.data) { if (options.data) {
const choices = options.data.actions.POST.net_interface.choices.map(function (obj) { const choices = options.data.actions.POST.net_interface.choices.map(function (obj) {
@ -43,29 +43,29 @@ export const actions = {
} }
}, },
async setGuiConfig ({ commit, state, rootState }, obj) { async setGuiConfig ({ commit, state }, obj) {
const stringObj = JSON.parse(JSON.stringify(obj)) const stringObj = JSON.parse(JSON.stringify(obj))
stringObj.extra_extensions = obj.extra_extensions.join(' ') stringObj.extra_extensions = obj.extra_extensions.join(' ')
const update = await this.$axios.put('api/guisettings/1/', stringObj, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const update = await this.$axios.put('api/guisettings/1/', stringObj)
return update return update
}, },
async getPlayoutConfig ({ commit, state, rootState }) { async getPlayoutConfig ({ commit, state }) {
const response = await this.$axios.get('api/config/?configPlayout', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const response = await this.$axios.get('api/config/?configPlayout')
if (response.data) { if (response.data) {
commit('UPDATE_PLAYLOUT_CONFIG', response.data) commit('UPDATE_PLAYLOUT_CONFIG', response.data)
} }
}, },
async setPlayoutConfig ({ commit, state, rootState }, obj) { async setPlayoutConfig ({ commit, state }, obj) {
const update = await this.$axios.post('api/config/?configPlayout', { data: obj }, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const update = await this.$axios.post('api/config/?configPlayout', { data: obj })
return update return update
}, },
async getUserConfig ({ commit, state, rootState }) { async getUserConfig ({ commit, state }) {
const user = await this.$axios.get('api/current/user/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const user = await this.$axios.get('api/current/user/')
const response = await this.$axios.get(`api/users/?username=${user.data.username}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const response = await this.$axios.get(`api/users/?username=${user.data.username}`)
if (user.data) { if (user.data) {
commit('SET_CURRENT_USER', user.data.username) commit('SET_CURRENT_USER', user.data.username)
@ -75,8 +75,8 @@ export const actions = {
} }
}, },
async setUserConfig ({ commit, state, rootState }, obj) { async setUserConfig ({ commit, state }, obj) {
const update = await this.$axios.put(`api/users/${obj.id}/`, obj, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const update = await this.$axios.put(`api/users/${obj.id}/`, obj)
return update return update
} }
} }

View File

@ -17,10 +17,10 @@ export const mutations = {
} }
export const actions = { export const actions = {
async getTree ({ commit, dispatch, state, rootState }, { extensions, path }) { async getTree ({ commit, dispatch, state }, { extensions, path }) {
const crumbs = [] const crumbs = []
let root = '/' let root = '/'
const response = await this.$axios.get(`api/media/?extensions=${extensions}&path=${path}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const response = await this.$axios.get(`api/media/?extensions=${extensions}&path=${path}`)
if (response.data.tree) { if (response.data.tree) {
const pathArr = response.data.tree[0].split('/') const pathArr = response.data.tree[0].split('/')

View File

@ -50,8 +50,8 @@ export const mutations = {
} }
export const actions = { export const actions = {
async getPlaylist ({ commit, dispatch, state, rootState }, { dayStart, date }) { async getPlaylist ({ commit, dispatch, state }, { dayStart, date }) {
const response = await this.$axios.get(`api/playlist/?date=${date}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) const response = await this.$axios.get(`api/playlist/?date=${date}`)
if (response.data && response.data.program) { if (response.data && response.data.program) {
commit('UPDATE_PLAYLIST_CHANNEL', response.data.channel) commit('UPDATE_PLAYLIST_CHANNEL', response.data.channel)