feat: 内置报告下载预览功能

This commit is contained in:
@changcode
2022-04-13 16:40:55 +08:00
parent 664111122a
commit c880913390
4 changed files with 108 additions and 61 deletions

View File

@@ -59,10 +59,10 @@
</el-table-column> </el-table-column>
</el-table> </el-table>
<div class="table-operation-all"> <div class="table-operation-all">
<el-checkbox v-model="checkbox" @change="builtinReportCheckbox"></el-checkbox> <el-checkbox v-model="checkboxAll" @change="builtinReportCheckbox"></el-checkbox>
<div class="table-operation-all-span"> <div class="table-operation-all-span">
<span>{{ $t('overall.all') }}</span> <span>{{ $t('overall.all') }}</span>
<span :disabled="downloadAgentFlag" :class="{'table-operation-all-checkbox': checkbox}" @click="batchDownload">{{$t('report.batchDow')}}</span> <span :class="{'table-operation-all-checkbox': checkboxAll}" @click="batchDownload">{{$t('report.batchDow')}}</span>
</div> </div>
</div> </div>
</template> </template>
@@ -70,6 +70,7 @@
<script> <script>
import table from '@/mixins/table' import table from '@/mixins/table'
import { get } from '@/utils/http' import { get } from '@/utils/http'
import axios from 'axios'
export default { export default {
name: 'builtinReportTable', name: 'builtinReportTable',
mixins: [table], mixins: [table],
@@ -99,8 +100,7 @@ export default {
show: true show: true
} }
], ],
checkbox: false, checkboxAll: false,
downloadAgentFlag: false,
checkboxIds: '', checkboxIds: '',
builtinId: '' builtinId: ''
} }
@@ -109,9 +109,9 @@ export default {
select (objs) { select (objs) {
this.checkboxIds = objs.map(item => { return item.id }).join(',') this.checkboxIds = objs.map(item => { return item.id }).join(',')
if (objs.length > 0) { if (objs.length > 0) {
this.checkbox = true this.checkboxAll = true
} else { } else {
this.checkbox = false this.checkboxAll = false
} }
}, },
builtinReportCheckbox (objs) { builtinReportCheckbox (objs) {
@@ -123,32 +123,53 @@ export default {
console.log(objs) console.log(objs)
}, },
batchDownload: function () { batchDownload: function () {
this.downloadAgentFlag = true const fileName = 'builtinReport' + '-' + this.getTimeString() + '.zip'
get('/report/job/batchDownloadPdf', { ids: this.checkboxIds }).then(data => { const params = {
this.downloadAgentFlag = false ids: this.checkboxIds
let fileName = 'confagent' }
const disposition = data.headers['content-disposition'] axios.get('/report/job/batchDownloadPdf', { responseType: 'blob', params: params }).then(res => {
if (disposition) { if (window.navigator.msSaveOrOpenBlob) {
fileName = disposition.split(';')[1].split('filename=')[1] // 兼容ie11
} const blobObject = new Blob([res.data])
// 由于ie不支持download属性故需要做兼容判断 window.navigator.msSaveOrOpenBlob(blobObject, fileName)
if (navigator.appVersion.toString().indexOf('.NET') > 0) {
// ie独有的msSaveBlob属性data.data为Blob文件流
window.navigator.msSaveBlob(data.data, fileName)
} else { } else {
// 以下流程即为文章开始的下载流程 const url = URL.createObjectURL(new Blob([res.data]))
const url = window.URL.createObjectURL(data.data) const a = document.createElement('a')
const link = document.createElement('a') document.body.appendChild(a) // 此处增加了将创建的添加到body当中
link.style.display = 'none' a.href = url
link.href = url a.download = fileName
link.download = fileName a.target = '_blank'
document.body.appendChild(link) a.click()
link.click() a.remove() // 将a标签移除
window.URL.revokeObjectURL(link.href)
} }
}).catch(() => { }, error => {
this.downloadAgentFlag = false const $self = this
const reader = new FileReader()
reader.onload = function (event) {
const responseText = reader.result
const exception = JSON.parse(responseText)
if (exception.message) {
$self.$message.error(exception.message)
} else {
console.error(error)
}
}
reader.readAsText(error.response.data)
}) })
},
getTimeString () {
const split = '-'
const date = new Date()
const year = date.getFullYear()
const month = this.formatNum(date.getMonth() + 1)
const day = this.formatNum(date.getDate())
const hours = this.formatNum(date.getHours())
const minutes = this.formatNum(date.getMinutes())
const seconds = this.formatNum(date.getSeconds())
return year + split + month + split + day + ' ' + hours + split + minutes + split + seconds
},
formatNum (num) {
return num > 9 ? num : '0' + num
} }
} }
} }

View File

@@ -3,6 +3,7 @@ import { defaultPageSize, fromRoute, position } from '@/utils/constants'
import { get, del } from '@/utils/http' import { get, del } from '@/utils/http'
import { ref } from 'vue' import { ref } from 'vue'
import pagination from '@/components/common/Pagination' import pagination from '@/components/common/Pagination'
import axios from 'axios'
export default { export default {
components: { components: {
pagination pagination
@@ -74,7 +75,7 @@ export default {
if (params) { if (params) {
this.searchLabel = { ...this.searchLabel, ...params } this.searchLabel = { ...this.searchLabel, ...params }
} }
this.searchLabel = { ...this.searchLabel, ...this.pageObj } this.searchLabel = { ...this.searchLabel, ...this.pageObj}
this.tools.loading = true this.tools.loading = true
delete this.searchLabel.total delete this.searchLabel.total
let listUrl = this.url let listUrl = this.url
@@ -146,37 +147,48 @@ export default {
this.rightBox.show = true this.rightBox.show = true
}, },
download (u) { download (u) {
get('/report/job/downloadPdf', { id: u.id }).then(data => { const fileName = 'builtinReport' + '-' + this.getTimeString() + '.pdf'
this.downloadAgentFlag = false const params = {
let fileName = 'confBuiltin' id: u.id
const disposition = data.headers['content-disposition'] }
if (disposition) { axios.get('/report/job/downloadPdf', { responseType: 'blob', params: params }).then(res => {
fileName = disposition.split(';')[1].split('filename=')[1] if (window.navigator.msSaveOrOpenBlob) {
} // 兼容ie11
// 由于ie不支持download属性故需要做兼容判断 const blobObject = new Blob([res.data])
if (navigator.appVersion.toString().indexOf('.NET') > 0) { window.navigator.msSaveOrOpenBlob(blobObject, fileName)
// ie独有的msSaveBlob属性data.data为Blob文件流
window.navigator.msSaveBlob(data.data, fileName)
} else { } else {
// 以下流程即为文章开始的下载流程 const url = URL.createObjectURL(new Blob([res.data]))
const url = window.URL.createObjectURL(data.data) const a = document.createElement('a')
const link = document.createElement('a') document.body.appendChild(a) // 此处增加了将创建的添加到body当中
link.style.display = 'none' a.href = url
link.href = url a.download = fileName
link.download = fileName a.target = '_blank'
document.body.appendChild(link) a.click()
link.click() a.remove() // 将a标签移除
window.URL.revokeObjectURL(link.href)
} }
}).catch(() => { }, error => {
this.downloadAgentFlag = false const $self = this
const reader = new FileReader()
reader.onload = function (event) {
const responseText = reader.result
const exception = JSON.parse(responseText)
if (exception.message) {
$self.$message.error(exception.message)
} else {
console.error(error)
}
}
reader.readAsText(error.response.data)
}) })
}, },
preview (u) { preview (u) {
get('/report/job/view', { id: u.id }).then(res => { const params = {
if (res.code === 200) { id: u.id
console.log(res) }
} axios.get('/report/job/view', { params: params }).then(res => {
const prevWindow = window.open('', '')
prevWindow.document.write(res.data)
prevWindow.focus()
}) })
}, },
esc () { esc () {
@@ -194,6 +206,20 @@ export default {
search (params) { search (params) {
this.pageObj.pageNo = 1 this.pageObj.pageNo = 1
this.getTableData(params) this.getTableData(params)
},
getTimeString () {
const split = '-'
const date = new Date()
const year = date.getFullYear()
const month = this.formatNum(date.getMonth() + 1)
const day = this.formatNum(date.getDate())
const hours = this.formatNum(date.getHours())
const minutes = this.formatNum(date.getMinutes())
const seconds = this.formatNum(date.getSeconds())
return year + split + month + split + day + ' ' + hours + split + minutes + split + seconds
},
formatNum (num) {
return num > 9 ? num : '0' + num
} }
}, },
watch: { watch: {

View File

@@ -4,7 +4,7 @@
<div class="cn-detection__row"> <div class="cn-detection__row">
<div v-if="security" class="cn-detection__header"> <div v-if="security" class="cn-detection__header">
<span :class="iconClass"><i :class="iconClass" class="cn-icon cn-icon-attacker"></i>{{detection.offenderIp || '-'}}</span> <span :class="iconClass"><i :class="iconClass" class="cn-icon cn-icon-attacker"></i>{{detection.offenderIp || '-'}}</span>
<div :class="iconClass" class="domain cn-detection-domain">{{detection.domain}}</div> <div :class="iconClass" class="domain cn-detection-domain" v-if="detection.domain">{{detection.domain}}</div>
<span class="line">-------</span> <span class="line">-------</span>
<span class="circle"></span> <span class="circle"></span>
<i class="cn-icon cn-icon-attacked" ></i>{{detection.victimIp || '-'}} <i class="cn-icon cn-icon-attacked" ></i>{{detection.victimIp || '-'}}

View File

@@ -4,10 +4,10 @@
<div class="cn-builtin-left-title"> <div class="cn-builtin-left-title">
{{$t('report.category')}} {{$t('report.category')}}
</div> </div>
<div class="cn-builtin-left-menu" :class="{'cn-active': builtinId === ''}" @click="builtinTabs('')"> <div class="cn-builtin-left-menu" :class="{'cn-active': builtinId === ''}" @click="builtinTabs('', '')">
{{$t('dns.all')}} {{$t('dns.all')}}
</div> </div>
<div class="cn-builtin-left-menu" :class="{'cn-active': builtinId === item.id}" v-for="item in builtinReportLeftMenu" :key="item.id" @click="builtinTabs(item.id)"> <div class="cn-builtin-left-menu" :class="{'cn-active': builtinId === item.id}" v-for="item in builtinReportLeftMenu" :key="item.id" @click="builtinTabs(item, item.id)">
{{item.name}} {{item.name}}
</div> </div>
</div> </div>
@@ -85,9 +85,9 @@ export default {
} }
}) })
}, },
builtinTabs (id) { builtinTabs (data, id) {
this.builtinId = id this.builtinId = id
this.getTableData({ id: this.builtinId }) this.getTableData({ name: data.name, tempId: data.id })
} }
}, },
mounted () { mounted () {