uploadImageExample.js
4.54 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
// component/uploadImageExample/uploadImageExample.js
import { uploadFile } from '../../libs/request.js'
Component({
/**
* 组件的属性列表
*/
properties: {
currentIndex: {
type: Number,
value: 0
},
uploadImage: {
type: String,
value: ''
},
up: {
type: String,
value: ''
},
down: {
type: String,
value: ''
},
uploadImages: {
type: Array,
value: []
}
},
/**
* 组件的初始数据
*/
data: {
isShowExample: false,
exampleImages: [
{
src: 'http://img10.static.yhbimg.com/article/2018/11/21/15/01312f5ad204d40e0ae7ee9852d0ab942b.png',
title: '瑕疵细节',
style: 'example',
imageStyle: 'example-image'
}, {
src: '/images/and@3x.png',
style: 'and',
imageStyle: 'and-image'
}, {
src: 'http://img10.static.yhbimg.com/article/2018/11/21/15/0181d222be867f9ae182045ca7f99864e0.png',
title: '合格证',
style: 'example',
imageStyle: 'example-image'
}, {
src: '/images/and@3x.png',
style: 'and',
imageStyle: 'and-image'
}, {
src: 'http://img10.static.yhbimg.com/article/2018/11/21/15/01515d77fa41dc25e6bcd3996d0d76dabc.png',
title: '条码标签',
style: 'example',
imageStyle: 'example-image'
}, {
src: '/images/and@3x.png',
style: 'and',
imageStyle: 'and-image'
}, {
src: 'http://img12.static.yhbimg.com/article/2018/11/21/15/02687dd41e9835128bdaa7854629dd60f0.png',
title: '领标',
style: 'example',
imageStyle: 'example-image'
},
],
tempImagePaths: [],
finalImageArray: [],
imageBgWidth: 'upload-image-1'
},
/**
* 组件的方法列表
*/
methods: {
showExample(e) {
const isShowExample = !this.data.isShowExample;
this.setData({
isShowExample
})
},
deleteImageFromUploadImages(e) {
console.log(e);
const index = e.currentTarget.dataset.index;
console.log(index);
let tempImagePaths = this.data.tempImagePaths;
let finalImageArray = this.data.finalImageArray;
tempImagePaths.splice(index, 1);
finalImageArray.splice(index, 1);
this.setData({
tempImagePaths,
finalImageArray
})
this.triggerEvent('updateimagearrays', { currentIndex: this.properties.currentIndex, finalImageArray });
},
previewImages(e) {
const index = e.target.dataset.index;
console.log(index);
let tempImagePaths = this.data.tempImagePaths;
wx.previewImage({
current: tempImagePaths[index],
urls: tempImagePaths,
})
},
selectPhoto(e) {
let tempImagePaths = this.data.tempImagePaths;
let finalImageArray = this.data.finalImageArray;
let count = 4 - tempImagePaths.length;
const that = this;
wx.chooseImage({
count: count,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths;
const imagesPromiseArr = [];
tempFilePaths.forEach((item, index) => {
imagesPromiseArr.push(uploadFile(item, index + finalImageArray.length).then((data) => {
console.log('uploadFile.data: ', data);
console.log('uploadFile.then: ', data.index);
return {
index: data.index,
path: data.imagesList[0]
}
}));
});
wx.showLoading({
title: '图片上传中...',
})
Promise.all(imagesPromiseArr).then((res) => {
res.forEach((item) => {
finalImageArray[item.index] = item.path;
});
that.setData({
finalImageArray
})
that.triggerEvent('updateimagearrays', { currentIndex: that.properties.currentIndex, finalImageArray });
console.log(finalImageArray);
wx.hideLoading();
}).catch(error => {
wx.hideLoading();
})
if (tempImagePaths.length < 4) {
tempImagePaths = tempImagePaths.concat(tempFilePaths);
that.setData({
tempImagePaths
})
}
}
})
},
confirmReasonInfo(e) {
this.triggerEvent('confirmreason', { currentIndex: this.properties.currentIndex, reason: e.detail.value});
}
}
})