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
cyber-narrator-cn-ui/src/mixins/data-list.js

316 lines
9.7 KiB
JavaScript
Raw Normal View History

2021-06-11 10:00:22 +08:00
import { tableSort } from '@/utils/tools'
import { defaultPageSize, fromRoute, position, storageKey } from '@/utils/constants'
2021-06-11 10:00:22 +08:00
import { get, del } from '@/utils/http'
import { ref } from 'vue'
import pagination from '@/components/common/Pagination'
2022-04-13 16:40:55 +08:00
import axios from 'axios'
import { api } from '@/utils/api'
2021-06-11 10:00:22 +08:00
export default {
components: {
pagination
},
2021-06-11 10:00:22 +08:00
data () {
return {
fromRoute: fromRoute,
// 侧滑
rightBox: {
show: false
},
pageObj: { // 分页对象
pageNo: 1,
pageSize: defaultPageSize,
total: ''
2021-06-11 10:00:22 +08:00
},
/* 工具参数 */
tools: {
loading: true, // 是否显示table加载动画
customTableTitle: [] // 自定义列工具的数据
},
mainTableHeight: position.tableHeight.normal, // 主列表table高度
batchDeleteObjs: [],
object: {},
searchLabel: ref({}),
tableData: [],
scrollbarWrap: null,
delFlag: false,
operationWidth: '165' // 操作列宽
}
},
methods: {
sortableShow: tableSort.sortableShow,
propTitle: tableSort.propTitle,
asce: tableSort.asce,
desc: tableSort.desc,
strToDate: tableSort.strToDate,
2022-04-13 18:31:13 +08:00
tableOperation ([command, row, param]) {
2021-06-11 10:00:22 +08:00
switch (command) {
case 'edit': {
this.edit(row)
break
}
case 'delete': {
this.del(row)
break
}
case 'copy': {
this.copy(row)
break
}
case 'download': {
2022-04-13 18:31:13 +08:00
this.download(row, param)
break
}
case 'preview': {
this.preview(row)
break
}
2021-06-11 10:00:22 +08:00
default:
break
}
},
selectionChange (objs) {
this.batchDeleteObjs = objs
},
getTableData (params) {
if (params) {
this.searchLabel = { ...this.searchLabel, ...params }
2022-04-22 11:18:14 +08:00
} else {
this.searchLabel = {}
2021-06-11 10:00:22 +08:00
}
2022-04-22 11:18:14 +08:00
this.searchLabel = { ...this.searchLabel, ...this.pageObj }
2021-06-11 10:00:22 +08:00
this.tools.loading = true
2021-09-02 17:12:27 +08:00
delete this.searchLabel.total
let listUrl = this.url
if (this.listUrl) {
listUrl = this.listUrl
}
get(listUrl, this.searchLabel).then(response => {
2021-06-11 10:00:22 +08:00
this.tools.loading = false
if (response.code === 200) {
for (let i = 0; i < response.data.list.length; i++) {
response.data.list[i].status = response.data.list[i].status + ''
}
this.tableData = response.data.list
this.pageObj.total = response.data.total
// TODO 回到顶部
}
})
},
del (row) {
this.$confirm(this.$t('tip.confirmDelete'), {
confirmButtonText: this.$t('tip.yes'),
cancelButtonText: this.$t('tip.no'),
type: 'warning'
}).then(() => {
del(this.url + '?ids=' + row.id).then(response => {
if (response.code === 200) {
this.delFlag = true
this.$message({ duration: 2000, type: 'success', message: this.$t('tip.deleteSuccess') })
this.getTableData()
} else {
this.$message.error(response.msg)
}
})
})
},
newObject () {
return JSON.parse(JSON.stringify(this.blankObject))
},
pageNo (val) {
this.pageObj.pageNo = val
this.getTableData()
},
pageSize (val) {
this.pageObj.pageSize = val
localStorage.setItem(storageKey.pageSize + '-' + localStorage.getItem(storageKey.username) + '-' + this.tableId, val)
2021-06-11 10:00:22 +08:00
this.getTableData()
},
add () {
this.object = this.newObject()
this.rightBox.show = true
},
closeRightBox (refresh) {
this.rightBox.show = false
if (refresh) {
this.delFlag = true
this.getTableData()
}
},
edit (u) {
get(`${this.url}/${u.id}`).then(response => {
if (response.code === 200) {
this.object = response.data
2021-06-11 10:00:22 +08:00
this.rightBox.show = true
}
})
},
copy (u) {
this.object = { ...u, name: 'Copy from ' + u.name, id: '' }
this.rightBox.show = true
},
2022-04-13 18:31:13 +08:00
download (u, n) {
if (this.$refs.dataTable.loading && n === 2) { // 批量下载
return
} else if (this.$refs.dataTable.loadingTableId === u.id && n === 1) { // 列表单个下载
return
}
2022-04-13 18:31:13 +08:00
let fileName = ''
let url = ''
let params = {}
if (n === 2) { // 批量下载
2022-04-13 18:31:13 +08:00
fileName = 'builtinReport' + '-' + this.getTimeString() + '.zip' // 文件名称
url = api.reportBatchDownloadPdf // 批量 zip 下载
2022-04-13 18:31:13 +08:00
params = {
ids: u
}
} else if (n === 1) {
2022-04-14 11:59:37 +08:00
fileName = u.name + '.pdf' // 文件名称
url = api.reportDownloadPdf // 单个 pdf 下载
2022-04-13 18:31:13 +08:00
params = {
id: u.id
}
2022-04-13 16:40:55 +08:00
}
if (n === 2) { // 批量下载
this.$refs.dataTable.loading = true
} else if (n === 1) { // 列表单个下载
this.$refs.dataTable.loadingTableId = u.id // 列表单个下载
}
if (!u && n === 2) { // 批量下载
return this.$refs.dataTable.loading = false
} else if (!u && n === 1) { // 列表单个下载
return this.$refs.dataTable.loadingTableId = u.id
}
2022-04-13 18:31:13 +08:00
axios.get(url, { responseType: 'blob', params: params }).then(res => {
2022-04-13 16:40:55 +08:00
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie11
const blobObject = new Blob([res.data])
window.navigator.msSaveOrOpenBlob(blobObject, fileName)
} else {
2022-04-13 16:40:55 +08:00
const url = URL.createObjectURL(new Blob([res.data]))
const a = document.createElement('a')
document.body.appendChild(a) // 此处增加了将创建的添加到body当中
a.href = url
a.download = fileName
a.target = '_blank'
a.click()
a.remove() // 将a标签移除
}
if (n === 2) { // 批量下载
this.$refs.dataTable.loading = false
} else if (n === 1) { // 列表单个下载
this.$refs.dataTable.loadingTableId = !u.id
}
2022-04-13 16:40:55 +08:00
}, error => {
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)
}
}
2022-04-13 16:40:55 +08:00
reader.readAsText(error.response.data)
if (n === 2) { // 批量下载
this.$refs.dataTable.loading = false
} else if (n === 1) { // 列表单个下载
this.$refs.dataTable.loadingTableId = !u.id
}
}).catch(() => {
if (n === 2) { // 批量下载
this.$refs.dataTable.loading = false
} else if (n === 1) { // 列表单个下载
this.$refs.dataTable.loadingTableId = !u.id
}
})
},
preview (u) {
if (this.$refs.dataTable.loadingPreviewId === u.id) { // 列表单个下载
return
}
2022-04-13 16:40:55 +08:00
const params = {
id: u.id
}
this.$refs.dataTable.loadingPreviewId = u.id
axios.get(api.reportView, { params: params }).then(res => {
2022-04-13 16:40:55 +08:00
const prevWindow = window.open('', '')
prevWindow.document.write(res.data)
prevWindow.focus()
this.$refs.dataTable.loadingPreviewId = !u.id
}).catch(() => {
this.$refs.dataTable.loadingPreviewId = !u.id
})
},
2021-06-11 10:00:22 +08:00
esc () {
this.rightBox.show = false
},
dragend () {
this.$nextTick(() => {
this.$refs.dataTable.$refs.dataTable.doLayout()
})
},
tableDataSort (orderBy) {
2022-01-26 19:14:40 +08:00
this.searchLabel.orderBy = orderBy
2021-06-11 10:00:22 +08:00
this.getTableData()
},
search (params) {
this.pageObj.pageNo = 1
this.getTableData(params)
2022-04-13 16:40:55 +08:00
},
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
2021-06-11 10:00:22 +08:00
}
},
watch: {
tableData: {
deep: true,
handler (n) {
if (n.length === 0 && this.pageObj.pageNo > 1) {
this.pageNo(this.pageObj.pageNo - 1)
}
// TODO 不是删除时回到顶部
}
},
'tools.customTableTitle': {
deep: true,
handler (n) {
this.dragend()
}
}
},
mounted () {
const pageSize = localStorage.getItem(storageKey.pageSize + '-' + localStorage.getItem(storageKey.username) + '-' + this.tableId)
2021-06-11 10:00:22 +08:00
if (pageSize && pageSize !== 'undefined') {
this.pageObj.pageSize = pageSize
}
let localStorageTableTitle = localStorage.getItem(storageKey.tableTitle + '-' + localStorage.getItem(storageKey.username) + '-' + this.tableId)
2021-06-11 10:00:22 +08:00
localStorageTableTitle = localStorageTableTitle ? JSON.parse(localStorageTableTitle) : this.$refs.dataTable.tableTitle
this.tools.customTableTitle = this.$refs.dataTable.tableTitle.map((item, index) => { // 修复切换中英文的问题
2021-11-11 23:28:52 +08:00
if (localStorageTableTitle[index]) {
2021-10-24 20:23:24 +08:00
item.show = localStorageTableTitle[index].show
}
2021-06-11 10:00:22 +08:00
return item
})
if (localStorageTableTitle && (localStorageTableTitle.length > this.$refs.dataTable.tableTitle.length)) {
const arr = localStorageTableTitle.splice(this.$refs.dataTable.tableTitle.length, localStorageTableTitle.length)
this.tools.customTableTitle = this.tools.customTableTitle.concat(arr)
}
this.getTableData()
}
}