|
1
2
3
4
5
6
|
<template>
<a-upload
name="file"
:multiple="true"
:action="uploadAction"
:headers="headers"
|
|
7
|
:data="{'biz':bizPath}"
|
|
8
9
|
:fileList="fileList"
:beforeUpload="beforeUpload"
|
|
10
|
@change="handleChange"
|
|
11
12
|
:disabled="disabled"
:returnUrl="returnUrl">
|
|
13
14
15
16
17
18
19
20
21
22
|
<a-button>
<a-icon type="upload" />{{ text }}
</a-button>
</a-upload>
</template>
<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from "@/store/mutation-types"
|
|
23
|
import { getFileAccessHttpUrl } from '@/api/manage';
|
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
const FILE_TYPE_ALL = "all"
const FILE_TYPE_IMG = "image"
const FILE_TYPE_TXT = "file"
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,"/");
}
return path.substring(path.lastIndexOf("/")+1);
}
export default {
name: 'JUpload',
data(){
return {
uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",
|
|
43
|
urlDownload:window._CONFIG['staticDomainURL'],
|
|
44
|
headers:{},
|
|
45
46
|
fileList: [],
newFileList: [],
|
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
}
},
props:{
text:{
type:String,
required:false,
default:"点击上传"
},
fileType:{
type:String,
required:false,
default:FILE_TYPE_ALL
},
/*这个属性用于控制文件上传的业务路径*/
bizPath:{
type:String,
required:false,
default:"temp"
},
value:{
|
|
67
|
type:[String,Array],
|
|
68
69
|
required:false
},
|
|
70
71
72
73
74
75
76
|
// update-begin- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
disabled:{
type:Boolean,
required:false,
default: false
},
// update-end- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
|
|
77
|
//此属性被废弃了
|
|
78
79
80
81
82
|
triggerChange:{
type: Boolean,
required: false,
default: false
},
|
|
83
84
85
86
87
88
89
90
91
92
|
/**
* update -- author:lvdandan -- date:20190219 -- for:Jupload组件增加是否返回url,
* true:仅返回url
* false:返回fileName filePath fileSize
*/
returnUrl:{
type:Boolean,
required:false,
default: true
},
|
|
93
94
95
|
},
watch:{
value(val){
|
|
96
|
if (val instanceof Array) {
|
|
97
98
99
100
101
|
if(this.returnUrl){
this.initFileList(val.join(','))
}else{
this.initFileListArr(val);
}
|
|
102
103
104
|
} else {
this.initFileList(val)
}
|
|
105
106
107
108
109
110
111
112
|
}
},
created(){
const token = Vue.ls.get(ACCESS_TOKEN);
this.headers = {"X-Access-Token":token}
},
methods:{
|
|
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
initFileListArr(val){
if(!val || val.length==0){
this.fileList = [];
return;
}
let fileList = [];
for(var a=0;a<val.length;a++){
fileList.push({
uid:uidGenerator(),
name:val[a].fileName,
status: 'done',
url: val[a].filePath,
response:{
status:"history",
message:val[a].filePath
}
})
}
this.fileList = fileList
},
|
|
133
134
|
initFileList(paths){
if(!paths || paths.length==0){
|
|
135
136
137
138
139
|
//return [];
// update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
this.fileList = [];
return;
// update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
|
|
140
141
142
143
|
}
let fileList = [];
let arr = paths.split(",")
for(var a=0;a<arr.length;a++){
|
|
144
|
let url = getFileAccessHttpUrl(arr[a],this.urlDownload,"http");
|
|
145
146
147
148
|
fileList.push({
uid:uidGenerator(),
name:getFileName(arr[a]),
status: 'done',
|
|
149
|
url: url,
|
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
response:{
status:"history",
message:arr[a]
}
})
}
this.fileList = fileList
},
handlePathChange(){
let uploadFiles = this.fileList
let path = ''
if(!uploadFiles || uploadFiles.length==0){
path = ''
}
let arr = [];
for(var a=0;a<uploadFiles.length;a++){
arr.push(uploadFiles[a].response.message)
}
if(arr.length>0){
path = arr.join(",")
}
|
|
172
|
this.$emit('change', path);
|
|
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
},
beforeUpload(file){
var fileType = file.type;
if(fileType===FILE_TYPE_IMG){
if(fileType.indexOf('image')<0){
this.$message.warning('请上传图片');
return false;
}
}else if(fileType===FILE_TYPE_TXT){
if(fileType.indexOf('image')>=0){
this.$message.warning('请上传文件');
return false;
}
}
//TODO 扩展功能验证文件大小
return true
},
handleChange(info) {
console.log("--文件列表改变--")
let fileList = info.fileList
if(info.file.status==='done'){
if(info.file.response.success){
fileList = fileList.map((file) => {
if (file.response) {
|
|
197
198
|
let reUrl = file.response.message;
file.url = getFileAccessHttpUrl(reUrl,this.urlDownload,"http");
|
|
199
200
201
202
|
}
return file;
});
}
|
|
203
|
//this.$message.success(`${info.file.name} 上传成功!`);
|
|
204
205
206
207
208
209
210
|
}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'){
|
|
211
212
213
214
215
216
217
218
219
220
221
222
223
|
//returnUrl为true时仅返回文件路径
if(this.returnUrl){
this.handlePathChange()
}else{
//returnUrl为false时返回文件名称、文件路径及文件大小
fileList = fileList.filter((file) => {
if (file.response) {
return file.response.success === true;
}
return false;
}).map((file) => {
var fileJson = {
fileName:file.name,
|
|
224
|
filePath:file.response.message,
|
|
225
226
227
228
229
230
|
fileSize:file.size
};
this.newFileList.push(fileJson);
this.$emit('change', this.newFileList);
});
}
|
|
231
232
233
234
235
236
|
}
},
handleDelete(file){
//如有需要新增 删除逻辑
console.log(file)
},
|
|
237
238
239
240
|
},
model: {
prop: 'value',
event: 'change'
|
|
241
242
243
244
245
246
247
|
}
}
</script>
<style scoped>
</style>
|