upload-image-multiple.vue
2.59 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
<template>
<view>
<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" :maxCount="maxCount"/>
</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 {
// v-bind不支持 后续只能单个props传入
props: {
value: String,
maxCount: [String, Number],
},
data() {
return {
fileList: this.value
}
},
watch: {
value: {
handler(val) {
this.fileList = val? val.split(',').map(i => ({
url: i
})) : []
},
immediate: true,
},
fileList(val) {
if (val) {
this.$emit('input', val.map(i => i.url).join(',') || '')
}
}
},
methods: {
// 新增图片
async afterRead(event) {
const file = event.file || {}
const url = file.url || ''
const result = await this.uploadFilePromise(url)
},
uploadFilePromise(filePath) {
return new Promise((resolve, reject) => {
uni.showLoading({ title: '上传中...' });
uni.uploadFile({
url,
header,
filePath,
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.fileList.push({url: result[0]})
} else {
if (result[0]) {
this.fileList.push({url: result[0]})
} else {
setTimeout(() => {
uni.showToast({ title: data.message || response.errMsg || '上传失败', icon: 'none' });
}, 1500);
}
}
},
fail: () => {
uni.hideLoading();
setTimeout(() => {
uni.showToast({ title: '系统异常', icon: 'none' });
}, 1500);
}
});
})
},
// 删除
deletePic(event) {
uni.showModal({
title: '提示',
content: '确定删除这张图片吗?',
// #ifdef MP-DINGTALK
confirmButtonText: '确定',
cancelButtonText: '取消',
// #endif
success: (res) => {
if (res.confirm) {
const {index} = event || {}
this.fileList.splice(index, 1)
}
}
});
}
}
}
</script>
<style lang="scss">
.u-upload__button{
border-radius: $radius-md;
box-shadow: $shadow-normal;
}
</style>