index.vue
4.22 KB
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
<template>
<div class="preview_image_page" :style="boxStyle">
<!-- 有图:点击走 el-image 原生大图预览 -->
<el-image
v-if="src"
class="preview_img"
:src="displaySrc"
:fit="fit"
:preview-src-list="previewList"
>
<div slot="error" class="img_state">
<i class="el-icon-picture-outline"></i>
<span class="state_text">加载失败</span>
</div>
</el-image>
<!-- 无图:占位 -->
<div v-else class="img_state">
<slot name="empty">
<i class="el-icon-picture-outline"></i>
<span class="state_text">{{ emptyText }}</span>
</slot>
</div>
<!-- 名称:浮在左下角 -->
<div v-if="name && showName" class="img_name">{{ name }}</div>
</div>
</template>
<script>
import { ossImageThumb, isOssUrl } from "@/utils/oss";
/**
* 公共图片组件(点击预览)
*
* props:
* src String 图片地址
* previewSrcList Array 预览列表(点开大图用),不传则只预览当前这张
* name String 名称,浮在左下角,不传则不显示
* showName Boolean 是否显示名称,默认 true
* fit String 填充方式,默认 cover
* width / height String 容器尺寸,默认 100%(撑满父级)
* ossProcess Boolean OSS 链接是否自动拼缩略图参数,默认 true
* (卡片显示小图,预览大图仍用原图 previewSrcList)
* emptyText String 无图占位文案,默认「暂无图片」
*
* 用法:
* <preview-image :src="url" :preview-src-list="list" name="面单照片" height="180px"></preview-image>
*/
export default {
name: "PreviewImage",
props: {
src: {
type: String,
default: "",
},
previewSrcList: {
type: Array,
default: function() {
return [];
},
},
name: {
type: String,
default: "",
},
showName: {
type: Boolean,
default: true,
},
fit: {
type: String,
default: "cover",
},
width: {
type: String,
default: "100%",
},
height: {
type: String,
default: "100%",
},
// OSS 链接是否自动拼缩略图参数(卡片显示小图)
ossProcess: {
type: Boolean,
default: true,
},
emptyText: {
type: String,
default: "暂无图片",
},
},
computed: {
boxStyle() {
return {
width: this.width,
height: this.height,
};
},
// 卡片显示用的图:OSS 链接默认拼缩略图参数;预览大图仍用原图(见 previewList)
displaySrc() {
if (this.ossProcess && isOssUrl(this.src)) {
return ossImageThumb(this.src);
}
return this.src;
},
previewList() {
const base =
this.previewSrcList && this.previewSrcList.length > 0
? this.previewSrcList
: this.src
? [this.src]
: [];
if (!this.src) {
return base;
}
// el-image 原生预览永远从第 0 项打开:把当前这张图排到首位,
// 点哪张默认呈现哪张,其余图按顺序跟在后面仍可左右切换。
return [this.src, ...base.filter((u) => u !== this.src)];
},
},
};
</script>
<style lang="scss" scoped>
.preview_image_page {
position: relative;
overflow: hidden;
background: #f5f7fa;
.preview_img {
width: 100%;
height: 100%;
border-radius: 4px;
display: block;
}
// 名称浮层:左下角
// pointer-events: none 让点击穿透到下层图片,点名称区域也能触发预览
.img_name {
position: absolute;
left: 8px;
bottom: 8px;
z-index: 4;
max-width: calc(100% - 16px);
padding: 2px 8px;
font-size: 12px;
line-height: 18px;
color: #fff;
background: rgba(0, 0, 0, 0.55);
border-radius: 4px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
pointer-events: none;
}
.img_state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
color: #c0c4cc;
font-size: 26px;
.state_text {
margin-top: 6px;
font-size: 12px;
}
}
}
</style>