zhaoxiaoqiang1
2026-01-04 f1d30d03186c79ca2cbcfe60d6d2ce7d73fba97b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<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>