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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<template>
<div class='app-picture-preview'>
<ul class="app-picture-preview__content">
<li v-for="(file,index) in files" :key="index" class="app-picture-preview__content__item">
<div>
<el-image :src="file.dynaImgUrl">
<div slot='error' class='item__img'>
<img src="@/assets/img/picture.png">
</div>
</el-image>
<div class='item__action'>
<span class='item__action__preview'>
<i class='el-icon-zoom-in' @click="onPreview(file)"></i>
</span>
<span class='item__action__download'>
<i class='el-icon-download' @click="onDownload(file)"></i>
</span>
</div>
</div>
</li>
</ul>
<!-- 预览 -->
<modal v-model="dialogVisible" footer-hide width="auto" class-name='app-picture-preview__model'>
<el-image :src="dialogImageUrl">
<div slot='error' class='model__img'>
<img src="@/assets/img/picture.png">
</div>
</el-image>
</modal>
</div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Watch, Provide } from 'vue-property-decorator';
import { Environment } from '@/environments/environment';
import { Subject, Subscription } from 'rxjs';
import { AppServiceBase, Http, ImgurlBase64 } from 'ibiz-core';
@Component({})
export default class AppImagePreview extends Vue {
/**
* 表单状态
*
* @type {Subject<any>}
* @memberof AppImagePreview
*/
@Prop() public formState?: Subject<any>
/**
* 表单状态事件
*
* @private
* @type {(Subscription | undefined)}
* @memberof AppImagePreview
*/
private formStateEvent: Subscription | undefined;
/**
* 初始化值
*
* @type {*}
* @memberof AppImagePreview
*/
@Prop() public value?: any;
/**
* 数据值变化
*
* @param {*} newval
* @param {*} val
* @returns
* @memberof AppImagePreview
*/
@Watch('value')
onValueChange(newval: any, val: any) {
this.setFiles(newval)
}
/**
* 所属表单项名称
*
* @type {string}
* @memberof AppImagePreview
*/
@Prop() public name!: string;
/**
* 上传文件路径
*
* @memberof AppImagePreview
*/
public uploadUrl = Environment.BaseUrl + Environment.UploadFile;
/**
* 下载文件路径
*
* @memberof AppImagePreview
*/
public downloadUrl = Environment.BaseUrl + Environment.ExportFile;
/**
* 文件列表
*
* @memberof AppImagePreview
*/
@Provide() public files = [];
/**
* 设置files
*
* @private
* @memberof AppImagePreview
*/
private setFiles(value:any): void {
let _files = JSON.parse(value);
if (value && Object.prototype.toString.call(_files)=='[object Array]') {
this.files = _files;
this.files.forEach((file: any) => {
let url = `${this.downloadUrl}/${file.id}`;
ImgurlBase64.getInstance()
.getImgURLOfBase64(url)
.then((res: any) => {
file.dynaImgUrl = res;
});
file.url = url;
});
} else {
this.files = [];
}
}
/**
* vue 生命周期
*
* @memberof AppImagePreview
*/
public created() {
if (this.formState) {
this.formStateEvent = this.formState.subscribe(($event: any) => {
// 表单加载完成
if (Object.is($event.type, 'load')) {
this.setFiles(this.value);
}
});
}
}
/**
* vue 生命周期
*
* @memberof AppImagePreview
*/
public mounted() {
this.setFiles(this.value);
}
/**
* 组件销毁
*
* @memberof AppImagePreview
*/
public destroyed(): void {
if (this.formStateEvent) {
this.formStateEvent.unsubscribe();
}
}
/**
* 下载文件
*
* @param {*} file
* @memberof AppImagePreview
*/
public onDownload(file: any) {
let downloadUrl: string = file.url;
const BaseUrl = AppServiceBase.getInstance().getAppEnvironment().BaseUrl;
if (downloadUrl.startsWith(BaseUrl)) {
downloadUrl = downloadUrl.replace(BaseUrl, '');
}
// 发送get请求
Http.getHttp()({
method: 'get',
url: downloadUrl,
responseType: 'blob'
}).then((response: any) => {
if (!response || response.status != 200) {
this.$throw(this.$t('components.appfileupload.downloaderror'));
return;
}
// 请求成功,后台返回的是一个文件流
if (response.data) {
// 获取文件名
const filename = file.name;
const ext = '.' + filename.split('.').pop();
let filetype = this.calcFilemime(ext);
// 用blob对象获取文件流
let blob = new Blob([response.data], {type: filetype});
// 通过文件流创建下载链接
var href = URL.createObjectURL(blob);
// 创建一个a元素并设置相关属性
let a = document.createElement('a');
a.href = href;
a.download = filename;
// 添加a元素到当前网页
document.body.appendChild(a);
// 触发a元素的点击事件,实现下载
a.click();
// 从当前网页移除a元素
document.body.removeChild(a);
// 释放blob对象
URL.revokeObjectURL(href);
} else {
this.$throw(this.$t('components.appfileupload.downloaderror'));
}
}).catch((error: any) => {
console.error(error);
});
}
/**
* 计算文件mime类型
*
* @param filetype 文件后缀
* @memberof AppImageUpload
*/
public calcFilemime(filetype: string): string {
let mime = "image/png";
switch(filetype) {
case ".wps":
mime = "application/kswps";
break;
case ".doc":
mime = "application/msword";
break;
case ".docx":
mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case ".txt":
mime = "text/plain";
break;
case ".zip":
mime = "application/zip";
break;
case ".png":
mime = "imgage/png";
break;
case ".gif":
mime = "image/gif";
break;
case ".jpeg":
mime = "image/jpeg";
break;
case ".jpg":
mime = "image/jpeg";
break;
case ".rtf":
mime = "application/rtf";
break;
case ".avi":
mime = "video/x-msvideo";
break;
case ".gz":
mime = "application/x-gzip";
break;
case ".tar":
mime = "application/x-tar";
break;
}
return mime;
}
/**
* 预览图片地址
*
* @type {string}
* @memberof AppImagePreview
*/
public dialogImageUrl: string = '';
/**
* 是否显示预览界面
*
* @type {boolean}
* @memberof AppImagePreview
*/
public dialogVisible: boolean = false;
/**
* 预览
*
* @param {*} file
* @memberof AppImagePreview
*/
public onPreview(file: any) {
this.dialogImageUrl = file.dynaImgUrl;
this.dialogVisible = true;
}
}
</script>