Blame view

ant-design-vue-jeecg/src/components/JVxeCells/JVxeFileCell.vue 6.63 KB
肖超群 authored
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
<template>
  <div>
    <template v-if="hasFile" v-for="(file, fileKey) of [innerFile || {}]">
      <div :key="fileKey" style="position: relative;">
        <a-tooltip v-if="file.status==='uploading'" :title="`上传中(${Math.floor(file.percent)}%)`">
          <a-icon type="loading"/>
          <span style="margin-left:5px">上传中…</span>
        </a-tooltip>

        <a-tooltip v-else-if="file.status==='done'" :title="file.name">
          <a-icon type="paper-clip"/>
          <span style="margin-left:5px">{{ ellipsisFileName }}</span>
        </a-tooltip>

        <a-tooltip v-else :title="file.message||'上传失败'">
          <a-icon type="exclamation-circle" style="color:red;"/>
          <span style="margin-left:5px">{{ ellipsisFileName }}</span>
        </a-tooltip>

        <template style="width: 30px">
          <a-dropdown :trigger="['click']" placement="bottomRight" style="margin-left: 10px;">
            <a-tooltip title="操作">
              <a-icon
                v-if="file.status!=='uploading'"
                type="setting"
                style="cursor: pointer;"/>
            </a-tooltip>

            <a-menu slot="overlay">
              <a-menu-item v-if="originColumn.allowDownload !== false" @click="handleClickDownloadFile">
                <span><a-icon type="download"/>&nbsp;下载</span>
              </a-menu-item>
              <a-menu-item v-if="originColumn.allowRemove !== false" @click="handleClickDeleteFile">
                <span><a-icon type="delete"/>&nbsp;删除</span>
              </a-menu-item>
              <a-menu-item @click="handleMoreOperation(originColumn)">
                <span><a-icon type="bars"/> 更多</span>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </div>
    </template>

    <a-upload
      v-show="!hasFile"
      name="file"
      :data="{'isup': 1}"
      :multiple="false"
      :action="uploadAction"
      :headers="uploadHeaders"
      :showUploadList="false"
      v-bind="cellProps"
      @change="handleChangeUpload"
    >
肖超群 authored
56
      <a-button icon="upload">{{ originColumn.btnText || '上传文件' }}</a-button>
肖超群 authored
57
58
59
60
61
62
    </a-upload>
    <j-file-pop ref="filePop" @ok="handleFileSuccess" :number="number"/>
  </div>
</template>

<script>
肖超群 authored
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
import {getFileAccessHttpUrl} from '@api/manage'
import JVxeCellMixins from '@/components/jeecg/JVxeTable/mixins/JVxeCellMixins'
import {ACCESS_TOKEN} from '@/store/mutation-types'
import JFilePop from '@/components/jeecg/minipop/JFilePop'

import JVxeUploadCell from '@/components/jeecg/JVxeTable/components/cells/JVxeUploadCell'

export default {
  name: 'JVxeFileCell',
  mixins: [JVxeCellMixins],
  components: {JFilePop},
  props: {},
  data() {
    return {
      innerFile: null,
      number: 0,
    }
  },
  computed: {
    /** upload headers */
    uploadHeaders() {
      let {originColumn: col} = this
      let headers = {}
      if (col.token === true) {
        headers['X-Access-Token'] = this.$ls.get(ACCESS_TOKEN)
肖超群 authored
88
      }
肖超群 authored
89
      return headers
肖超群 authored
90
91
    },
肖超群 authored
92
93
94
95
96
97
98
99
    /** 上传请求地址 */
    uploadAction() {
      if (!this.originColumn.action) {
        return window._CONFIG['domianURL'] + '/sys/common/upload'
      } else {
        return this.originColumn.action
      }
    },
肖超群 authored
100
肖超群 authored
101
102
103
    hasFile() {
      return this.innerFile != null
    },
肖超群 authored
104
肖超群 authored
105
106
107
108
109
110
111
112
113
114
    ellipsisFileName() {
      let length = 5
      let file = this.innerFile
      if (!file || !file.name) {
        return ''
      }
      if (file.name.length > length) {
        return file.name.substr(0, length) + '…'
      }
      return file.name
肖超群 authored
115
    },
肖超群 authored
116
117
118
119
120
121
122

    responseName() {
      if (this.originColumn.responseName) {
        return this.originColumn.responseName
      } else {
        return 'message'
      }
肖超群 authored
123
124
    },
肖超群 authored
125
126
127
128
129
130
131
  },
  watch: {
    innerValue: {
      immediate: true,
      handler() {
        if (this.innerValue) {
          this.innerFile = this.innerValue
肖超群 authored
132
        } else {
肖超群 authored
133
          this.innerFile = null
肖超群 authored
134
135
        }
      },
肖超群 authored
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    },
  },
  methods: {

    // 点击更多按钮
    handleMoreOperation(originColumn) {
      //update-begin-author:wangshuai date:20201021 for:LOWCOD-969 判断传过来的字段是否存在number,用于控制上传文件
      if (originColumn.number) {
        this.number = originColumn.number
      } else {
        this.number = 0
      }
      //update-end-author:wangshuai date:20201021 for:LOWCOD-969 判断传过来的字段是否存在number,用于控制上传文件
      if (originColumn && originColumn.fieldExtendJson) {
        let json = JSON.parse(originColumn.fieldExtendJson);
        this.number = json.uploadnum ? json.uploadnum : 0;
      }
      let path = ''
      if (this.innerFile) {
        path = this.innerFile.path
      }
      this.$refs.filePop.show('', path)
    },
肖超群 authored
159
肖超群 authored
160
161
162
163
164
165
166
    // 更多上传回调
    handleFileSuccess(file) {
      if (file) {
        this.innerFile.path = file.path
        this.handleChangeCommon(this.innerFile)
      }
    },
肖超群 authored
167
肖超群 authored
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    handleChangeUpload(info) {
      let {originColumn: col} = this
      let {file} = info
      let value = {
        name: file.name,
        type: file.type,
        size: file.size,
        status: file.status,
        percent: file.percent
      }
      if (file.response) {
        value['responseName'] = file.response[this.responseName]
      }
      if (file.status === 'done') {
        if (typeof file.response.success === 'boolean') {
          if (file.response.success) {
肖超群 authored
184
185
            value['path'] = file.response[this.responseName]
            this.handleChangeCommon(value)
肖超群 authored
186
187
188
          } else {
            value['status'] = 'error'
            value['message'] = file.response.message || '未知错误'
肖超群 authored
189
          }
肖超群 authored
190
191
192
193
        } else {
          // 考虑到如果设置action上传路径为非jeecg-boot后台,可能不会返回 success 属性的情况,就默认为成功
          value['path'] = file.response[this.responseName]
          this.handleChangeCommon(value)
肖超群 authored
194
        }
肖超群 authored
195
196
197
198
199
      } else if (file.status === 'error') {
        value['message'] = file.response.message || '未知错误'
      }
      this.innerFile = value
    },
肖超群 authored
200
肖超群 authored
201
202
203
204
205
    handleClickDownloadFile() {
      let {url, path} = this.innerFile || {}
      if (!url || url.length === 0) {
        if (path && path.length > 0) {
          url = getFileAccessHttpUrl(path.split(',')[0])
肖超群 authored
206
        }
肖超群 authored
207
208
209
210
211
      }
      if (url) {
        window.open(url)
      }
    },
肖超群 authored
212
肖超群 authored
213
214
    handleClickDeleteFile() {
      this.handleChangeCommon(null)
肖超群 authored
215
    },
肖超群 authored
216
217
218
219
220
221
222

  },
  // 【组件增强】注释详见:JVxeCellMixins.js
  enhanced: {
    switches: {visible: true},
    getValue: value => JVxeUploadCell.enhanced.getValue(value),
    setValue: value => JVxeUploadCell.enhanced.setValue(value),
肖超群 authored
223
  }
肖超群 authored
224
}
肖超群 authored
225
226
227
228
</script>

<style scoped lang="less">
</style>