upload-image.vue
4.53 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
168
169
170
171
172
173
174
175
<template>
<view class="upload-image" hover-class="active" hover-start-time="20" hover-stay-time="70" @click="handleUpload">
<template v-if="safeValue">
<image class="upload-image__image" :src="safeValue" mode="aspectFill" @click="previewImage"></image>
<view v-if="!readOnly" class="upload-image__close" @click.stop="removeImage">
<u-icon name="close"></u-icon>
</view>
</template>
<template v-else>
<slot v-if="$slots.default || $slots.$default"></slot>
<template v-else>
<template v-if="readOnly">
<view class="upload-image__icon">
<u-icon name="photo"></u-icon>
</view>
</template>
<template v-else>
<view class="upload-image__icon">
<u-icon name="camera"></u-icon>
</view>
<text class="upload-image__text">{{ text }}</text>
</template>
</template>
</template>
</view>
</template>
<script>
import cache from '@/utils/cache';
import config from '@/config';
const url = config.uploadHost;
const userInfo = cache.get('USERINFO')
const header = {
'Authorization': 'Bearer ' + userInfo?.accessToken
};
export default {
name: 'upload-image',
props: {
value: String,
icon: {
type: String,
default: 'photocamera'
},
text: {
type: String,
default: '点击上传'
},
readOnly: Boolean,
},
computed: {
safeValue() {
if (this.value) {
const imgList = this.value.replace(/^(\s|\,)+|(\s|\,)+$/g, '').split(',') || [];
return imgList.length > 0 ? imgList[0] : '';
}
return '';
}
},
methods: {
// 预览图片
previewImage() {
uni.previewImage({ urls: this.value.replace(/^(\s|\,)+|(\s|\,)+$/g, '').split(',') });
},
// 删除图片
removeImage() {
uni.showModal({
title: '提示',
content: '确定删除这张图片吗?',
// #ifdef MP-DINGTALK
confirmButtonText: '确定',
cancelButtonText: '取消',
// #endif
success: (res) => {
if (res.confirm) {
this.$emit('input', '');
}
}
});
},
// 上传图片
handleUpload() {
if (!this.value && !this.readOnly) {
uni.chooseImage({
count: 1,
success: (res) => {
const tempFilePaths = res.tempFilePaths || [];
if (tempFilePaths.length > 0) {
uni.showLoading({ title: '上传中...' });
uni.uploadFile({
url,
header,
filePath: tempFilePaths[0],
name: 'file',
formData: {
module: 'dispatch-ma',
category: 'opr'
},
success: (response) => {
uni.hideLoading();
const { statusCode } = response;
const data = JSON.parse(response.data.replace(/\ufeff/g, '') || '{}');
const result = data.result || [];
if (statusCode == 200) {
this.$emit('input', result[0]);
} else {
if (result[0]) {
this.$emit('input', result[0]);
} else {
setTimeout(() => {
uni.showToast({ title: data.message || response.errMsg || '上传失败', icon: 'none' });
}, 1500);
}
}
},
fail: () => {
uni.hideLoading();
setTimeout(() => {
uni.showToast({ title: '系统异常', icon: 'none' });
}, 1500);
}
});
}
}
});
}
}
}
};
</script>
<style lang="scss">
.upload-image {
height: 300upx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
color: rgba(151, 151, 151, 0.5);
&.active {
background-color: $color-field;
}
&__image {
width: 100%;
height: 100%;
}
&__close {
position: absolute;
top: 0;
right: 0;
transform: translateX(50%) translateY(-50%);
background-color: $color-red;
border-radius: 50%;
padding: 5upx;
height: 50upx;
width: 50upx;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
color: $color-white;
line-height: 1;
}
&__icon {
font-size: 70upx;
padding-bottom: 10upx;
line-height: 1;
}
&__text {
line-height: 1;
}
}
</style>