index.js
5.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
var __assign =
(this && this.__assign) ||
function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, '__esModule', { value: true });
var component_1 = require('../common/component');
var utils_1 = require('./utils');
var shared_1 = require('./shared');
component_1.VantComponent({
props: __assign(
__assign(
{
disabled: Boolean,
multiple: Boolean,
uploadText: String,
useBeforeRead: Boolean,
afterRead: null,
beforeRead: null,
previewSize: {
type: null,
value: 90,
},
name: {
type: [Number, String],
value: '',
},
accept: {
type: String,
value: 'image',
},
fileList: {
type: Array,
value: [],
observer: 'formatFileList',
},
maxSize: {
type: Number,
value: Number.MAX_VALUE,
},
maxCount: {
type: Number,
value: 100,
},
deletable: {
type: Boolean,
value: true,
},
showUpload: {
type: Boolean,
value: true,
},
previewImage: {
type: Boolean,
value: true,
},
previewFullImage: {
type: Boolean,
value: true,
},
imageFit: {
type: String,
value: 'scaleToFill',
},
uploadIcon: {
type: String,
value: 'photograph',
},
},
shared_1.chooseImageProps
),
shared_1.chooseVideoProps
),
data: {
lists: [],
isInCount: true,
},
methods: {
formatFileList: function () {
var _a = this.data,
_b = _a.fileList,
fileList = _b === void 0 ? [] : _b,
maxCount = _a.maxCount;
var lists = fileList.map(function (item) {
return __assign(__assign({}, item), {
isImage:
typeof item.isImage === 'undefined'
? utils_1.isImageFile(item)
: item.isImage,
});
});
this.setData({ lists: lists, isInCount: lists.length < maxCount });
},
getDetail: function (index) {
return {
name: this.data.name,
index: index == null ? this.data.fileList.length : index,
};
},
startUpload: function () {
var _this = this;
var _a = this.data,
maxCount = _a.maxCount,
multiple = _a.multiple,
accept = _a.accept,
lists = _a.lists,
disabled = _a.disabled;
if (disabled) return;
utils_1
.chooseFile(
__assign(__assign({}, this.data), {
maxCount: maxCount - lists.length,
})
)
.then(function (res) {
var file = null;
if (utils_1.isVideo(res, accept)) {
file = __assign({ path: res.tempFilePath }, res);
} else {
file = multiple ? res.tempFiles : res.tempFiles[0];
}
_this.onBeforeRead(file);
})
.catch(function (error) {
_this.$emit('error', error);
});
},
onBeforeRead: function (file) {
var _this = this;
var _a = this.data,
beforeRead = _a.beforeRead,
useBeforeRead = _a.useBeforeRead;
var res = true;
if (typeof beforeRead === 'function') {
res = beforeRead(file, this.getDetail());
}
if (useBeforeRead) {
res = new Promise(function (resolve, reject) {
_this.$emit(
'before-read',
__assign(__assign({ file: file }, _this.getDetail()), {
callback: function (ok) {
ok ? resolve() : reject();
},
})
);
});
}
if (!res) {
return;
}
if (utils_1.isPromise(res)) {
res.then(function (data) {
return _this.onAfterRead(data || file);
});
} else {
this.onAfterRead(file);
}
},
onAfterRead: function (file) {
var maxSize = this.data.maxSize;
var oversize = Array.isArray(file)
? file.some(function (item) {
return item.size > maxSize;
})
: file.size > maxSize;
if (oversize) {
this.$emit('oversize', __assign({ file: file }, this.getDetail()));
return;
}
if (typeof this.data.afterRead === 'function') {
this.data.afterRead(file, this.getDetail());
}
this.$emit('after-read', __assign({ file: file }, this.getDetail()));
},
deleteItem: function (event) {
var index = event.currentTarget.dataset.index;
this.$emit(
'delete',
__assign(__assign({}, this.getDetail(index)), {
file: this.data.fileList[index],
})
);
},
onPreviewImage: function (event) {
if (!this.data.previewFullImage) return;
var index = event.currentTarget.dataset.index;
var lists = this.data.lists;
var item = lists[index];
wx.previewImage({
urls: lists
.filter(function (item) {
return item.isImage;
})
.map(function (item) {
return item.url || item.path;
}),
current: item.url || item.path,
fail: function () {
wx.showToast({ title: '预览图片失败', icon: 'none' });
},
});
},
onClickPreview: function (event) {
var index = event.currentTarget.dataset.index;
var item = this.data.lists[index];
this.$emit(
'click-preview',
__assign(__assign({}, item), this.getDetail(index))
);
},
},
});