增加请求的全局拦截添加token,增加下载文件日志的封装函数
This commit is contained in:
parent
867b73bb5a
commit
bf6ef5bae2
@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<v-dialog v-model="showDialog" persistent :width="400">
|
<v-dialog v-model="showDialog" persistent :width="450">
|
||||||
<panel :title="titleText" :icon="mdiConnection" card-class="the-connection-dialog" :margin-bottom="false">
|
<panel :title="titleText" :icon="mdiConnection" card-class="the-connection-dialog" :margin-bottom="false">
|
||||||
<v-card-text v-if="connectingFailed" class="pt-5">
|
<v-card-text v-if="connectingFailed" class="pt-5">
|
||||||
<connection-status :moonraker="false" />
|
<connection-status :moonraker="false" />
|
||||||
|
@ -72,23 +72,15 @@
|
|||||||
</v-snackbar>
|
</v-snackbar>
|
||||||
<emergency-stop-dialog :show-dialog="showEmergencyStopDialog" @close="showEmergencyStopDialog = false" />
|
<emergency-stop-dialog :show-dialog="showEmergencyStopDialog" @close="showEmergencyStopDialog = false" />
|
||||||
<v-dialog v-model="dialog" max-width="290">
|
<v-dialog v-model="dialog" max-width="290">
|
||||||
<v-card color="white" style="color: black;">
|
<v-card color="#1e1e1e">
|
||||||
<v-card-title class="headings">
|
<v-card-title class="headings">logout</v-card-title>
|
||||||
Prompt
|
<v-card-text style="color:#FFF">Are you sure you want to log out?</v-card-text>
|
||||||
</v-card-title>
|
<v-card-actions>
|
||||||
<v-card-text style="color: black;">
|
<v-spacer />
|
||||||
Are you sure you want to log out?
|
<v-btn text style="color: #E53935" @click="dialog = false">NO</v-btn>
|
||||||
</v-card-text>
|
<v-btn text style="color: #43a047" @click="goLogot">YES</v-btn>
|
||||||
<v-card-actions>
|
</v-card-actions>
|
||||||
<v-spacer />
|
</v-card>
|
||||||
<v-btn text style="color: black;" @click="dialog = false">
|
|
||||||
NO
|
|
||||||
</v-btn>
|
|
||||||
<v-btn text style="color: black;" @click="goLogot">
|
|
||||||
YES
|
|
||||||
</v-btn>
|
|
||||||
</v-card-actions>
|
|
||||||
</v-card>
|
|
||||||
</v-dialog>
|
</v-dialog>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -249,17 +241,15 @@ export default class TheTopbar extends Mixins(BaseMixin, ThemeMixin) {
|
|||||||
this.naviDrawer = this.$vuetify.breakpoint.lgAndUp
|
this.naviDrawer = this.$vuetify.breakpoint.lgAndUp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dialog: boolean = false;
|
dialog: boolean = false
|
||||||
|
|
||||||
goLogot() {
|
goLogot() {
|
||||||
this.dialog = false;
|
this.$socket.emitAndWait('access.logout').then(({ res }) => {
|
||||||
this.$services.post('/access/logout').then((response) => {
|
localStorage.removeItem('token')
|
||||||
localStorage.removeItem('token');
|
this.$toast.success('logout successful', { position: 'top' })
|
||||||
this.$toast.success('logout successful', {
|
window.location.reload()
|
||||||
position: 'top'
|
})
|
||||||
});
|
|
||||||
window.location.reload();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
btnEmergencyStop() {
|
btnEmergencyStop() {
|
||||||
|
@ -253,7 +253,8 @@ export default class GcodefilesPanelTableRowFile extends Mixins(BaseMixin, Contr
|
|||||||
const filename = this.currentPath + '/' + this.item.filename
|
const filename = this.currentPath + '/' + this.item.filename
|
||||||
const href = this.apiUrl + '/server/files/gcodes' + escapePath(filename)
|
const href = this.apiUrl + '/server/files/gcodes' + escapePath(filename)
|
||||||
|
|
||||||
window.open(href)
|
// window.open(href)
|
||||||
|
this.$getDownloadZip(href,filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
editFile() {
|
editFile() {
|
||||||
|
@ -156,7 +156,8 @@ export default class KlippyStatePanel extends Mixins(BaseMixin) {
|
|||||||
if ('href' in event.target.attributes) href = event.target.attributes.href.value
|
if ('href' in event.target.attributes) href = event.target.attributes.href.value
|
||||||
if ('href' in event.target.parentElement.attributes) href = event.target.parentElement.attributes.href.value
|
if ('href' in event.target.parentElement.attributes) href = event.target.parentElement.attributes.href.value
|
||||||
|
|
||||||
window.open(href)
|
// window.open(href)
|
||||||
|
this.$getDownloadLog(href)
|
||||||
}
|
}
|
||||||
|
|
||||||
powerOn() {
|
powerOn() {
|
||||||
|
@ -1033,8 +1033,10 @@ export default class ConfigFilesPanel extends Mixins(BaseMixin, ThemeMixin) {
|
|||||||
|
|
||||||
downloadFile() {
|
downloadFile() {
|
||||||
const filename = this.absolutePath + '/' + this.contextMenu.item.filename
|
const filename = this.absolutePath + '/' + this.contextMenu.item.filename
|
||||||
const href = `${this.apiUrl}/server/files${escapePath(filename)}`
|
// const href = `${this.apiUrl}/server/files${escapePath(filename)}`
|
||||||
window.open(href)
|
// window.open(href)
|
||||||
|
const href = `/server/files${escapePath(filename)}`
|
||||||
|
this.$getDownloadZip(href,filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadSelectedFiles() {
|
async downloadSelectedFiles() {
|
||||||
|
@ -61,7 +61,8 @@ export default class LogfilesPanel extends Mixins(BaseMixin) {
|
|||||||
if ('href' in event.target.attributes) href = event.target.attributes.href.value
|
if ('href' in event.target.attributes) href = event.target.attributes.href.value
|
||||||
if ('href' in event.target.parentElement.attributes) href = event.target.parentElement.attributes.href.value
|
if ('href' in event.target.parentElement.attributes) href = event.target.parentElement.attributes.href.value
|
||||||
|
|
||||||
window.open(href)
|
// window.open(href)
|
||||||
|
this.$getDownloadLog(href)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -268,9 +268,12 @@ export default class StatusPanelGcodefilesEntry extends Mixins(BaseMixin, Contro
|
|||||||
}
|
}
|
||||||
|
|
||||||
downloadFile() {
|
downloadFile() {
|
||||||
const href = this.apiUrl + '/server/files/gcodes/' + escapePath(this.item.filename)
|
// const href = this.apiUrl + '/server/files/gcodes/' + escapePath(this.item.filename)
|
||||||
|
|
||||||
window.open(href)
|
// window.open(href)
|
||||||
|
|
||||||
|
const href = '/server/files/gcodes/' + escapePath(this.item.filename)
|
||||||
|
this.$getDownloadZip(href)
|
||||||
}
|
}
|
||||||
|
|
||||||
renameFile() {
|
renameFile() {
|
||||||
|
44
src/main.ts
44
src/main.ts
@ -40,8 +40,11 @@ import { defaultMode } from './store/variables'
|
|||||||
|
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import services from '@/utils/services'
|
import services from '@/utils/services'
|
||||||
|
import { getDownloadLog,getDownloadZip} from '@/utils/tool'
|
||||||
|
|
||||||
Vue.prototype.$services = services
|
Vue.prototype.$services = services
|
||||||
|
Vue.prototype.$getDownloadLog = getDownloadLog
|
||||||
|
Vue.prototype.$getDownloadZip = getDownloadZip
|
||||||
|
|
||||||
Vue.config.productionTip = false
|
Vue.config.productionTip = false
|
||||||
|
|
||||||
@ -55,6 +58,47 @@ Vue.use(VueToast, {
|
|||||||
duration: 3000,
|
duration: 3000,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
axios.interceptors.request.use((config) => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (token && !config.headers.Authorization) {
|
||||||
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
}, error => {
|
||||||
|
return Promise.reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
const originalFetch = window.fetch;
|
||||||
|
window.fetch = async function (url, options: RequestInit = {}) {
|
||||||
|
try {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (token) {
|
||||||
|
if (!options.headers) {
|
||||||
|
options.headers = new Headers();
|
||||||
|
}
|
||||||
|
if (options.headers instanceof Headers) {
|
||||||
|
options.headers.set('Authorization', `Bearer ${token}`);
|
||||||
|
} else if (typeof options.headers === 'object' && !Array.isArray(options.headers)) {
|
||||||
|
(options.headers as Record<string, string>)['Authorization'] = `Bearer ${token}`;
|
||||||
|
} else {
|
||||||
|
const newHeaders = new Headers();
|
||||||
|
if (Array.isArray(options.headers)) {
|
||||||
|
options.headers.forEach(([key, value]) => newHeaders.set(key, value));
|
||||||
|
} else {
|
||||||
|
Object.entries(options.headers as Record<string, string>).forEach(([key, value]) => newHeaders.set(key, value));
|
||||||
|
}
|
||||||
|
newHeaders.set('Authorization', `Bearer ${token}`);
|
||||||
|
options.headers = newHeaders;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const response = await originalFetch.call(window, url, options);
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Fetch error:', error);
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const isSafari = navigator.userAgent.includes('Safari') && navigator.userAgent.search('Chrome') === -1
|
const isSafari = navigator.userAgent.includes('Safari') && navigator.userAgent.search('Chrome') === -1
|
||||||
const isTouch = 'ontouchstart' in window || (navigator.maxTouchPoints > 0 && navigator.maxTouchPoints !== 256)
|
const isTouch = 'ontouchstart' in window || (navigator.maxTouchPoints > 0 && navigator.maxTouchPoints !== 256)
|
||||||
Vue.use(OverlayScrollbarsPlugin, {
|
Vue.use(OverlayScrollbarsPlugin, {
|
||||||
|
@ -12,6 +12,7 @@ import i18n from '@/plugins/i18n'
|
|||||||
import { hiddenDirectories, validGcodeExtensions } from '@/store/variables'
|
import { hiddenDirectories, validGcodeExtensions } from '@/store/variables'
|
||||||
import axios, { AxiosProgressEvent } from 'axios'
|
import axios, { AxiosProgressEvent } from 'axios'
|
||||||
import { BatchMessage } from '@/plugins/webSocketClient'
|
import { BatchMessage } from '@/plugins/webSocketClient'
|
||||||
|
import { getDownloadZip } from '@/utils/tool'
|
||||||
|
|
||||||
export const actions: ActionTree<FileState, RootState> = {
|
export const actions: ActionTree<FileState, RootState> = {
|
||||||
reset({ commit }) {
|
reset({ commit }) {
|
||||||
@ -362,7 +363,10 @@ export const actions: ActionTree<FileState, RootState> = {
|
|||||||
downloadZip({ rootGetters }, payload) {
|
downloadZip({ rootGetters }, payload) {
|
||||||
const apiUrl = rootGetters['socket/getUrl']
|
const apiUrl = rootGetters['socket/getUrl']
|
||||||
const url = `${apiUrl}/server/files/${payload.destination.root}/${encodeURI(payload.destination.path)}`
|
const url = `${apiUrl}/server/files/${payload.destination.root}/${encodeURI(payload.destination.path)}`
|
||||||
window.open(url)
|
const urls = `/server/files/${payload.destination.root}/${encodeURI(payload.destination.path)}`
|
||||||
|
console.log(`获取下载的url`, urls);
|
||||||
|
|
||||||
|
getDownloadZip(urls);
|
||||||
},
|
},
|
||||||
|
|
||||||
rolloverLog(_, payload) {
|
rolloverLog(_, payload) {
|
||||||
|
@ -28,7 +28,7 @@ export const actions: ActionTree<ServerState, RootState> = {
|
|||||||
version: rootState.packageVersion,
|
version: rootState.packageVersion,
|
||||||
type: 'web',
|
type: 'web',
|
||||||
url: 'https://github.com/mainsail-crew/mainsail',
|
url: 'https://github.com/mainsail-crew/mainsail',
|
||||||
...(token ? { access_token: token }: {})
|
...(token ? { access_token: token } : {})
|
||||||
})
|
})
|
||||||
commit('setConnectionId', connection.connection_id)
|
commit('setConnectionId', connection.connection_id)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@ -36,7 +36,7 @@ export const actions: ActionTree<ServerState, RootState> = {
|
|||||||
this.dispatch('socket/setConnectionFailed', e.message)
|
this.dispatch('socket/setConnectionFailed', e.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.code === 400) { localStorage.removeItem("token"); window.location.reload() }
|
if (e.code === 400||e.message=='JWT Expired') { localStorage.removeItem("token"); window.location.reload() }
|
||||||
Vue.prototype.$toast.error(e.message, { position: 'top' });
|
Vue.prototype.$toast.error(e.message, { position: 'top' });
|
||||||
window.console.error('Error while identifying client: ' + e.message)
|
window.console.error('Error while identifying client: ' + e.message)
|
||||||
return
|
return
|
||||||
|
79
src/utils/tool.ts
Normal file
79
src/utils/tool.ts
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
import service from './services';
|
||||||
|
|
||||||
|
|
||||||
|
export const getDownloadLog = async (url: string): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const response = await service.get(url);
|
||||||
|
if (response) {
|
||||||
|
let urls = JSON.stringify(response.data)
|
||||||
|
const blob = new Blob([JSON.parse(urls)], { type: 'text/plain' });
|
||||||
|
const objectUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = objectUrl;
|
||||||
|
let time = getDateTime()
|
||||||
|
link.download = `${time}.log`;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
window.URL.revokeObjectURL(objectUrl);
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
console.error(error.message);
|
||||||
|
} else {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const getDownloadZip = async (url: string, filename?: string): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const response = await service.get(url, { responseType: 'blob' });
|
||||||
|
if (response) {
|
||||||
|
let type = 'application/zip'
|
||||||
|
if (filename) {
|
||||||
|
let index = filename.lastIndexOf('.')
|
||||||
|
if (index !== -1) {
|
||||||
|
type = 'application/' + filename.substring(index + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const blob = new Blob([response.data], { type: type });
|
||||||
|
const downloadUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = downloadUrl;
|
||||||
|
if (filename) {
|
||||||
|
link.setAttribute('download', filename);
|
||||||
|
} else {
|
||||||
|
let time = getDateTime()
|
||||||
|
link.setAttribute('download', `${time}`);
|
||||||
|
}
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(downloadUrl);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
console.error(error.message);
|
||||||
|
} else {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//获取当前时间
|
||||||
|
function getDateTime() {
|
||||||
|
let date = new Date();
|
||||||
|
let year = date.getFullYear();
|
||||||
|
let month = date.getMonth() + 1;
|
||||||
|
let day = date.getDate();
|
||||||
|
let hour = date.getHours();
|
||||||
|
let minute = date.getMinutes();
|
||||||
|
let second = date.getSeconds();
|
||||||
|
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user