Blame view

ant-design-vue-jeecg/src/components/jeecg/JImageUpload.vue 6.71 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
  <div class="img">
    <a-upload
      name="file"
      listType="picture-card"
      :multiple="isMultiple"
      :action="uploadAction"
      :headers="headers"
      :data="{biz:bizPath}"
      :fileList="fileList"
      :beforeUpload="beforeUpload"
      :disabled="disabled"
      :isMultiple="isMultiple"

      @change="handleChange"
      @preview="handlePreview"
      :class="[!isMultiple?'imgupload':'', (!isMultiple && picUrl)?'image-upload-single-over':'' ]">
      <div>
        <!--<img v-if="!isMultiple && picUrl" :src="getAvatarView()" style="width:100%;height:100%"/>-->
        <div class="iconp">
肖超群 authored
21
          <a-icon :type="uploadLoading ? 'loading' : 'plus'"/>
肖超群 authored
22
23
24
25
26
27
28
29
30
31
32
          <div class="ant-upload-text">{{ text }}</div>
        </div>
      </div>
      <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel()">
        <img alt="example" style="width: 100%" :src="previewImage"/>
      </a-modal>
    </a-upload>
  </div>
</template>

<script>
肖超群 authored
33
34
35
import Vue from 'vue'
import {ACCESS_TOKEN} from "@/store/mutation-types"
import {getFileAccessHttpUrl} from '@/api/manage'
肖超群 authored
36
肖超群 authored
37
38
39
40
41
42
43
const uidGenerator = () => {
  return '-' + parseInt(Math.random() * 10000 + 1, 10);
}
const getFileName = (path) => {
  if (path.lastIndexOf("\\") >= 0) {
    let reg = new RegExp("\\\\", "g");
    path = path.replace(reg, "/");
肖超群 authored
44
  }
肖超群 authored
45
46
47
48
49
50
51
52
53
54
55
56
57
  return path.substring(path.lastIndexOf("/") + 1);
}
export default {
  name: 'JImageUpload',
  data() {
    return {
      uploadAction: window._CONFIG['domianURL'] + "/sys/common/upload",
      uploadLoading: false,
      picUrl: false,
      headers: {},
      fileList: [],
      previewImage: "",
      previewVisible: false,
肖超群 authored
58
    }
肖超群 authored
59
60
61
62
63
64
  },
  props: {
    text: {
      type: String,
      required: false,
      default: "上传"
肖超群 authored
65
    },
肖超群 authored
66
67
68
69
70
    /*这个属性用于控制文件上传的业务路径*/
    bizPath: {
      type: String,
      required: false,
      default: "temp"
肖超群 authored
71
    },
肖超群 authored
72
73
74
    value: {
      type: [String, Array],
      required: false
肖超群 authored
75
    },
肖超群 authored
76
77
78
79
    disabled: {
      type: Boolean,
      required: false,
      default: false
肖超群 authored
80
    },
肖超群 authored
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    isMultiple: {
      type: Boolean,
      required: false,
      default: false
    },
    //update-begin-author:wangshuai date:20201021 for:LOWCOD-969 新增number属性,用于判断上传数量
    number: {
      type: Number,
      required: false,
      default: 0
    }
    //update-end-author:wangshuai date:20201021 for:LOWCOD-969 新增number属性,用于判断上传数量
  },
  watch: {
    value: {
      handler(val, oldValue) {
        if (val instanceof Array) {
          this.initFileList(val.join(','))
        } else {
          this.initFileList(val)
肖超群 authored
101
        }
肖超群 authored
102
103
        if (!val || val.length == 0) {
          this.picUrl = false;
肖超群 authored
104
105
        }
      },
肖超群 authored
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
      //立刻执行handler
      immediate: true
    }
  },
  created() {
    const token = Vue.ls.get(ACCESS_TOKEN);
    this.headers = {"X-Access-Token": token}
  },
  methods: {
    initFileList(paths) {
      if (!paths || paths.length == 0) {
        this.fileList = [];
        return;
      }
      this.picUrl = true;
      let fileList = [];
      let arr = paths.split(",")
      for (var a = 0; a < arr.length; a++) {
        let url = getFileAccessHttpUrl(arr[a]);
        fileList.push({
          uid: uidGenerator(),
          name: getFileName(arr[a]),
          status: 'done',
          url: url,
          response: {
            status: "history",
            message: arr[a]
肖超群 authored
133
          }
肖超群 authored
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        })
      }
      this.fileList = fileList
    },
    beforeUpload: function (file) {
      var fileType = file.type;
      if (fileType.indexOf('image') < 0) {
        this.$message.warning('请上传图片');
        return false;
      }
    },
    handleChange(info) {
      this.picUrl = false;
      let fileList = info.fileList
      //update-begin-author:wangshuai date:20201022 for:LOWCOD-969 判断number是否大于0和是否多选,返回选定的元素。
      if (this.number > 0 && this.isMultiple) {
        fileList = fileList.slice(-this.number);
      }
      //update-end-author:wangshuai date:20201022 for:LOWCOD-969 判断number是否大于0和是否多选,返回选定的元素。
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          this.picUrl = true;
          fileList = fileList.map((file) => {
            if (file.response) {
              file.url = file.response.message;
肖超群 authored
159
            }
肖超群 authored
160
161
            return file;
          });
肖超群 authored
162
        }
肖超群 authored
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        //this.$message.success(`${info.file.name} 上传成功!`);
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 上传失败.`);
      } else if (info.file.status === 'removed') {
        this.handleDelete(info.file)
      }
      this.fileList = fileList
      if (info.file.status === 'done' || info.file.status === 'removed') {
        this.handlePathChange()
      }
    },
    // 预览
    handlePreview(file) {
      this.previewImage = file.url || file.thumbUrl
      this.previewVisible = true
    },
    getAvatarView() {
      if (this.fileList.length > 0) {
        let url = this.fileList[0].url
        return getFileAccessHttpUrl(url)
      }
    },
    handlePathChange() {
      let uploadFiles = this.fileList
      let path = ''
      if (!uploadFiles || uploadFiles.length == 0) {
        path = ''
      }
      let arr = [];
      if (!this.isMultiple && uploadFiles.length > 0) {
        arr.push(uploadFiles[uploadFiles.length - 1].response.message)
      } else {
        for (let a = 0; a < uploadFiles.length; a++) {
          // update-begin-author:taoyan date:20200819 for:【开源问题z】上传图片组件 LOWCOD-783
          if (uploadFiles[a].status === 'done') {
            arr.push(uploadFiles[a].response.message)
          } else {
            return;
          }
          // update-end-author:taoyan date:20200819 for:【开源问题z】上传图片组件 LOWCOD-783
肖超群 authored
203
        }
肖超群 authored
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
      }
      if (arr.length > 0) {
        path = arr.join(",")
      }
      this.$emit('change', path);
    },
    handleDelete(file) {
      //如有需要新增 删除逻辑
      console.log(file)
    },
    handleCancel() {
      this.close();
      this.previewVisible = false;
    },
    close() {
肖超群 authored
219
220

    },
肖超群 authored
221
222
223
224
  },
  model: {
    prop: 'value',
    event: 'change'
肖超群 authored
225
  }
肖超群 authored
226
}
肖超群 authored
227
228
229
</script>

<style scoped>
肖超群 authored
230
231
232
233
234
/* update--begin--autor:lvdandan-----date:20201016------for:j-image-upload图片组件单张图片详情回显空白
* https://github.com/zhangdaiscott/jeecg-boot/issues/1810
* https://github.com/zhangdaiscott/jeecg-boot/issues/1779
*/
235
::v-deep .imgupload .iconp {
肖超群 authored
236
237
  padding: 20px;
}
肖超群 authored
238
肖超群 authored
239
/* update--end--autor:lvdandan-----date:20201016------for:j-image-upload图片组件单张图片详情回显空白*/
肖超群 authored
240
241
::v-deep .image-upload-single-over .ant-upload-select {
肖超群 authored
242
243
  display: none
}
肖超群 authored
244
</style>