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/dataList.js

862 lines
25 KiB
JavaScript
Raw Normal View History

import bus from '@/libs/bus'
import { tableSet } from '@/components/common/js/tools'
import { fromRoute } from '@/components/common/js/constants'
import routerPathParams from '@/components/common/mixin/routerPathParams'
export default {
mixins: [routerPathParams],
props: {
switchTab: {
type: String,
default: ''
}
},
data () {
return {
fromRoute: fromRoute,
// 侧滑
rightBox: {
show: false
},
pageObj: { // 分页对象
pageNo: 1,
pageSize: this.$CONSTANTS.defaultPageSize,
pages: 1,
total: 0
},
orderBy: '',
/* 工具参数 */
tools: {
2021-05-19 11:32:41 +08:00
loading: true, // 是否显示table加载动画
customTableTitle: [], // 自定义列工具的数据
extraTableTitle: [] // 特殊页面asset额外的列
},
mainTableHeight: this.$tableHeight.normal, // 主列表table高度
batchDeleteObjs: [],
object: {},
tableData: [],
searchLabel: {}, // 搜索参数
scrollbarWrap: null,
delFlag: false,
fromBottom: false,
operationWidth: '165', // 操作列宽
searchCheckBox: {}
}
},
methods: {
sortableShow: tableSet.sortableShow,
propTitle: tableSet.propTitle,
asce: tableSet.asce,
desc: tableSet.desc,
strTodate: tableSet.strTodate,
2021-04-13 10:00:48 +08:00
tableOperation ([command, row]) {
switch (command) {
case 'edit': {
this.edit(row)
break
}
case 'delete': {
2021-04-13 10:00:48 +08:00
this.del(row)
break
}
case 'copy': {
this.copy(row)
break
}
default:
break
}
},
isBuildIn (row) {
return (row.buildIn && row.buildIn == 1) || (row.builtIn && row.builtIn == 1)
},
2021-04-13 10:00:48 +08:00
selectionChange (objs) {
this.batchDeleteObjs = objs
},
getTableData (params) {
if (params && Object.keys(params).length > 0) {
for (const key in params) {
this.$set(this.searchLabel, key, params[key])
}
}
if (this.orderBy) {
this.$set(this.searchLabel, 'orderBy', this.orderBy)
} else {
delete this.searchLabel.orderBy
}
this.$set(this.searchLabel, 'pageNo', this.pageObj.pageNo)
this.$set(this.searchLabel, 'pageSize', this.pageObj.pageSize)
this.tools.loading = true
const dataListParam = {
...this.searchLabel,
...this.searchCheckBox
}
if (this.switchTab) {
dataListParam.switchTab = this.switchTab
}
const path = this.$route.fullPath.match(/\/(\S*)\?/)[1]
this.updatePath(dataListParam, path)
this.$get(this.url, { ...this.searchLabel, ...this.searchCheckBox }).then(response => {
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.detailViewRightObj = this.tableData[0]
this.pageObj.total = response.data.total
this.pageObj.pages = response.data.pages
if (!this.scrollbarWrap && this.$refs.dataTable.$refs.dataTable) {
this.$nextTick(() => {
this.scrollbarWrap = this.$refs.dataTable.$refs.dataTable.bodyWrapper
this.toTopBtnHandler(this.scrollbarWrap)
})
}
if (this.detailType === 'view') {
this.$refs.dataDetail && (this.$refs.dataDetail.$refs.dataTable.scrollTop = 0)
}
}
})
},
del (row) {
const self = this
this.$confirm(this.$t('tip.confirmDelete'), {
confirmButtonText: this.$t('tip.yes'),
cancelButtonText: this.$t('tip.no'),
type: 'warning'
}).then(() => {
2021-04-13 10:00:48 +08:00
this.$delete(this.url + '?ids=' + row.id).then(response => {
if (response.code === 200) {
self.delFlag = true
this.$message({ duration: 2000, type: 'success', message: this.$t('tip.deleteSuccess') })
self.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('nz-pageSize-' + localStorage.getItem('nz-username') + '-' + this.tableId, val)
this.getTableData()
},
add () {
this.object = this.newObject()
this.rightBox.show = true
},
closeRightBox (refresh) {
this.rightBox.show = false
this.copyFlag = false
if (refresh) {
this.delFlag = true
this.getTableData()
}
},
closeSilenceBox (refresh) {
this.silenceBoxShow = false
if (refresh) {
this.delFlag = true
this.getTableData()
}
},
edit (u) {
2021-04-26 21:42:15 +08:00
this.$get(`${this.url}/${u.id}`).then(response => {
if (response.code === 200) {
this.object = response.data
this.rightBox.show = true
}
})
},
copy (u) {
this.object = { ...u, name: u.name + '-copy', id: '' }
if (this.object.name.length > 64) {
const length = this.object.name.length - 64
this.object.name = u.name.substring(0, u.name.length - length) + '-copy'
}
this.rightBox.show = true
},
esc () {
this.rightBox.show = false
},
dragend () {
this.$nextTick(() => {
this.$refs.dataTable.$refs.dataTable && this.$refs.dataTable.$refs.dataTable.doLayout()
})
},
search (searchObj) {
this.searchLabel = {}
this.pageObj.pageNo = 1
for (const item in searchObj) {
if (typeof searchObj[item] !== 'undefined' && searchObj[item] !== null && searchObj[item] !== '') {
this.$set(this.searchLabel, item, searchObj[item])
}
}
this.getTableData()
},
tableDataSort (orderBy) {
this.orderBy = orderBy
this.getTableData()
},
tableTitleReset (src, dist) {
dist.forEach(item => {
const title = src.find(t => t.prop === item.prop)
if (title && title.label) {
item.label = title.label
}
})
},
toTop (wrap) {
let currentTop = wrap.scrollTop
const interval = currentTop / 10
const intervalFunc = setInterval(function () { // 花200ms分10次回到顶部模拟动画效果
if (currentTop === 0) {
clearInterval(intervalFunc)
} else {
currentTop = (currentTop - interval) < interval * 0.5 ? 0 : currentTop - interval
wrap.scrollTop = currentTop
}
}, 20)
},
2021-04-22 16:35:15 +08:00
clickTab (showTabInfo, showTab) {
this.$emit('changeShowTab', showTab)
},
toTopBtnHandler (wrap) {
const vm = this
wrap.addEventListener('scroll', bus.debounce(function () {
vm.tools.showTopBtn = wrap.scrollTop > 50
vm.tools.tableHover = wrap.scrollTop > 50
}, 100))
},
showClickSearch () {
const index = this.dataListLayout.indexOf('clickSearch')
if (index > -1) {
this.dataListLayout.splice(index, 1)
} else {
this.dataListLayout.push('clickSearch')
}
2021-10-26 15:55:57 +08:00
localStorage.setItem('dataList-layout' + this.tableId, JSON.stringify(this.dataListLayout))
},
addSilence (row, type) {
2021-12-31 16:41:30 +08:00
this.blankSilenceObject.startAt = bus.timeFormate(bus.getOffsetTimezoneData(), 'YYYY-MM-DD HH:mm:ss')
this.blankSilenceObject.endAt = bus.timeFormate(bus.getOffsetTimezoneData(1), 'YYYY-MM-DD HH:mm:ss')
this.objectSilence = JSON.parse(JSON.stringify(this.blankSilenceObject))
2021-06-01 15:19:47 +08:00
this.objectSilence.name = 'Quick silence'
if (type !== 'alertMessage' && type !== 'alertRule') {
this.objectSilence.matchers = [
{ name: type, value: row.name, regex: 0 },
{ name: type + '_id', value: row.id, regex: 0 }
]
} else if (type === 'alertMessage') {
if (typeof row.labels === 'string') row.labels = JSON.parse(row.labels)
const labels = JSON.parse(JSON.stringify(row.labels))
this.objectSilence.matchers = []
const filterArr = ['alertname', 'severity_id', 'severity', 'rule_type']
Object.keys(labels).forEach((key, i) => {
if (filterArr.indexOf(key) != -1) {
return
}
this.objectSilence.matchers.push(
{ name: key, value: labels[key], regex: 0 }
)
})
} else if (type === 'alertRule') {
this.objectSilence.matchers = [
{ name: 'alertName', value: row.name, regex: 0 }
]
}
this.silenceBoxShow = true
},
closeDialog () {
this.silenceBoxShow = false
},
showText (row) {
this.dialogShowText = true
this.dialogText = row.alertRule.trbShot
}
},
watch: {
tableData: {
deep: true,
handler (n) {
if (n.length === 0 && this.pageObj.pageNo > 1) {
this.pageNo(this.pageObj.pageNo - 1)
}
/* if (!this.delFlag) { // 不是删除时回到顶部
this.$refs.dataTable.bodyWrapper.scrollTop = 0
} else {
this.delFlag = false
} */
}
},
'tools.customTableTitle': {
deep: true,
handler (n) {
this.dragend()
}
}
},
created () {
const path = this.$route.fullPath.match(/\/(\S*)\?/)[1]
let searchKeys = {}
if (path === 'dc') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
id: 'ids',
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
},
location: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'location',
type: 'string',
defaultJson: {
disabled: false,
label: 'location',
name: 'Location',
type: 'input',
val: ''
},
jsonKey: 'val'
},
state: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'state',
type: 'string',
defaultJson: {
disabled: false,
label: 'dcState',
name: 'State',
readonly: true,
type: 'selectString',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'i18n') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
id: 'ids',
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
code: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'code',
type: 'string',
defaultJson: {
disabled: false,
label: 'code',
name: 'Code',
type: 'input',
val: ''
},
jsonKey: 'val'
},
value: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'value',
type: 'string',
defaultJson: {
disabled: false,
label: 'value',
name: 'Value',
type: 'input',
val: ''
},
jsonKey: 'val'
},
lang: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'lang',
type: 'string',
defaultJson: {
disabled: false,
label: 'lang',
name: 'Lang',
type: 'input',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'model') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
id: 'ids',
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
},
brandIds: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'brandIds',
type: 'number',
defaultJson: {
disabled: false,
label: 'brandIds',
name: 'Brand',
readonly: true,
type: 'brand',
val: '',
valnum: '',
listStr: 'brandSelect'
},
jsonKey: 'valnum'
}
}
} else if (path === 'operationLog') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
type: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'type',
type: 'number',
defaultJson: {
disabled: false,
id: 11,
label: 'type',
name: 'Type',
type: 'input',
val: ''
},
jsonKey: 'val'
},
username: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'username',
type: 'string',
defaultJson: {
disabled: false,
id: 12,
label: 'username',
name: 'User',
type: 'input',
val: ''
},
jsonKey: 'val'
},
operation: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'operation',
type: 'string',
defaultJson: {
disabled: false,
id: 13,
label: 'operation',
name: 'Operation',
type: 'selectString',
val: ''
},
jsonKey: 'val'
},
state: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'state',
type: 'string',
defaultJson: {
disabled: false,
id: 16,
label: 'state',
name: 'State',
readonly: true,
type: 'selectString',
val: ''
},
jsonKey: 'val'
},
operaId: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'operaId',
type: 'string',
defaultJson: {
disabled: false,
id: 14,
label: 'operaId',
name: 'Resources',
type: 'input',
val: ''
},
jsonKey: 'val'
},
params: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'params',
type: 'string',
defaultJson: {
disabled: false,
id: 17,
label: 'params',
name: 'Params',
type: 'input',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'agent') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
id: 'ids',
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
state: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'state',
type: 'string',
defaultJson: {
disabled: false,
label: 'promState',
name: 'State',
readonly: true,
type: 'select',
val: ''
},
jsonKey: 'val'
},
dc: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'dc',
type: 'number',
defaultJson: {
name: 'Date center',
type: 'dc',
label: 'dcIds',
disabled: false,
val: '',
valnum: '',
listStr: 'dcSelect'
},
jsonKey: 'valnum'
},
type: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'type',
type: 'number',
defaultJson: {
disabled: false,
label: 'promType',
name: 'Type',
readonly: true,
type: 'select',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'roles') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'template' && this.switchTab) {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
},
gname: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'gname',
type: 'string',
defaultJson: {
disabled: false,
label: 'gname',
name: 'Group',
type: 'input',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'credentials') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
},
type: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'type',
type: 'string',
defaultJson: {
disabled: false,
label: 'credentialType',
name: 'Type',
readonly: true,
type: 'select',
val: ''
},
jsonKey: 'val'
}
}
} else if (path === 'mib') {
searchKeys = {
// key: path 键
// value: vue set 参数
pageNo: { target: this.pageObj, propertyName: 'pageNo', type: 'number' },
pageSize: { target: this.pageObj, propertyName: 'pageSize', type: 'number' },
orderBy: { target: this.$data, propertyName: 'orderBy', type: 'string' },
ids: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'ids',
type: 'string',
defaultJson: {
disabled: false,
label: 'ids',
name: 'ID',
type: 'input',
val: ''
},
jsonKey: 'val'
},
name: {
target: this.searchLabel,
isSearchInput: true,
propertyName: 'name',
type: 'string',
defaultJson: {
disabled: false,
id: 'name',
label: 'name',
name: 'Name',
type: 'input',
val: ''
},
jsonKey: 'val'
}
}
}
this.initQueryFromPath(searchKeys)
},
mounted () {
const pageSize = localStorage.getItem('nz-pageSize-' + localStorage.getItem('nz-username') + '-' + this.tableId)
if (pageSize && pageSize !== 'undefined') {
this.pageObj.pageSize = pageSize
}
2021-05-12 09:47:28 +08:00
let localStorageTableTitle = localStorage.getItem('nz-tableTitle-' + localStorage.getItem('nz-username') + '-' + this.tableId)
localStorageTableTitle = localStorageTableTitle ? JSON.parse(localStorageTableTitle) : this.$refs.dataTable.tableTitle
2021-05-13 11:40:22 +08:00
this.tools.customTableTitle = this.$refs.dataTable.tableTitle.map((item, index) => { // 修复切换中英文的问题
item.show = localStorageTableTitle[index].show
2021-05-12 09:47:28 +08:00
return item
})
2021-05-13 11:40:22 +08:00
if (localStorageTableTitle && (localStorageTableTitle.length > this.$refs.dataTable.tableTitle.length)) {
const arr = localStorageTableTitle.splice(this.$refs.dataTable.tableTitle.length, localStorageTableTitle.length)
arr.forEach(item => {
item.minWidth = item.label.length * 16 + 20
})
2021-05-13 11:40:22 +08:00
this.tools.customTableTitle = this.tools.customTableTitle.concat(arr)
}
if (!this.fromBottom) {
this.getTableData()
this.$nextTick(() => {
this.$refs.dataTable.$refs.dataTable && this.$refs.dataTable.$refs.dataTable.doLayout()
})
}
},
beforeDestroy () {
if (this.scrollbarWrap) {
this.scrollbarWrap.removeEventListener('scroll', bus.debounce)
}
}
}