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
| <template>
| <view
| class="cl-image"
| :style="[imgStyle, baseStyle]"
| :class="[
| {
| 'is-round': round,
| 'is-error': !src || isError,
| },
| ]"
| @tap="onPreview"
| >
| <view class="cl-image__placeholder" v-if="!src">
| <slot name="placeholder">
| <text class="icon cl-icon-image"></text>
| </slot>
| </view>
|
| <view class="cl-image__error" v-else-if="isError">
| <slot name="error">
| <text class="icon cl-icon-toast-warning"></text>
| </slot>
| </view>
|
| <template v-else>
| <view class="cl-image__loading" v-if="isLoading && showLoading">
| <cl-loading />
| </view>
|
| <image
| class="cl-image__target"
| :src="src"
| :mode="mode"
| :lazy-load="lazyLoad"
| :webp="webp"
| :show-menu-by-longpress="showMenuByLongpress"
| @error="handleError"
| @load="handleLoad"
| />
|
| <slot></slot>
| </template>
| </view>
| </template>
|
| <script lang="ts">
| import { type PropType, computed, defineComponent, ref, watch } from "vue";
| import { isNumber, isArray, isString, isNaN } from "lodash-es";
| import { parseRpx } from "/@/cool/utils";
| import { useStyle } from "../../hooks";
|
| export default defineComponent({
| name: "cl-image",
|
| props: {
| // 图片地址
| src: String,
| // 图片裁剪、缩放的模式
| mode: {
| type: String,
| default: "aspectFill",
| },
| // 图片大小
| size: {
| type: [String, Number, Array],
| default: "100%",
| },
| // 是否圆角
| round: Boolean,
| // 当前预览值
| previewCurrent: String,
| // 预览列表
| previewList: Array as PropType<string[]>,
| // 懒加载
| lazyLoad: Boolean,
| fadeShow: {
| type: Boolean,
| default: true,
| },
| webp: Boolean,
| showMenuByLongpress: Boolean,
| showLoading: {
| type: Boolean,
| default: true,
| },
| },
|
| setup(props, { emit }) {
| // 是否加载失败
| const isError = ref(false);
|
| // 是否加载中
| const isLoading = ref(false);
|
| // 样式
| const imgStyle = computed(() => {
| const [height, width] = size.value;
|
| let minWidth = "0";
| let minHeight = "0";
|
| // 不是有效高
| if (isNaN(parseInt(height))) {
| minHeight = width;
| }
|
| // 不是有效宽
| if (isNaN(parseInt(width))) {
| minWidth = height;
| }
|
| return {
| height,
| width,
| minWidth,
| minHeight,
| };
| });
|
| // 尺寸
| const size = computed(() => {
| let size: any = ["100%", "100%"];
|
| if (isString(props.size) || isNumber(props.size)) {
| size = [props.size, props.size];
| } else if (isArray(props.size)) {
| size = props.size;
| }
|
| return size.map(parseRpx);
| });
|
| // 加载失败
| function handleError(e: Event) {
| isError.value = true;
| isLoading.value = false;
| emit("error", e);
| }
|
| // 加载成功
| function handleLoad(e: Event) {
| isError.value = false;
| isLoading.value = false;
| emit("load", e);
| }
|
| // 点击是否预览图片
| function onPreview(e: Event) {
| if (props.previewList) {
| uni.previewImage({
| urls: props.previewList || [],
| current: props.previewCurrent || props.src,
| });
|
| e.stopPropagation();
| }
| }
|
| watch(
| () => props.src,
| (val) => {
| isLoading.value = !!val;
| isError.value = false;
| },
| {
| immediate: true,
| },
| );
|
| return {
| isError,
| isLoading,
| size,
| imgStyle,
| handleError,
| handleLoad,
| onPreview,
| ...useStyle(),
| };
| },
| });
| </script>
|
|