This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nezha-fronted/nezha-fronted/src/components/common/mixin/exportHtml.js

68 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-05-24 15:04:46 +08:00
import bus from '@/libs/bus'
2022-10-17 17:35:54 +08:00
import { Loading } from 'element-ui'
2022-04-11 15:49:10 +08:00
export default {
data () {
return {
fileName: 'panel'
}
},
methods: {
2022-05-24 15:04:46 +08:00
exportToHtml (name) {
2022-07-25 15:36:58 +08:00
const vars = this.$store.getters.getVariablesArr.map(item => {
return {
name: item.name,
value: item.checked.join('|')
2022-07-25 15:36:58 +08:00
}
})
2022-05-24 15:04:46 +08:00
const params = {
format: 'html',
panelId: this.showPanel.id,
start: this.$stringTimeParseToUnix(bus.formateTimeToTime(this.searchTime[0])),
2022-07-25 15:36:58 +08:00
end: this.$stringTimeParseToUnix(bus.formateTimeToTime(this.searchTime[1])),
vars: vars
2022-05-24 15:04:46 +08:00
}
2022-10-17 17:35:54 +08:00
const loading = Loading.service({
lock: true,
customClass: 'export-pdf-mask',
background: 'rgba(0,0,0,.2)'
})
2022-05-24 15:04:46 +08:00
this.$get('/visual/panel/snapshot', params, 'blob').then(res => {
2022-10-17 17:35:54 +08:00
loading.close()
2022-05-24 15:04:46 +08:00
const self = this
let fileName = name
const resFileName = ''
if (resFileName) {
fileName = resFileName
}
if (res.type == 'application/json') {
const reader = new FileReader() // 创建一个FileReader实例
reader.readAsText(res, 'utf-8') // 读取文件,结果用字符串形式表示
reader.onload = function () { // 读取完成后,**获取reader.result**
const { msg } = JSON.parse(reader.result)
self.$message.error(msg) // 弹出错误提示
2022-04-11 15:49:10 +08:00
}
2022-05-24 15:04:46 +08:00
return
}
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie11
const blobObject = new Blob([res])
window.navigator.msSaveOrOpenBlob(blobObject, fileName + '.html')
} else {
const blob = new Blob([res])
const link = document.createElement('a')
const href = window.URL.createObjectURL(blob) // 下载链接
link.href = href
link.download = fileName + '.html' // 下载后文件名
document.body.appendChild(link)
link.click() // 点击下载
document.body.removeChild(link) // 下载完成移除元素
window.URL.revokeObjectURL(href) // 释放blob对象
}
this.$refs.topTool.closeDialog()
}, () => {
2022-05-24 15:04:46 +08:00
this.$message.error('123')
2022-04-11 15:49:10 +08:00
})
}
}
}