/**
* 表头通过标签管理配置的模块的表格数据的混入
*/
import { downFile, downloadFile, postAction } from '@api/manage'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import store from '@/store'
let importModalLoading
export const ConfigurableTableMixin = {
data () {
return {
/* 分页参数 */
ipagination: {
current: 1,
pageSize: 50,
pageSizeOptions: ['10', '50', '100', '150', '200'],
showTotal: (total, range) => {
return range[0] + '-' + range[1] + ' ' + this.$t('total') + ' ' + total + ' ' + this.$t('strip')
},
showQuickJumper: true,
showSizeChanger: true,
total: 0
},
columns: [],
selectedRowKeys: [],
dataSource: [],
isTrue: false, // 是否显示
searchParams: {},
loading: false,
orderBy: '1',
orderByField: '',
operateMaxNum: 1,
// 操作列的column
operateColumn: {
title: this.$t('operation'),
align: 'center',
fixed: 'right',
scopedSlots: { customRender: 'operation' }
},
// type: '', // 这个字段不知道什么含义
filters: {}, // 固定的搜索条件
needFilterTableBtn: true, // 是否需要过滤没有权限的按钮
tokenHeader: { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) },
getTableHeaderFunc: null // 获取表头的方法
}
},
computed: {
importExcelUrl () {
return `${window._CONFIG.domianURL}${this.url.importUrl}`
}
},
mounted () {
this.getTableHeader()
// this.loadData()
// 过滤没有权限的按钮
// if (this.needFilterTableBtn) {
// this.operationList = this.operationList.filter(item => isHasPermission(item.has))
// }
},
methods: {
/**
* 查询,通过search组件$emit事件获取查询参数
* @param queryParam
*/
searchQuery (queryParam) {
Object.keys(queryParam).forEach(res => {
if (queryParam[res] instanceof Array) {
queryParam[res] = queryParam[res].join(',')
}
})
this.searchParams = queryParam
this.getTableHeader()
this.loadData()
},
/**
* 重置查询
*/
searchReset () {
this.searchParams = {}
this.selectedRowKeys = []
this.getData()
this.getTableList()
},
/**
* 获取表头数据
*/
getTableHeader () {
if (!this.getTableHeaderFunc && typeof this.getTableHeaderFunc !== 'function') {
return
}
const params = { module: this.flag }
this.getTableHeaderFunc(params).then(res => {
if (res.success) {
this.columns = this.dealColumns(res.result || [])
this.isTrue = false
this.$nextTick(() => {
this.isTrue = true
})
}
})
},
/**
* 处理表头数据
* @param columns
* @returns {*[]}
*/
dealColumns (columns) {
const tempColumns = []
columns.forEach(item => {
// 可点击项加入插槽
if (item.click === 'true') {
item.scopedSlots = {
customRender: 'detailClick'
}
} else if (item.urlClick === 'true') {
item.scopedSlots = {
customRender: 'urlClick'
}
}
// 是否可排序
item.sorter = item.sortFlag === 1
item.width = 215
item.title = item.dbFieldTxt
item.dataIndex = item.dbFieldName
tempColumns.push(item)
})
if (this.showAction && this.operateColumn) {
let width = 0
if (this.operationList && this.operationList.length > 0) {
width += this.getTextWith(this.operationList[0].text) + this.getTextWith(this.$t('more'))
}
const operateColumn = JSON.parse(JSON.stringify(this.operateColumn))
operateColumn.width = width || 100
tempColumns.push(operateColumn)
}
return tempColumns
},
/**
* 通过文字获取列宽
* @param text
* @param fontStyle
* @returns {number}
*/
getTextWith (text, fontStyle) {
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
context.font = fontStyle || '14px' // 设置字体样式
const dimension = context.measureText(text)
return dimension.width + 40
},
getQueryParams () {
const pageNo = this.ipagination.current
const pageSize = this.ipagination.pageSize
return {
...this.filters,
...this.searchParams,
orderBy: this.orderBy,
orderByField: this.orderByField,
module: this.flag,
pageNo: pageNo,
pageSize: pageSize
}
},
loadData (arg) {
if (!this.url.list) {
this.$message.error('请设置url.list属性!')
return
}
// 加载数据 若传入参数1则加载第一页的内容
if (arg === 1) {
this.ipagination.current = 1
}
const params = this.getQueryParams()
this.loading = true
postAction(this.url.list, params).then((res) => {
if (res.success) {
if (res.result.current > 1 && res.result.records.length === 0) {
this.ipagination.current = res.result.current - 1
this.loadData()
return
}
this.dataSource = res.result.records || []
this.ipagination.total = res.result.total
} else {
this.$message.warn(res.message)
}
}).finally(() => {
this.loading = false
})
},
tableOnChange (pagination, filters, sorter) {
this.orderBy = sorter.order === 'ascend' ? '1' : '2'
this.orderByField = sorter.columnKey
this.ipagination = pagination
this.loadData(1)
},
onSelectChange (selectedRowKeys) {
this.selectedRowKeys = selectedRowKeys
},
/* 导入 */
handleImportExcel (info) {
// 加一个大的提示
if (!this.loading) {
importModalLoading = this.$info({
title: '提示',
content: 正在导入,请稍候