<template>
|
<el-button class="el-button--small" size="small" type="primary" icon="el-icon-download" @click="download">{{text}}</el-button>
|
</template>
|
<script>
|
import { createExportApply } from '@comprehensive/serve/public'
|
import { alert } from '@comprehensive/utils/comm'
|
export default {
|
props: {
|
// 按钮文本
|
text: {
|
type: String,
|
default: '导出Excel'
|
},
|
|
// 导出类型: 0 formData,1 JsonToExcel
|
type: {
|
type: [String, Number],
|
default: 0
|
},
|
|
/**
|
* values type为0时,需提交的数据
|
* @example
|
* {serialNo: '', customerName: ''}
|
*/
|
values: {
|
type: Object,
|
default: () => {}
|
},
|
|
/**
|
* headers 表头字段列表 type为1时有效
|
* @example
|
* ['凭证类型', '凭证金额', '凭证日期'] or [{prop: 'periodno', width: '60px', label: '期次'}]
|
*/
|
headers: {
|
type: Array,
|
default: () => []
|
},
|
|
/**
|
* list 数据列表 type为1时有效
|
* @example
|
* [{documentType: '', certificateAmount: '', voucherDate: ''}]
|
* or
|
* [['', '', ''], ['', '', '']]
|
*/
|
list: {
|
type: Array,
|
default: () => []
|
},
|
|
// 文件名 type为1时有效
|
filename: {
|
type: String,
|
default: '导出Excel'
|
}
|
},
|
methods: {
|
async download() {
|
const { type, values } = this
|
if (typeof this.$listeners.handleClick === 'function') {
|
this.$emit('handleClick', values)
|
return false
|
}
|
|
// 贷款查询Excel导出
|
if (type === 0) {
|
const res = await createExportApply(values)
|
if(res.code == '00'){
|
alert('提交成功',res.msg,() => {})
|
}
|
}
|
if (type === 1) {
|
this.jsonToExcel()
|
}
|
},
|
|
// jsonToExcel 转换json数据为Excel,并下载Excel文件
|
jsonToExcel() {
|
let { headers, list, filename } = this
|
let str = ''
|
|
// example1
|
// headers = ['期次', '还款计划类型']
|
// list = [
|
// ['1', '本息偿还计划'],
|
// ['12', '本息偿还计划']
|
// ]
|
|
if (headers.length > 0 && typeof headers[0] !== 'object') {
|
str = list.reduce((pre, curr) => {
|
let tempStr = curr.join(',')
|
pre = `${pre}\n${tempStr}`
|
return pre
|
}, `${headers.join(',')}`)
|
}
|
|
// example2
|
// headers = [
|
// { label: '期次', prop: 'periodno' },
|
// { label: '还款计划类型', prop: 'pstype' }
|
// ]
|
// list = [
|
// { periodno: 0, pstype: '本息偿还计划' },
|
// { periodno: 12, pstype: '本息偿还计划' }
|
// ]
|
if (headers.length > 0 && typeof headers[0] === 'object') {
|
let hadersArr = headers.reduce((pre, curr) => {
|
pre.push(curr.label)
|
return pre
|
}, [])
|
|
str = list.reduce((pre, curr) => {
|
let tempStr = headers.reduce((headersPre, { prop }) => {
|
headersPre = `${headersPre}${curr[prop]},`
|
return headersPre
|
}, '')
|
pre = `${pre}${tempStr}\n`
|
return pre
|
}, `${hadersArr.join(',')}\n`)
|
}
|
|
const uri = `data:text/csv;charset=utf-8,\ufeff${encodeURIComponent(str)}`
|
const link = document.createElement('a')
|
link.href = uri
|
link.download = `${filename}.csv`
|
document.body.appendChild(link)
|
link.click()
|
document.body.removeChild(link)
|
}
|
}
|
}
|
</script>
|
<style lang="stylus" scoped>
|
.el-button--small
|
height 34px
|
font-size 14px
|
padding 9px 12px
|
</style>
|